问题
I wrote a simple powershell script that checks the number of tickets in a PSA service board and outputs the number of tickets and a color associated with it. However, my script is printing a '1' before the results. I am very new to powershell, and I can't seem to find the line that would print this. Any help would be appreciated. Thanks
$SqlServer = "server"
$SqlCatalog = "database"
$SqlQuery = "SELECT COUNT(*) FROM v_rpt_Service WHERE Board_Name = 'Traige' AND Parent = ''"
$SqlConnection = New-Object System.Data.SqlClient.SqlConnection
$SqlConnection.ConnectionString = "Server = $SqlServer; Database = $SqlCatalog; uid = user; pwd = pass"
$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)
$SqlConnection.Close()
$value = $DataSet.Tables[0].Rows[0][0]
$color = "Red"
if($value -lt 4){
$color = "Yellow"
}
if($value -lt 1){
$color = "Green"
}
$obj = new-object System.object
add-member -inputobject $obj -membertype NoteProperty -Name Tickets -value $value
add-member -inputobject $obj -membertype NoteProperty -Name Color -value $color
$obj
回答1:
$SqlAdapter.Fill($DataSet) | Out-Null
Read here about DbDataAdapter.Fill and its return value:
Type: System.Int32 The number of rows successfully added to or refreshed in the DataSet. This does not include rows affected by statements that do not return rows.
回答2:
Here is an alternative that casts to void without using the pipeline. The pipeline should be avoided for performance reasons.
[void] $SqlAdapter.Fill($DataSet);
回答3:
You should be able to narrow it down to the actual line by injecting some lines that just print out some text. Once you find the actual line, you can pipe it through the "out-null" commandlet in order to suppress the output.
http://technet.microsoft.com/en-us/library/dd315279.aspx
来源:https://stackoverflow.com/questions/10535816/my-powershell-script-is-printing-a-1-before-my-expected-output-why