【问题标题】:Call a remote script from another with multiple parameters not working从另一个调用远程脚本,多个参数不起作用
【发布时间】: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


【解决方案1】:

您可以在任何版本的 PowerShell 中使用它而不使用 $Using 语句。但也可以将它作为参数传递。

例如:-

Invoke-Command -ScriptBlock
   param($Name)
   & $Command $Name
} -ArgumentList 'Get-Process','Notepad'

但是你必须在使用调用运算符'&'时传递位置参数

Get-Help About_Parameters

https://msdn.microsoft.com/en-us/powershell/reference/5.1/microsoft.powershell.core/about/about_parameters

问候,

Kvprasoon

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-11-05
    • 1970-01-01
    • 2019-01-08
    • 2023-01-31
    • 2018-06-14
    相关资源
    最近更新 更多