简体   繁体   English

调用命令脚本块未生成输出

[英]Invoke-Command script block not generating output

I am trying to use a script block in a remote powershell session. 我正在尝试在远程Powershell会话中使用脚本块。 This command is working and I get an output about the machine state: 此命令正在运行,我得到有关机器状态的输出:

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

My second iteration of this, also using a scriptblock, is failing. 我的第二次迭代(也使用脚本块)失败了。 The command Get-BrokerMachine is not executed and there is no output. 不执行命令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 

}

Can someone explain what is wrong with the second script? 有人可以解释第二个脚本有什么问题吗?

The one important thing that is missing in your second script is that you are not passing $fullhost as an argument. 第二个脚本中缺少的一件事是您没有传递$fullhost作为参数。 When the scriptblock gets called on the remote system $fullhost would be $null . 当在远程系统上调用$fullhost$fullhost将为$null

Rough guess would be you need to do something like this: 粗略的猜测是您需要执行以下操作:

#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
}

I changed the name of the variable inside the scriptblock to $host to remove the potential perceived ambiguity of the scopes. 我将脚本块中的变量名称更改为$host以消除潜在的范围模糊性。

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

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