0

尝试使用以下代码获取 Citrix XenApp 6.5 服务器状态时,在区域数据收集器上本地运行 PowerShell 时返回结果集:

$serverName = "SOMECITRIXSERVER"
$Invoke-Command -ScriptBlock {Add-PSSnapin Citrix.XenApp.Commands}
$serverStatus = Get-XAServer | select ServerName,@{n="InMaintenanceMode";e={ if($_.LogOnMode -like "Prohibit*"){$true}elseif($_.LogOnMode -eq "AllowLogons"){$false} }} | where {$_.ServerName -eq $serverName} | Select InMaintenanceMode}
Write-Output "Status: $serverStatus.InMaintenanceMode"
Status: false

然后在 PowerShell 中执行相同的脚本块,并将计算机名设置为 ZDC 并提供凭据。不抛出异常,结果集为空:

$zoneDataCollector = "SOMEZDCHOST"
$serverName = "SOMECITRIXSERVER"
$key = (somekeyvaluehere)
$passwordZDC = cat CredentialFile.txt | convertto-securestring -key $key
$credZDC = new-object -typename System.Management.Automation.PSCredential -argumentlist $usernameZDC, $passwordZDC
$ZDCSession = New-PSSession -ComputerName $zoneDataCollector -Credential $credZDC
$Invoke-Command -Session $ZDCSession -ScriptBlock {Add-PSSnapin Citrix.XenApp.Commands}
$serverStatus = Invoke-Command -Session $ZDCSession -ScriptBlock {Get-XAServer | select ServerName,@{n="InMaintenanceMode";e={ if($_.LogOnMode -like "Prohibit*"){$true}elseif($_.LogOnMode -eq "AllowLogons"){$false} }} | where {$_.ServerName -eq $serverName} | Select InMaintenanceMode}
Write-Output "Status: $serverStatus.InMaintenanceMode"
Status: 

运行 Wireshark 网络数据包捕获不是很有帮助,因为 WinRM 流量负载已加密。

关于为什么脚本块中的命令可以在本地工作,但返回一个空的结果集而不抛出异常的任何想法?

谢谢!ds

4

1 回答 1

0

当您在远程服务器上使用 invoke-command,并且您需要从本地机器调用变量时,您需要将参数添加到 scriptblock 和 argumentslist

所以你的代码应该看起来像

$serverStatus = Invoke-Command -Session $ZDCSession -ScriptBlock {param($serverName) Get-XAServer | select ServerName,@{n="InMaintenanceMode";e={ if($_.LogOnMode -like "Prohibit*"){$true}elseif($_.LogOnMode -eq "AllowLogons"){$false} }} | where {$_.ServerName -eq $serverName} | Select InMaintenanceMode} –argumentlist $serverName
于 2017-02-08T07:47:36.200 回答