0
在下面的代码中,我使用$scripts
变量遍历Invoke-Command
语句中的foreach
循环。但$script
值不能正确替换,并且结果似乎是单个字符串,因为“count.sql size.sql”。如果在Invoke-Command
循环之外定义,则foreach
循环正在正确执行。如何使用PowerShell中的Invoke-Command内的foreach循环?
是否有任何特定的方式来定义foreach
循环内Invoke-Command
?
$scripts = @("count.sql", "size.sql")
$user = ""
$Password = ""
$SecurePassword = $Password | ConvertTo-SecureString -AsPlainText -Force
$cred = New-Object System.Management.Automation.PSCredential -ArgumentList $User, $SecurePassword
foreach ($server in $servers) {
Invoke-Command -ComputerName $Server -Credential $cred -ScriptBlock {
Param($server, $InputFile, $scripts, $url)
foreach ($script in $scripts) {
echo "$script"
} -ArgumentList "$server,"$scripts","$url"
}
这个脚本does not看起来完整。你似乎没有正确地调用服务器上的可变参数 – ArcSet
,因为你的编辑看起来像你的参数列表是错误的。您正在使用“在他们周围将所有变量声明为字符串。将参数列表更改为 -ArgumentList $ server,$ scripts,$ url 另外您还没有按顺序声明所有的arugments ....服务器,输入文件,脚本,URL。目前$ Scripts是= to $ inputfile – ArcSet
'-argumentList'参数也看起来被错误地放置,它当前在脚本块内 – andyb