簡體   English   中英

將參數傳遞給 Workflow (powershell)

[英]Pass argument to Workflow (powershell)

我知道您不能在工作流中使用 Read-Host。 但我需要通過 Read-Host 提示用戶,然后在工作流中使用該輸入數據。 我們需要在遠程機器上執行 MSG.EXE(因為 NetSend 再見了)

我有這個代碼。 它運行沒有錯誤,但它不發送任何消息。 我如何將我的參數傳遞到工作流中以使其有效?

$Computers = @("mg2014","cclab2","MG11751","mg10462","mg11768","mg11786","mg11741","mg13244","mg13434","mg14464", "mg10257")

workflow Test-WFConnection {
    param(

         [Parameter (Mandatory = $true)]
        [object[]]$message
    )

foreach -parallel ($computer in $computers) {
if (Test-Connection -ComputerName $computer -Count 1 -ErrorAction SilentlyContinue) {msg /SERVER:$computer * $msg}

  }
}

$msg = Read-host "Enter your message here"
Test-WFConnection -message $msg

問題是 $msg 永遠不會被發送到任何機器。

要處理工作流,您必須了解工作流中的范圍。 在這里, $msg的值與范圍無關,這就是為什么該值根本沒有進入塊的原因。 所以你必須使用$Using:msg ,然后它將可用。

我將在下面舉一個小例子來澄清你的疑惑:

workflow sample_test
{
    $variable1 = 5

    # Changes to variable value in an InlineScript do not affect
    # the value of the workflow variable.
    InlineScript {$variable1 = $Using:variable1 + 1; "Inline Variable1 = $variable1"}
    "Workflow variable1 = $variable1"
    # To change the value in workflow scope, return the new value.
     $a = InlineScript {$variable1 = $Using:variable+1; $variable1}
     "Workflow New variable1 = $variable1"
}

下面是截圖供您參考:

在此處輸入圖片說明

希望能幫助到你...!!!

你如何聲明工作流中的計算機?

workflow send-message {
param(
  [Parameter (Mandatory = $true)]
  [string]$message
)

$computers = "mg2014","cclab2","MG11751","mg10462","mg11768","mg11786","mg11741","mg13244","mg13434","mg14464", "mg10257"
foreach -parallel ($computer in $computers){
if (Test-Connection -ComputerName $computer -Count 1 -ErrorAction SilentlyContinue){
  msg /SERVER:$computer * $message
}}}

send-message -message (read-host 'Enter message')

當我測試它時,這工作正常。 如此處發布: https : //community.spiceworks.com/topic/1951356-pass-argument-to-workflow

您可以像這樣將計算機作為參數傳遞,或者是否有任何限制阻止您這樣做:

$Computers = @("mg2014","cclab2","MG11751","mg10462","mg11768","mg11786","mg11741","mg13244","mg13434","mg14464", "mg10257")

workflow Test-WFConnection {
    param(

         [Parameter (Mandatory = $true)]
        [object[]]$message,[object[]]$computers
    )

foreach -parallel ($computer in $computers) {
if (Test-Connection -ComputerName $computer -Count 1 -ErrorAction SilentlyContinue) {msg /SERVER:$computer * $msg}

  }
}

$msg = Read-host "Enter your message here"
Test-WFConnection -message $msg  -computers $Computers

我不確定這些答案發生了什么 - 它們有點模糊。

Neal5 的答案是正確的答案,但沒有實際解釋原因(盡管我也建議將計算機列表作為參數添加到 Abhijith pk 的答案中)。

問題是在滿足條件時運行的語句:

if (Test-Connection -ComputerName $computer -Count 1 -ErrorAction SilentlyContinue) {
  msg /SERVER:$computer * $msg
}

您在這里使用$msg ,但是您已將參數命名為$message

param(
    [Parameter (Mandatory = $true)]
    [object[]]$message
    )

所以你的聲明應該是:

  msg /SERVER:$computer * $message

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM