Windows 服务器上的远程powershell表示参数的参数为空字符串
我在机器a上有一个powershell脚本,它使用Windows 服务器上的远程powershell表示参数的参数为空字符串,windows,powershell,azure,remote-access,Windows,Powershell,Azure,Remote Access,我在机器a上有一个powershell脚本,它使用PSSession调用机器B上的命令。机器B有一个powershell脚本,它接受4个参数。当我用4个参数作为变量(它们必须是变量)调用这个脚本时,它们作为空字符串/null传递。当我将它们作为字符串传递时(例如-Argument1“Hello”),该字符串将作为“Hello”传递,而不是作为NULL/空字符串传递 有人能告诉我为什么不能正确地传递这些信息,以及如何修复它吗 客户端上的powershell版本为5.1.17134.112。远程机器
PSSession
调用机器B上的命令。机器B有一个powershell脚本,它接受4个参数。当我用4个参数作为变量(它们必须是变量)调用这个脚本时,它们作为空字符串/null传递。当我将它们作为字符串传递时(例如-Argument1“Hello”
),该字符串将作为“Hello”传递,而不是作为NULL/空字符串传递
有人能告诉我为什么不能正确地传递这些信息,以及如何修复它吗
客户端上的powershell版本为5.1.17134.112
。远程机器使用5.1.14393.2248
。这些版本是通过运行$PSVersionTable.PSVersion
获得的
客户端正在使用Windows 10 Pro 10.0.17134。服务器使用的是Windows 2016数据中心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"
远程计算机的被调用脚本
$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
由于scriptblock中的内容与脚本的其余部分在不同的范围内,因此
$ClientArgument
变量在scriptblock中未定义。如果使用PowerShell 3或更新版本,最简单的解决方案是使用$using:
范围。否则,Invoke命令的参数列表将是必需的
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}
我建议的第一件事是删除在args上键入的[string]
。我不知道这是否是问题所在,但这并不是必须的,而且我在过去看到强制键入会做一些奇怪的事情。您没有确定参数的范围。使用$using:
作用域执行远程命令。可能重复@TheIncorrigible1您错了,OP在问题中没有这样做,使用$args[0]等。
将解决问题,就像$using
修复作用域问题一样。事后看来,我误读了他的部分问题。不管怎样,这都是一个重复的问题,每周都会在这里被问到。