SQL results from Powershell

Cyberrad

Limp Gawd
Joined
Sep 12, 2008
Messages
327
Once again I am stumped on a script and require the assistance of the wonderful [H] members.

I am writing a Powershell script that enables me to backup my event logs to a SQL database. I need a way to make sure I only input unique events into the database. Here is what I have come up with:

Code:
function RunQuery($SqlQuery) {
    $SqlConnection = New-Object System.Data.SqlClient.SqlConnection
    $SqlConnection.ConnectionString = "Server = $SQLServer; Database = $SQLDBName; Integrated Security = True"

    $SqlCmd = New-Object System.Data.SQLClient.SqlCommand
    $SqlCmd.CommandText = $SqlQuery
    $SqlCmd.Connection = $SqlConnection

    $SQLAdapter = New-Object System.Data.SqlClient.SqlDataAdapter
    $SqlAdapter.SelectCommand = $SqlCmd

    $DataSet = New-Object System.Data.DataSet
    $SqlAdapter.Fill($DataSet)

    $DataSet.Tables[0]
    
    $SQLConnection.Close()
}

$Machine = Some Machine

$Result = RunQuery("SELECT LastIndex FROM Poll WHERE Machine = '$Machine'")

$Result will be put into the following line:

Code:
$AppLog = Get-EventLog -LogName "Application" -ComputerName "Master" | where {$_.Index -GT "$Result"}

When I output $Result I get the following:

Code:
1

LastIndex                                                                                                                                              
---------                                                                                                                                              
20

So, here is where I need the help. Is there a way to pull out the value of LastIndex, in this case 20, as a string?
 
I was able to get rid of the 1 in $Result's output.

Code:
$SqlAdapter.Fill($DataSet) | Out-Null

Now the output looks like this:

Code:
LastIndex                                                                                                                                              
---------                                                                                                                                              
20

It has a blank line above LastIndex. If I use format-table -HideTableHeaders then I get 20 with a lot of white space above and below 20 like this:

Code:
20

Any ideas on how to remove this white space? Another odd thing to note is that when I do the following:

Code:
Write-Host "$Result"

I get this instead of the output above:

Code:
Microsoft.PowerShell.Commands.Internal.Format.FormatStartData Microsoft.PowerShell.Commands.Internal.Format.GroupStartData Microsoft.PowerShel
l.Commands.Internal.Format.FormatEntryData Microsoft.PowerShell.Commands.Internal.Format.GroupEndData Microsoft.PowerShell.Commands.Internal.Format.Form
atEndData
 
Code:
$Result = $Result.Get_Item("LastIndex")
This fixed it along with the out-null from the above post.

Thanks for looking!
 
Back
Top