简体   繁体   English

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

[英]Invoke-Command with ScriptBlock Works on Local Server - Remotely Resultset is Empty

When trying to obtain the Citrix XenApp 6.5 server status by using the following code, a resultset is returned when run in PowerShell locally on a Zone Data Collector: 当尝试使用以下代码获取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

The same scriptblock is then executed in PowerShell with computername set to a ZDC and credentials supplied. 然后,在PowerShell中执行同一脚本块,并将计算机名设置为ZDC,并提供凭据。 No exceptions are thrown and the resultset is empty: 没有异常抛出,并且结果集为空:

$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: 

Running a Wireshark network packet capture is not very helpful because the WinRM traffic payload is encrypted. 运行Wireshark网络数据包捕获不是很有用,因为WinRM通信量有效载荷已加密。

Any ideas as to why the command within the scriptblock would work locally, but return an empty resultset without throwing exceptions? 关于为什么脚本块中的命令可以在本地运行但返回一个空结果集而不抛出异常的任何想法?

Thanks! 谢谢! ds DS

When you using the invoke-command on a remote server, and you need to call a variable from the local machine, then you need to add params to the scriptblock and argumentslist 在远程服务器上使用invoke-command时,需要从本地计算机上调用变量,然后需要将参数添加到脚本块和参数列表

So your code should look like 所以你的代码应该看起来像

$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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM