为什么在我输入$PW
变量的密码并使用它创建了正确的PSCredentials后,PowerShell会要求我提供Credentials来访问远程计算机?我遇到的另一个问题是Start-Process找不到指定的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
}
}}
答案 0 :(得分:1)
您需要在Invoke-Command
scriptblock:
foreach ($c in $ComputerName) {
Invoke-Command -ComputerName $c -Credential $cred -ArgumentList $cred,$UNCexepath {
param($cred,$UNCexepath)
Start-Process -Filepath $UNCexepath -Credential $cred -Verb RunAs
}
}}