【问题标题】:Invoke-Command with ScriptBlock Works on Local Server - Remotely Resultset is Empty使用 ScriptBlock 调用命令在本地服务器上工作 - 远程结果集为空
【发布时间】:2017-02-02 13:05:55
【问题描述】:

当尝试使用以下代码获取 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

【问题讨论】:

    标签: citrix invoke-command xenapp scriptblock


    【解决方案1】:

    当您在远程服务器上使用invoke-command,并且您需要从本地机器调用一个变量时,您需要将参数添加到脚本块和参数列表中

    所以你的代码应该是这样的

    $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
    

    【讨论】:

      猜你喜欢
      • 2021-06-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-01-03
      • 1970-01-01
      • 1970-01-01
      • 2022-08-22
      • 2018-11-30
      相关资源
      最近更新 更多