Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/powershell/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何将参数传递给Powershell脚本并在远程计算机上运行它_Powershell_Remote Execution - Fatal编程技术网

如何将参数传递给Powershell脚本并在远程计算机上运行它

如何将参数传递给Powershell脚本并在远程计算机上运行它,powershell,remote-execution,Powershell,Remote Execution,我试图将2个参数传递给PS脚本,该脚本将在远程计算机上修补SQL Server。以下是我使用的代码: $Patcher = "\\remoteShareLocation\SQLpatcher.ps1" $ver = "SQL2012" $inst = "NEWINST" Invoke-Command -ComputerName RMTMACHINE -credential dmn\usrname -ScriptBlock {$Patcher $ver $inst} 这会要求提供凭据,但不会运

我试图将2个参数传递给PS脚本,该脚本将在远程计算机上修补SQL Server。以下是我使用的代码:

$Patcher = "\\remoteShareLocation\SQLpatcher.ps1"
$ver = "SQL2012"
$inst = "NEWINST"

Invoke-Command -ComputerName RMTMACHINE -credential dmn\usrname -ScriptBlock {$Patcher $ver $inst}
这会要求提供凭据,但不会运行并显示任何输出。语法有问题吗?有其他cmdlet吗?

请尝试

$cred = Get-Credential
Invoke-Command -ComputerName RMTMACHINE -credential $cred -ScriptBlock {$Patcher $ver $inst}
或者创建一个凭证对象,如下所示:

$username = "username"
$password = "password" | ConvertTo-SecureString -asPlainText -Force
$cred = New-Object System.Management.Automation.PSCredential($username,$password)
您需要传递凭证对象,而不是域\用户名


要远程运行脚本文件,请查看

如果您有powershell V3或更高版本,您可以使用
使用
前缀将本地VAR“传输”到远程主机:

Invoke-Command -ComputerName RMTMACHINE -credential $cred -ScriptBlock {$using:Patcher $using:ver $using:inst}

对于旧版本,请查看

-argumentlist
参数以补充:如果$Patcher、$ver和$inst是命令,则必须在其前面加上“&”。例如:Invoke命令-ComputerName RMTMACHINE-credential$cred-ScriptBlock{&$using:Patcher$using:ver$using:inst}也无法使用凭据,因此尝试发送参数:$ScriptBlockContent={param($p,$v,$i)powershell.exe-ExecutionPolicy Bypass$p$v$i}调用命令-ComputerName RMTMACHINE-credential$cred-ScriptBlock$ScriptBlockContent-ArgumentList$Patcher、$ver、$inst。这也会引发错误:术语“\\remoteShareLocation\Patcher.ps1”无法识别为cmdlet、函数、脚本文件或可操作程序的名称。请检查名称的拼写,或者如果包含路径,请验证路径是否正确,然后重试。