I'm trying to get the number of rows from a Microsoft SQL database table in Powershell as an integer and am stuck peeling the figure out of the object Powershell is returning.
I have;
$SQLServer = "MSSQL01" $SQLDBName = "TheTable" $SqlQuery = "select count(*) from cases where opened_date =< '2014-07-30'"
$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)
$SqlConnection.Close()
Now $DataSet.Tables[0] in this instance returns an object like;
Table Name
-----------
45
How can I grab just the 45 as an int?
ExecuteScalarinstead of filling an adapter