1

我試圖從服務器2對服務器-1(即遠程服務器)執行以下PowerShell腳本:如何通過局部變量調用命令的-ScriptBlock

$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: 

任何幫助將appriciated得到它的工作。

回答

0

全部
因此,最後我想出瞭如何將本地變量傳遞給-ScriptBlock,同時使用Invoke-Command對遠程服務器。

這裏是我使用的代碼和它的工作就像一個魅力:

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對於遠程服務器的問題,這將有利於他人, HP

0

我不會實現你的代碼,但會通過一個簡單的例子來解釋,以便你可以相應地實現它。

假設您需要啓動IIS上託管的網站。我已經聲明瞭這些變量。在函數中,你可以給變量賦值,它應該是這樣的,而不是$ args [0], 它應該是$ abc,但是我已經分配了變量運行時的值。 ü還可以通過用逗號分隔其分配多個值,像

-ScriptBlock {啓動網站的$ 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) 
相關問題