title: Reading from SQL SELECT in using Powershell
body: <p>I have been looking for the issue which I couldn't figure out. Need help.
  Below is the powershell script that is used to revoke the Public rrights on available media. I am trying to check the result of my SQL query and then returning the result for the same in the powershell variable sNumCountAvailMed but whenever the reader reads the information it always give me the Fieldcount as 1 and doesn't give any other result . Please let me know what am I doing wrong in below Command. I am trying to read the sql reader during the debug and most of the time it return no value and this go out of loop</p>
  
  <h1>System defined variables</h1>
  
  <pre><code>[string] $Server= $sStrSQLName
  [string] $Database = "master"
  $SQLQuery= @"
  DECLARE @sNumCountAvailMed INT
  IF EXISTS (SELECT 1 FROM sys.database_permissions WHERE OBJECT_NAME(major_id) = 'xp_availablemedia' AND [type] = 'EX')
  BEGIN
  REVOKE EXECUTE ON xp_availablemedia TO PUBLIC;
  END
  SELECT @sNumCountAvailMed = count(*) FROM sys.database_permissions WHERE OBJECT_NAME(major_id) = 'xp_availablemedia' AND [type] = 'EX'
  SELECT @sNumCountAvailMed
  "@
  
  
      try
      {
          $Connection = New-Object System.Data.SQLClient.SQLConnection
          $Connection.ConnectionString = "server='$Server';database='$Database';trusted_connection=true;"
          $Connection.Open()
          $Command = New-Object System.Data.SQLClient.SQLCommand
          $Command.Connection = $Connection
          $Command.CommandText = $SQLQuery
          $Reader = $Command.ExecuteReader()
          Write-Host $Reader.VisibleFieldCount       
          while ($Reader.Read()) {
               Write-Host $Reader.GetValue($1)
          }
          $Connection.Close()
  }
  </code></pre>