服务器上的远程 powershell 说参数的参数是空字符串

Remote powershell on server says argument for parameter is empty string

我在机器 A 上有一个 powershell 脚本,它使用 PSSession 在机器 B 上调用一个命令。机器 B 有一个 powershell 脚本,它接受 4 个参数。当我使用 4 个参数作为变量(它们必须是)调用此脚本时,它们被传递为空 strings/null。当我将它们作为字符串传递时(例如 -Argument1 "Hello"),该字符串将作为 "Hello" 而不是 NULL/empty 字符串传递。

谁能告诉我为什么这些没有正确传递以及如何解决?

客户端的powershell版本为5.1.17134.112。远程机器使用 5.1.14393.2248。这些版本已由 运行ning $PSVersionTable.PSVersion 获得。

客户端正在使用 Windows 10 Pro 10.0.17134。服务器正在使用 Windows 2016 Datacenter 10.0.14393 并且 运行 作为 Azure 上的虚拟机。

我曾尝试使用 Script.ps1 -Argument1 $ClientArgument1 -Argument2 $ClientArgument2 ... 传递变量并使用 ArgumentList 将逗号分隔的值传递给脚本,但这两种尝试都导致无法打印。

我注意到当我使用 -Argument1 "Hello" -Argument2 $ClientArgument2 -Argument3 $ClientArgument3 -Argument4 $ClientArgument4 时,Hello 确实被打印出来了。

代码

连接远程机器的客户端

$ErrorActionPreference = "Stop"

#Create credentials to log in
$URL = 'https://url.to.server:5986'
$Username = "username"
$pass = ConvertTo-SecureString -AsPlainText 'password' -Force
$SecureString = $pass
$MySecureCreds = New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList $Username,$SecureString

$ClientArgument1 = "Argument 1"
$ClientArgument2 = "Argument 2" 
$ClientArgument3 = "Argument 3" 
$ClientArgument4 = "Argument 4" 

#Create the remote PS session
$sessionOption = New-PSSessionOption -SkipCACheck -SkipCNCheck -SkipRevocationCheck
$session = New-PSSession -ConnectionUri $URL -Credential $MySecureCreds -SessionOption $sessionOption

#Call the remote script and pass variables
Invoke-Command -Session $session -Command {C:\Path\To\Script\On\Remote\Machine\Script.ps1 -Argument1 $ClientArgument1 -Argument2 $ClientArgument2 -Argument3 $ClientArgument3 -Argument4 $ClientArgument4}

#Note: Command is used because Command allows me to execute a script that is located on disk of the remote machine

远程机器调用的脚本

param(
    [String]$Argument1,
    [String]$Argument2,
    [String]$Argument3,
    [String]$Argument4
)

Write-Host 'Results of the 4 parameters passed into this script:'

Write-Host $Argument1
Write-Host $Argument2
Write-Host $Argument3
Write-Host $Argument4

Write-Host "The results have been printed" 

预期和实际结果

预期结果:

Results of the 4 parameters passed into this script:
Argument 1
Argument 2
Argument 3
Argument 4
The results have been printed 

实际结果

Results of the 4 parameters passed into this script:




The results have been printed

非常感谢您的宝贵时间!

尝试这样执行:

Invoke-Command -Session $session -Command {

C:\Path\To\Script\On\Remote\Machine\Script.ps1 `
-Argument1 $args[0] -Argument2 $args[1] `
-Argument3 $args[2] -Argument4 $args[3]

} -ArgumentList $ClientArgument1,$ClientArgument2,$ClientArgument3,$ClientArgument4

由于脚本块内的内容与脚本的其余部分处于不同的范围内,因此 $ClientArgument 变量在脚本块内未定义。如果您使用的是 PowerShell 3 或更新版本,最简单的解决方案是使用 $using: 范围。否则将需要 Invoke-Command 的参数列表。

Invoke-Command -Session $session -Command {C:\Path\To\Script\On\Remote\Machine\Script.ps1 -Argument1 $using:ClientArgument1 -Argument2 $using:ClientArgument2 -Argument3 $using:ClientArgument3 -Argument4 $using:ClientArgument4}