使用ScriptBlock的Invoke-Command在本地服务器上运行 - 远程结果集为空

时间:2017-02-02 13:05:55

标签: citrix invoke-command xenapp scriptblock

尝试使用以下代码获取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流量负载已加密。

有关为什么scriptblock中的命令可以在本地工作但是返回空结果集而不抛出异常的任何想法?

谢谢! DS

1 个答案:

答案 0 :(得分:0)

在远程服务器上使用invoke-command并且需要从本地计算机调用变量时,需要将params添加到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