0

I'm trying trying to get two properties from two separate commands and add them to a variable to be able to further evaluate. I was told a custom object would work...

Clear-Host
Add-PSSnapin citrix* -ErrorAction SilentlyContinue

$DRSrvs = Get-XAServer drptsw00* | select -ExpandProperty servername
$hash = $null
$hash = @{}

foreach ($DR in $DRSrvs) {
    $hash = New-Object PsObject -Property @{
        servername = $DR
        Logins = (Get-XALoadEvaluator -ServerName $DR).LoadEvaluatorName
    }
}
Ansgar Wiechers
  • 193,178
  • 25
  • 254
  • 328
Empyre
  • 21
  • 1
  • 5

2 Answers2

4

A hashtable is for mapping (unique) keys to values. If you need to map different servernames to login names use a hashtable, otherwise use custom objects. Either way you need to handle the data structures correctly.

Hashtable:

$hash = @{}

foreach ($DR in $DRSrvs) {
    $hash[$DR] = (Get-XALoadEvaluator -ServerName $DR).LoadEvaluatorName
}

Custom object list:

$list = foreach ($DR in $DRSrvs) {
    New-Object PsObject -Property @{
        servername = $DR
        Logins = (Get-XALoadEvaluator -ServerName $DR).LoadEvaluatorName
    }
}

Assigning something to a variable in a loop replaces the previous value in that variable with each iteration, leaving you with just the last value after the loop finishes.

sodawillow
  • 12,497
  • 4
  • 34
  • 44
Ansgar Wiechers
  • 193,178
  • 25
  • 254
  • 328
0

I used this method and got a very clean output. Citrix SDK for Powershell if very funny and has lots of gotchas.

Clear-Host
Add-PSSnapin citrix* -ErrorAction SilentlyContinue    

$OutputData = $null
$OutputData = @()
$Srvs = Get-XAServer Srv123* | Select-Object -ExpandProperty ServerName
$object = New-Object PSObject
Add-Member -InputObject $object -MemberType NoteProperty -Name Servername -Value ""
Add-Member -InputObject $object -MemberType NoteProperty -Name LoadEval -Value ""

foreach ($Srv in $Srvs) { 
    $servername= $Srv
    $LoadEval = ((Get-XALoadEvaluator -ServerName $Srv).LoadEvaluatorName)
    $appObject = New-Object System.Object
    $appObject |
        Add-Member -MemberType NoteProperty -Name "ServerName" -Value $servername -PassThru |
        Add-Member -MemberType NoteProperty -Name "LoadEval" -Value $LoadEval
    $outputData += $appObject
}
Empyre
  • 21
  • 1
  • 5