簡體   English   中英

使用ScriptBlock的Invoke-Command在本地服務器上有效-遠程結果集為空

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

當嘗試使用以下代碼獲取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

在遠程服務器上使用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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM