語法
workflow <Verb-Noun>
{
...
}
例子1:
是不是覺得和function有點像呢?
關鍵字
和workflow相關的關鍵字有如下:
- Workflow
- Parallel
- Foreach –parallel
- Sequence
- InlineScript
- Checkpoint-workflow
- Suspend-workflow
例子2:
workflow paralleltest {
parallel {
Get-CimInstance –ClassName Win32_OperatingSystem
Get-Process –Name PowerShell*
Get-CimInstance –ClassName Win32_ComputerSystem
Get-Service –Name s*
}
}
然而,你會發現,每次運行paralleltest得出來的結果都不一樣!
請記住一點,workflow是建立在.NET Framework Windows Workflow Foundation(WWF)之上的,workflow的代碼是轉成XAML去運行的,如下截圖可以看到paralleltest的XAML的code
注意事項
在workflow中不再使用–ComputerName,而是使用 –PSComputerName代替。例如
workflow foreachtest {
param([string[]]$computers)
foreach –parallel ($computer in $computers){
Get-WmiObject -Class Win32_ComputerSystem -PSComputerName $computer
Get-WmiObject –Class Win32_OperatingSystem –PSComputerName $computer
}
}
可以按照以下方式運行:
foreachtest -Computers “server1″, “server2″, “server3″
但是修改以下則結果不一樣咯
workflow foreachtest2 {
param([string[]]$computers)
foreach –parallel ($computer in $computers){
sequence {
Get-WmiObject -Class Win32_ComputerSystem -PSComputerName $computer
Get-WmiObject –Class Win32_OperatingSystem –PSComputerName $computer
}
}
}
注意了,運行foreachtest "192.168.2.83","192.168.2.88"和運行foreachtest2 "192.168.2.83","192.168.2.88"的結果是不一樣的。
foreachstest"192.168.2.83","192.168.2.88"的結果是每次都會變化的哦,是順序的變化;
而foreachtest2運行的結果都是一樣的
再來看個例子:
workflow foreachpsptest {
param([string[]]$computers)
foreach –parallel ($computer in $computers){
sequence {
Get-WmiObject -Class Win32_ComputerSystem -PSComputerName $computer
Get-WmiObject –Class Win32_OperatingSystem –PSComputerName $computer
$disks = Get-WmiObject -Class Win32_LogicalDisk -Filter "DriveType = 3" –PSComputerName $computer
<span style="white-space:pre"> </span>
foreach -parallel ($disk in $disks){
sequence {
$vol = Get-WmiObject -Class Win32_Volume -Filter "DriveLetter = '$($disk.DeviceID)'" –PSComputerName $computer
Invoke-WmiMethod -Path $($vol.__PATH) -Name DefragAnalysis
}
}
}
<span style="white-space:pre"> </span>}
<span style="white-space:pre"> </span>}
例子3
在workflow中,不能直接加| format-list,如果需要加上format-list,則需要使用InlineScript
workflow foreachpitest {
param([string[]]$computers)
foreach –parallel ($computer in $computers){
InlineScript {
Get-WmiObject –Class Win32_OperatingSystem –ComputerName $using:computer |
Format-List
}
}
}