I have the following script that runs a stored procedure and returns a value. The problem is it returns a hash table and using result.answer returns nothing. How can I get the value without the hash table attached?

$SqlConnection = New-Object System.Data.SqlClient.SqlConnection
$SqlConnection.ConnectionString = "Server=server;Database=AspDotNetStoreFront;Integrated Security=True"
$SqlCmd = New-Object System.Data.SqlClient.SqlCommand
$SqlCmd.CommandText = "exec dbo.[sp_ESC_CheckForOrderingIssues] 1"
$SqlCmd.Connection = $SqlConnection
$SqlAdapter = New-Object System.Data.SqlClient.SqlDataAdapter
$SqlAdapter.SelectCommand = $SqlCmd
$DataSet = New-Object System.Data.DataSet
$SqlAdapter.Fill($DataSet)
$SqlConnection.Close()
$Result = $DataSet.Tables[0]
$Result
If ($Result -eq 1 )
{PANIC}
else {Dont Panic}
link|improve this question

You can use $Result.Values to get to the content of the hash table. Using $Result.Keys would get to the other column of info. – Shawn Melton Oct 12 '11 at 13:42
Thanks for the quick response! Get-Member Doesn't show a property called values, and calling $result.values returns nothing. – Brandan Oct 12 '11 at 14:14
It looks like it returns as a Data table since you are using Syste.Data.DataSet. So get-member should show a Property for each column returned. If you are only wanting one column change $result to be equal to $DataSet.Tables[0] | Select Column1. – Shawn Melton Oct 12 '11 at 14:43
Thanks again for the help, But no matter what i try it still returns the result in a table, so the if statement cant understand the result, and never runs because it doesn't see the result in a usable form. I have scoured the internet for a solution.. Is there a better way to run a Stored Procedure and then react based on the returned info from that SP? – Brandan Oct 12 '11 at 15:35
feedback

1 Answer

up vote 0 down vote accepted

This is the only way i could get it to work.

$Result = $DataSet.Tables[0] |
  select column1 $Result = $Result | 
    ConvertTo-Csv 
$Result = $Result[2] 
[int]$Result = $Result -replace ('"',' ') 
$Result If ($Result -eq 2)

If anyone knows a better way I would like to hear about it.

link|improve this answer
feedback

Your Answer

 
or
required, but never shown

Not the answer you're looking for? Browse other questions tagged or ask your own question.