调用另一个远程脚本与多个参数不工作
问题描述:
我想创建一个脚本,将采取输入(现在的硬编码值)和调用安装PS脚本并在多个服务器上运行它。我正在使用PSSession和Invoke-Command(请参阅下文)。以下运行,但什么都不做。它似乎没有调用其他脚本。除了实际安装之外,我需要知道它是否成功。我是Powershell的新手,所以任何提示/帮助/建议都会很棒。下面是包裹在foreach循环使用$计算机服务器调用另一个远程脚本与多个参数不工作
Try
{
$session = New-PSSession -ComputerName App02 -Credential $cred
$sourceInstall = $sourceFolder + 'Install\Install.ps1'
Invoke-Command -Session $session -ScriptBlock{param($serviceName, $installFolder, $sourceFolder, $Action, $username, $password) $sourceInstall} -ArgumentList ($ServiceName, $installFolder, $sourceFolder, $Action, $username, $password)
}
Catch
{
$Filename = "Error.txt"
Write-Output "ERROR: Partial Service Deployment. See error log file(s)"
Add-Content $Filename $_.Exception.Message
}
Get-PSSession | Remove-PSSession
答
你可以使用它没有在$的PowerShell.But任何版本使用的语句传递也作为参数。
如: -
Invoke-Command -ScriptBlock
param($Name)
& $Command $Name
} -ArgumentList 'Get-Process','Notepad'
但是你必须通过定位参数使用呼叫运营商 '&'
Get-Help About_Parameters
问候时,
Kvprasoon
1)您不会将'$ sourceInstall'变量传递给远程会话。 2)你的远程命令只是返回远程'$ sourceInstall'变量的值,但不执行它指向的脚本。 – PetSerAl
@PetSerAl如何让脚本运行呢?另外,如果我在install.ps1脚本中放入return语句,是否允许本地脚本知道错误消息? – Styxtb1598
它取决于PowerShell版本。使用v3 +,您可以只使用&$ using:sourceInstall'。 – PetSerAl