How to pass local variable to Invoke-Command's

2019-01-27 04:41发布

问题:

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."    
}
}

Question: Code part

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

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.

回答1:

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

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

Thanks, HP



回答2:

I will not implement your code but will explain through a simple example so that you can implement it accordingly.

Suppose you need to start a website hosted on IIS. I have declared the variables. In the function u can assign values to variables as, it should have been like this, instead of $args[0], it should have been $abc, but I have I assigned the value of variable runtime. u can also assign multiple values by separating it with a comma, like

-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)