title: Is there a way to pass MSSQL table column as parameter to powershell function?
body: <p>I have a below code in powershell where I am passing parameter is from the SQL CMS however I am willing to pass this servername from MSSQL tablename is there a way to achieve this.</p>
  
  <pre><code>import-module sqlps -DisableNameChecking  
  function Test-SQLConnection{
      param([parameter(mandatory=$true)][string[]] $Instances)
  
      $return = @()
  
      foreach($InstanceName in $Instances){
          $row = New-Object –TypeName PSObject –Prop @{'InstanceName'=$InstanceName;'StartupTime'=$null;'currenttime'=$null}
          $servernameRDPcheck= $InstanceName 
  
  
          try
  
          {
  
  
          $Isconnected= (New-Object System.Net.Sockets.TCPClient -ArgumentList $row.instancename,3389).Connected
  
  
            $insertquery="  update dba.dbo.cms_validation set [IsServerRDPpingable]=1 where servername='$instancename' and convert(varchar(10),entrydt,111)=convert(varchar(10),getdate(),111)"
  
              Invoke-SQLcmd -ServerInstance 'HostedServer' -query $insertquery  -Database dba 
  
          }
  
          catch
  
          {
  
            $insertquery="  update dba.dbo.cms_validation set [IsServerRDPpingable]=0 where servername='$instancename' and convert(varchar(10),entrydt,111)=convert(varchar(10),getdate(),111)"
  
              Invoke-SQLcmd -ServerInstance 'HostedServer' -query $insertquery  -Database dba 
  
          }
  
      }
      return $return
  }
  
  
  $servers=@((dir "SQLSERVER:\SQLRegistration\Central Management Server Group\pih-win-sptlsql\Test\").Name)
  
  $servers+="HostedServer"
  Test-SQLConnection -Instances $servers
  </code></pre>
  
  <p>so instead of passing value from CMS as above i am willing to pass it from SQL table name as below and it should be looped while all servername in that table </p>
  
  <pre><code>$servers= select servername from tablename
  </code></pre>