【问题标题】:How to pass local variable to Invoke-Command's -ScriptBlock如何将局部变量传递给 Invoke-Command -ScriptBlock
【发布时间】:2016-03-31 17:11:00
【问题描述】:

我正在尝试从 Server-2 对 Server-1(即远程服务器)执行以下 PowerShell 脚本:

$DBServer = 'Server1' 

Invoke-Command -ComputerName $DBServer -ScriptBlock {
$status = Start-Process "C:\Program Files\iQ4bis\HaloSource\HaloSource.Run.exe" '"D:\Test\UsageTracking.iqp" /wf "Default Workflow" /e "Dev" ' -Wait -PassThru
$test2 = $status.ExitCode
if ($test2 -ne 0) 
{ 
    Throw "The command exited with error code: $test2"
}
else
{
    Write-host "Workflow executed successfully."    
}
}

问题:代码部分

"C:\Program Files\iQ4bis\HaloSource\HaloSource.Run.exe" '"D:\Test\UsageTracking.iqp" /wf "Default Workflow" /e "Dev" ' -Wait -PassThru

我想让它参数化,因为 D:\Test\UsageTracking.iqpDev 的值会因项目而异。如何使用参数传入新值?如果可能的话,这就是我想要的:

clear-host

$DBServer = 'Server1'
$v1 = 'D:\Test\UsageTracking.iqp'
$v2 = 'Dev'
$v3 = "C:\Program Files\iQ4bis\HaloSource\HaloSource.Run.exe"

Invoke-Command -ComputerName $DBServer -ScriptBlock {
param(
[string] $IQPFileDestinationLocation = $v1, [string] $ExecutionEnvironment = $v2, [string] $exe = $v3
)

$IQPFileLocation = $IQPFileDestinationLocation
$workflow = "Default Workflow"
$environment = $ExecutionEnvironment
$test = """$exe"" '""$IQPFileLocation"" /wf ""$workflow"" /e ""$environment""'"
$test

$status = Start-Process $test -Wait -PassThru

$test2 = $status.ExitCode
if ($test2 -ne 0) 
{ 
    Throw "The command exited with error code: $test2"
}
else
{
    Write-host "It went ok."    
}

} -ArgumentList $v1 ,$v2 ,$v3

当我在上面运行时,我收到以下错误消息:

    This command cannot be run due to the error: The system cannot find the file specified.
    + CategoryInfo          : InvalidOperation: (:) [Start-Process], InvalidOperationException
    + FullyQualifiedErrorId : InvalidOperationException,Microsoft.PowerShell.Commands.StartProcessCommand
    + PSComputerName        : ken-dev-bi001

The command exited with error code: 
At line:8 char:1
+ Invoke-Command -ComputerName $DBServer -ScriptBlock {
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : OperationStopped: (The command exited with error code: :String) [], RuntimeException
    + FullyQualifiedErrorId : The command exited with error code: 

任何帮助都将得到它的工作。

【问题讨论】:

    标签: powershell-3.0 remote-server invoke-command scriptblock


    【解决方案1】:

    我不会实现你的代码,但会通过一个简单的例子来解释,以便你可以相应地实现它。

    假设您需要启动一个托管在 IIS 上的网站。我已经声明了变量。在函数中,你可以为变量赋值,它应该是这样的,而不是 $args[0], 它应该是 $abc,但我已经分配了变量运行时的值。您还可以通过用逗号分隔来分配多个值,例如

    -ScriptBlock {Start-Website $args[0] $args[1]} -ArgumentList $xyz, $abc

    Code Snippet
    
       $User="UserName"
       $Password="password"
       $abc="MyWebsite"
       $xyz="MyWebsite2"
       $ComputerName = "MyComputerName"
       $pword = ConvertTo-SecureString -String $Password -AsPlainText -Force
       $credential = New-Object -TypeName "System.Management.Automation.PSCredential" -ArgumentList $User, $pword
    
       function StartIIS($abc)
       { 
         Invoke-Command -ComputerName $ComputerName -Credential $credential -ScriptBlock {Start-Website $args[0]} ArgumentList $abc
       }  
    
       StartIIS($abc)
    

    【讨论】:

      【解决方案2】:

      全部,
      所以,最后我想出了如何在与远程服务器一起使用Invoke-Command 时将局部变量传递给-ScriptBlock

      这是我使用的代码,它就像一个魅力:

      Write-Host "Workflow command was: "$HaloSourceCommandLine
      
      Invoke-Command -ComputerName $DBServer -ScriptBlock {
      param ([string] $t1 = $HaloSourceCommandLine, [string] $t2 = $HaloSourceExecutableLocation)
      
          $status = Start-Process $t2 $t1 -Wait -PassThru
          $ExitCodeInfo = $status.ExitCode
              if ($ExitCodeInfo -ne 0) 
              { 
                  Throw "The command exited with error code: $test2"
              }
              else
              {
                  Write-host "Workflow executed successfully."    
              }
      } -ArgumentList $HaloSourceCommandLine,$HaloSourceExecutableLocation
      

      如果其他人在通过Invoke-Command 对远程服务器执行-ScriptBlock 时遇到问题,希望这对其他人有所帮助

      谢谢, 惠普

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2016-05-31
        • 1970-01-01
        • 1970-01-01
        • 2023-04-03
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多