[英]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.