[英]PowerShell passing arguments to Invoke-Command in a foreach loop
在我已经输入$PW
变量的密码并使用它创建了正确的PSCredentials之后,为什么PowerShell要求我提供访问远程计算机的凭据? 我遇到的另一个问题是,启动进程找不到指定的FilePath(空或空)。
我猜这两个错误是由于相同的错误,这一定是因为我的变量值没有传递给foreach循环。 如您所见,我尝试通过尝试使用-argumentlist
参数来实现解决方案,但是它不起作用。 我究竟做错了什么?
function Install-Exe {
param(
[string[]]$ComputerName,
[string]$UNCexepath,
[string[]]$exeargs
)
$Admin = "domain\admin"
$PW = Read-Host "Enter Password" -AsSecureString
$cred = New-Object System.Management.Automation.PSCredential -argumentlist $Admin, $PW
foreach ($c in $ComputerName) {
Invoke-Command -ComputerName $c -Credential $cred -ArgumentList $cred,$UNCexepath {
Start-Process -Filepath $UNCexepath -Credential $cred -Verb RunAs
}
}}
您需要在Invoke-Command
脚本块中声明参数:
foreach ($c in $ComputerName) {
Invoke-Command -ComputerName $c -Credential $cred -ArgumentList $cred,$UNCexepath {
param($cred,$UNCexepath)
Start-Process -Filepath $UNCexepath -Credential $cred -Verb RunAs
}
}}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.