2016-12-22 69 views
0

我知道您不能在工作流程中使用Read-Host。但我需要通過Read-Host提示用戶,然後在工作流程中使用該輸入數據。我們需要在遠程機器上執行MSG.EXE(因爲NetSend去拜拜了)將參數傳遞給Workflow(PowerShell)

我有這段代碼。它運行時沒有錯誤,但它不發送任何消息。如何將我的參數傳遞到工作流程,以便它可以工作?

$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 

的問題是,$味精永遠不會被髮送到任何機器。

+0

我能問你爲什麼使用工作流,而不是隻使用'Test-Connection'的'-AsJob'參數? –

回答

0

要處理工作流程,您必須瞭解工作流程中的範圍。在這裏,$ msg的值不在範圍內,這就是爲什麼該值完全不在塊內。所以你必須使用$使用: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" 
} 

下面是供您參考截圖:

enter image description here

希望它可以幫助... !!!

+0

嗨,謝謝。我很抱歉,但我不明白我如何將您的邏輯加入到我的工作流程中。我如何/在哪裏添加這個?我試過$使用:$味精,並得到這個錯誤: – mqh7

+0

嗨,謝謝。我很抱歉,但我不明白我如何將您的邏輯加入到我的工作流程中。我如何/在哪裏添加這個?我試過$使用:$味精,並得到這個錯誤:一個使用變量不能被檢索。使用變量只能用於腳本工作流程中的Invoke-Command,Start-Job或InlineScript 那麼,我將如何在腳本中創建它?非常感謝您的幫助 – mqh7

+0

我也可以看到一個缺陷。你正在接受輸入爲$ message,但你傳遞的是$ msg。'if(Test-Connection -ComputerName $ computer -Count 1 -ErrorAction SilentlyContinue){msg/SERVER:$ computer * $ using:message}'。使用這個現在 –

0

您如何在工作流中聲明計算機?

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

0

你可以通過電腦作爲參數,這樣還是有阻止你做任何限制:

$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