[英]Pass argument to Workflow (powershell)
I know that you can't use Read-Host within a Workflow.我知道您不能在工作流中使用 Read-Host。 But I need to prompt the user via Read-Host and then use that input data within a Workflow.但我需要通过 Read-Host 提示用户,然后在工作流中使用该输入数据。 We need to execute MSG.EXE on remote machines (since NetSend went bye bye)我们需要在远程机器上执行 MSG.EXE(因为 NetSend 再见了)
I have this code.我有这个代码。 It runs without error but it does Not send any message.它运行没有错误,但它不发送任何消息。 how do I pass my argument into the Workflow so it works?我如何将我的参数传递到工作流中以使其有效?
$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
The issue is the $msg never gets sent to any machine.问题是 $msg 永远不会被发送到任何机器。
To deal with workflows you have to understand the scopes in workflow.要处理工作流,您必须了解工作流中的范围。 Here, the value of $msg is not with the scope thats why the value is not at all coming inside the block.在这里, $msg的值与范围无关,这就是为什么该值根本没有进入块的原因。 So you have to use $Using:msg , then it will be available.所以你必须使用$Using:msg ,然后它将可用。
I will give a small example below to clarify your doubt:我将在下面举一个小例子来澄清你的疑惑:
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"
}
Below is the screenshot for your reference:下面是截图供您参考:
Hope it helps...!!!希望能帮助到你...!!!
How about you declare the computers in the workflow?你如何声明工作流中的计算机?
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')
This worked ok when I tested it.当我测试它时,这工作正常。 As posted here: https://community.spiceworks.com/topic/1951356-pass-argument-to-workflow如此处发布: https : //community.spiceworks.com/topic/1951356-pass-argument-to-workflow
you can pass the computers as parameter like this or is there any limitation that stops you from doing that:您可以像这样将计算机作为参数传递,或者是否有任何限制阻止您这样做:
$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
I'm not certain what is happening with these answers - they're a bit obscure.我不确定这些答案发生了什么 - 它们有点模糊。
Neal5's answer was the correct answer, but without actually explaining why (although I would also suggest adding the list of computers as a parameter as in Abhijith pk's answer). Neal5 的答案是正确的答案,但没有实际解释原因(尽管我也建议将计算机列表作为参数添加到 Abhijith pk 的答案中)。
The issue is the statement that is run if the condition is met:问题是在满足条件时运行的语句:
if (Test-Connection -ComputerName $computer -Count 1 -ErrorAction SilentlyContinue) {
msg /SERVER:$computer * $msg
}
You're using $msg
here, however you have named the parameter $message
:您在这里使用$msg
,但是您已将参数命名为$message
:
param(
[Parameter (Mandatory = $true)]
[object[]]$message
)
So you're statement should read:所以你的声明应该是:
msg /SERVER:$computer * $message
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.