[英]how to pass parameters to Powershell script and run it on a remote computer
我试图将2个参数传递给PS脚本,该脚本将修补远程计算机上的SQL Server。 这是我使用的代码:
$Patcher = "\\remoteShareLocation\SQLpatcher.ps1"
$ver = "SQL2012"
$inst = "NEWINST"
Invoke-Command -ComputerName RMTMACHINE -credential dmn\usrname -ScriptBlock {$Patcher $ver $inst}
这会要求提供凭据,但不会运行,也不会显示任何输出。 语法有什么问题吗? 还有其他替代cmdlet吗?
如果您拥有Powershell V3或更高版本,则可以使用using
前缀将本地变量“传输”到远程主机:
Invoke-Command -ComputerName RMTMACHINE -credential $cred -ScriptBlock {$using:Patcher $using:ver $using:inst}
对于较旧的版本,请查看invoke-command的-argumentlist
参数
尝试
$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)
您需要传递凭据对象而不是Domain \\ username。
要远程运行脚本文件,请查看https://stackoverflow.com/a/10446318/4550393
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.