Alen
Alen

Reputation: 192

Selecting value from first pipelined argument

I wrote a PowerShell script that gets some cluster information. One of the columns I need is from the first argument in the pipeline and I can't find a way to return it's value.

function Get-SQL-Clusters {
    Param([string]$server)

    $servers = Get-Content -LiteralPath "C:\temp\sql_clusters.txt"
    if ($server -ne 1) {
        $files = foreach ($box in $servers) {
            Invoke-Command -ComputerName $box {
                Get-ClusterResource | Get-ClusterParameter
            } | Where-Object {
                $_.Name -eq "Address"
            } | Format-Table PSComputerName, ClusterObject, State, Name, Value -AutoSize
        }
    } else {
        Write-Warning "'$server' is not a valid path."
    }
    return $files
}

When I run this, I get the data I need but State is blank. It's in Get-ClusterResource, but the IP, which is what I'm mostly looking for, is in Get-ClusterParameter.

Ideally I would like to return the name of the cluster, each of the alwayson names, it's IP and it's current state so I can see if the active IP is on the primary site or on the DR site.

Upvotes: 1

Views: 152

Answers (2)

Alen
Alen

Reputation: 192

The almost final code. It's not 100% complete but I get the State and IP values now and will fix the rest later. Another change I made was to stop using text files and created a hash table for my servers because of formatting problems I had with text files.

function Get-SQL-Clusters-scrap
            {
 param([string]$server)

  import-module c:\temp\sql_hashtable2.ps1
  $servers = $sql_servers.hadr

  if ($server -ne 1)
  {

   $files = ForEach ($box in $servers) {invoke-command -ComputerName $box {Get-ClusterResource | 
   foreach-object {($state) = ($_.State); $_ |
  get-clusterparameter |Where-Object {$_.Name -eq "Address"} |

    Format-Table  ClusterObject,@{Name=”State”;Expression={$state}}, Name, Value, PSComputerName -AutoSize}}}

   }           
              else 
              {Write-Warning "'$server' is not a valid path."}
              return $files
            }

Upvotes: 0

Josh E
Josh E

Reputation: 7432

Your call to Invoke-Command places the Get-ClusterResource | Get-ClusterParameter calls into its own script block {...}, then pipes the results of evaluating those expressions to the Where-Object cmd. This may not be the intended order of operations.

Project your results using the Select-Object cmdlet at intermediate places in your pipeline to give you access to the desired properties at later stages (specific syntax hasn't been checked;YMMV):

Invoke-Command -ComputerName $box { Get-ClusterResource | Select-Object -Property State, @{Name="ClusterParameter";Expression = {(Get-ClusterParameter -InputObject $_) }}| Where-Object { $_.ClusterParameter.Name -eq ...

Will produce objects like:

State | ClusterParameter
------------------------
foo     ClusterParameter.ToString()

Upvotes: 1

Related Questions