0

我试图从两个单独的命令中获取两个属性并将它们添加到一个变量中以便能够进一步评估。有人告诉我自定义对象可以工作...

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
    }
}
4

2 回答 2

4

哈希表用于将(唯一)键映射到值。如果您需要将不同的服务器名映射到登录名,请使用哈希表,否则使用自定义对象。无论哪种方式,您都需要正确处理数据结构。

哈希表:

$hash = @{}

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

自定义对象列表:

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

在循环中为变量赋值会在每次迭代时替换该变量中的前一个值,在循环结束后只剩下最后一个值。

于 2015-12-09T21:30:21.663 回答
0

我使用了这种方法并得到了非常干净的输出。适用于 Powershell 的 Citrix SDK 非常有趣并且有很多陷阱。

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
}
于 2015-12-10T20:20:31.670 回答