如何将局部变量传递给 Invoke-Command 的 -ScriptBlock [英] How to pass local variable to Invoke-Command's -ScriptBlock

查看:81
本文介绍了如何将局部变量传递给 Invoke-Command 的 -ScriptBlock的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am trying to execute following PowerShell script from Server-2 against Server-1 (i.e. Remote server):

$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 的值会随着每个项目而改变.如何使用参数传入新值?如果可能,这就是我想要的方式:

I wanted to make it parameterized, as values for D:\Test\UsageTracking.iqp and Dev is going to change per project. How can I pass in new values using parameter? This is the how I wanted it if possible:

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

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

When I run above, I get following error message:

    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: 

任何帮助都会让它工作起来.

any help would be appriciated to get it working.

推荐答案

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

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

All,
So, finally I figured it out how to pass local variable to -ScriptBlock while using with the Invoke-Commandagainst the remote server.

Here is the code I used and it worked like a charm:

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 时遇到问题,希望这会对其有所帮助

Hopefully this will help to others if they are having issue executing -ScriptBlock against remote server via Invoke-Command

谢谢,惠普

这篇关于如何将局部变量传递给 Invoke-Command 的 -ScriptBlock的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆