【发布时间】:2017-02-17 19:51:24
【问题描述】:
我正在尝试创建一个脚本,该脚本将接受输入(目前为硬编码值)并调用安装 PS 脚本并在多个服务器上运行它。我正在使用 PSSession 和 Invoke-Command(见下文)。下面运行,但什么也不做。它似乎没有调用其他脚本。除了让它实际安装之外,我还需要知道它是否成功。我是 Powershell 的新手,所以任何提示/帮助/建议都会很棒。下面被包裹在一个 ForEach 中,用 $Computer 循环服务器
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
【问题讨论】:
-
1) 您没有将
$sourceInstall变量传递给远程会话。 2)你的远程命令只是返回远程$sourceInstall变量的值,而不是执行它指向的脚本。 -
@PetSerAl 我如何让脚本运行呢?另外,如果我在 install.ps1 脚本中添加一个 return 语句,是否允许本地脚本知道错误消息?
-
取决于 PowerShell 版本。使用 v3+,您只需
& $using:sourceInstall。
标签: powershell powershell-remoting