如何在PowerShell工作流中运行Invoke-Command?
要在PowerShell工作流中运行Invoke-Command,我们需要使用InlineScript块,因为工作流中不直接支持Invoke-Command。下面的示例在不使用InlineScript块的情况下出现错误。
示例
Workflow TestInvokeCommand{
Invoke-Command -ComputerName LabMachine2k16 -ScriptBlock{
Get-Service WINRM
}
}输出-
At line:2 char:5
+ Invoke-Command -ComputerName LabMachine2k16 -ScriptBlock{
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Cannot call the 'Invoke-Command' command. Other commands from this module have been packaged as workflow activities, but this
command was specifically excluded. This is likely because the command requires an interactive Windows PowerShell session, or has
behavior not suited for workflows. To run this command anyway, place it within an inline-script (InlineScript { Invoke-Command })
where it will be invoked in isolation.
+ CategoryInfo : ParserError: (:) [], ParseException
+ FullyQualifiedErrorId : CommandActivityExcluded上面的错误表明Invoke-Command不能直接与PowerShell一起使用,我们需要在InlineScript块内使用它,如下所示。
Workflow TestInvokeCommand{
InlineScript{
Invoke-Command -ComputerName LabMachine2k16 -ScriptBlock{
Get-Service WINRM
}
}
}
TestInvokeCommand要在InlineScript块内的scriptblock中使用外部变量,我们需要使用$Using:VariableName。例如,
示例
Workflow TestInvokeCommand{
$server = "Labmachine2k16"
InlineScript{
Invoke-Command -ComputerName $using:Server -ScriptBlock{
Get-Service WINRM
}
}
}
TestInvokeCommand要存储输出变量,
输出结果
Workflow TestInvokeCommand{
$server = "Labmachine2k16"
$Status = InlineScript{
Invoke-Command -ComputerName $using:Server -ScriptBlock{
return( Get-Service WINRM).Status
}
}
Write-Output "WinRM Status: $status"
}
TestInvokeCommand热门推荐
10 新人红包祝福语简短精辟
11 幼师给毕业祝福语简短
12 生日高考祝福语妹妹简短
13 下飞机祝福语简短英文
14 元宵发客户祝福语简短
15 爸妈金婚文案祝福语简短
16 新郎朋友红包祝福语简短
17 虎年五一祝福语大全简短
18 出阁宴祝福语姐姐简短