Invoke-Command脚本块不生成输出

时间:2015-03-31 13:37:15

标签: shell powershell citrix

我正在尝试在远程PowerShell会话中使用脚本块。这个命令正常,我得到一个关于机器状态的输出:

$SecurePassword = $ParamPassword | ConvertTo-SecureString -AsPlainText -Force  
$cred = New-Object System.Management.Automation.PSCredential `
 -ArgumentList $UserName, $SecurePassword
$ParamDomain = 'mydomain'
$ParamHostname = "myhostname"

$fullhost =  "$ParamDomain"+"\"+"$ParamHostname"  

#Get-BrokerMachine No1
if ($ParamCommand -eq 'Get-BrokerMachine'){
$s = New-PSSession -computerName $desktopbroker -credential $cred
Invoke-Command -Session $s -ScriptBlock { param( $fullhost ) ;asnp citrix.* ; Get-BrokerMachine -machinename $fullhost  } -Args $fullhost
}

我的第二次迭代,也是使用脚本块,失败了。命令Get-BrokerMachine未执行且没有输出。

#Get-BrokerMachine No2
if ($ParamCommand -eq 'Get-BrokerMachine'){
$ScriptBlock = {
    asnp citrix.* ; Get-BrokerMachine -machinename $fullhost 
};
$s = New-PSSession -computerName $desktopbroker -credential $cred
Invoke-Command -Session $s -ScriptBlock $ScriptBlock 

}

有人可以解释第二个脚本的问题吗?

1 个答案:

答案 0 :(得分:2)

您的第二个脚本中缺少的一件重要事情是您没有将$fullhost作为参数传递。在远程系统$fullhost上调用scriptblock时,$null

粗略猜测你需要做这样的事情:

#Get-BrokerMachine No2
if ($ParamCommand -eq 'Get-BrokerMachine'){
    $ScriptBlock = {
        param($host)
        asnp citrix.* ; Get-BrokerMachine -machinename $host 
    };
    $s = New-PSSession -computerName $desktopbroker -credential $cred
    Invoke-Command -Session $s -ScriptBlock $ScriptBlock -ArgumentList $fullhost
}

我将scriptblock中变量的名称更改为$host,以消除范围可能存在的模糊性。

相关问题