这按设计工作。请注意我所做的微小更改。
workflow ForEachParaTest {
param([string[]]$computers)
'OS & PowerShell Version'
(Get-CimInstance -ClassName Win32_OperatingSystem).Caption
$PSVersionTable
foreach –parallel ($computer in $computers)
{
# Write current target to screen
"Processing $computer"
}
}
ForEachParaTest -Computers 'server01', 'server02', 'server03'
<#
# Results
OS & PowerShell Version
Microsoft Windows 10 Pro
Name Value
---- -----
PSRemotingProtocolVersion 2.3
BuildVersion 10.0.18362.752
PSVersion 5.1.18362.752
PSCompatibleVersions {1.0, 2.0, 3.0, 4.0, 5.0, 5.1.18362.752}
PSEdition Desktop
CLRVersion 4.0.30319.42000
WSManStackVersion 3.0
SerializationVersion 1.1.0.1
Processing server03
Processing server02
Processing server01
#>
如果我把你所有的都加回来。您可以看到,在 6 个选项中,只有 4 个会成功。
workflow ForEachParaTest {
param([string[]]$computers)
'OS & PowerShell Version'
(Get-CimInstance -ClassName Win32_OperatingSystem).Caption
$PSVersionTable
foreach –parallel ($computer in $computers)
{
# Write current target to screen
# Using PS Default
"`nProcessing $computer using PowerShell default output`n"
(InlineScript {Write-Host "`nProcessing $computer using Write-Host via inline script for output`n"})
Write-Output "`nProcessing localhost using Write-Output for output`n"
Write-Host "`nProcessing $computer using Write-Host for output`n"
Write-Information "`nProcessing $computer using Write-Information for output`n"
Write-Verbose "`nProcessing $computer using Write-Verbose for output`n"
}
}
ForEachParaTest -Computers 'server01', 'server02', 'server03'
<#
# Results
At line:20 char:5
+ Write-Host "`nProcessing $computer using Write-Host for output`n"
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Cannot call the 'Write-Host' command. Other commands from this module...
command requires an interactive Windows PowerShell session, or has be...
Write-Host }) where it will be invoked in isolation.
+ CategoryInfo : ParserError: (:) [], ParseException
+ FullyQualifiedErrorId : CommandActivityExcluded
OS & PowerShell Version
Microsoft Windows 10 Pro
Name Value
---- -----
PSRemotingProtocolVersion 2.3
BuildVersion 10.0.18362.752
PSVersion 5.1.18362.752
PSCompatibleVersions {1.0, 2.0, 3.0, 4.0, 5.0, 5.1.18362.752}
PSEdition Desktop
CLRVersion 4.0.30319.42000
WSManStackVersion 3.0
SerializationVersion 1.1.0.1
Processing server03
Processing server02
Processing server01
server01
server02
server03
server01
server02
server03
server01
server02
server03
#>
在一般实践中,应该避免 Write-Host,除非绝对需要。想想颜色,没有它就无法工作的其他格式细节。此外,正如您将注意到的,如果您运行它,“`n”也不会在工作流中被处理,在正常情况下,它会被处理。
现在,通过这种方式,我们可以更好地渲染和注释问题子项。
workflow ForEachParaTest {
param([string[]]$computers)
'OS & PowerShell Version'
(Get-CimInstance -ClassName Win32_OperatingSystem).Caption
"`n"
$PSVersionTable
"`n"
foreach –parallel ($computer in $computers)
{
"Processing $computer"
# (InlineScript {Write-Host "`nProcessing $computer using Write-Host via inline script for output`n"})
Write-Output "`nProcessing $computer using Write-Output for output`n"
# Write-Host "`nProcessing $computer using Write-Host for output`n"
Write-Information "`nProcessing $computer using Write-Information for output`n"
Write-Verbose "`nProcessing $computer using Write-Verbose for output`n"
}
}
ForEachParaTest -Computers 'server01', 'server02', 'server03'
<#
# Results
OS & PowerShell Version
Microsoft Windows 10 Pro
BuildVersion 10.0.18362.752
PSVersion 5.1.18362.752
PSCompatibleVersions {1.0, 2.0, 3.0, 4.0, 5.0, 5.1.18362.752}
PSEdition Desktop
CLRVersion 4.0.30319.42000
WSManStackVersion 3.0
SerializationVersion 1.1.0.1
Processing server03
Processing server02
Processing server01
Processing server03 using Write-Output for output
Processing server02 using Write-Output for output
Processing server01 using Write-Output for output
#>
现在,回到你原来的帖子和这个小改动:
$names = @('foo', 'bar', 'bat')
Workflow Test-Print
{
param([string[]]$names)
foreach -Parallel ($name in $names)
{
# execute some commands on #name
# (InlineScript {Write-Host "Hello $name";}) # doesn't print
Write-Output "Hello $name" # doesn't print
# Write-Host "Hello $name" # error
Write-Information "Hello $name" # doesn't print
Write-Verbose "Hello $name" # doesn't print, even with -Verbose flag
}
}
Test-Print $names
<#
# Results
Hello bat
Hello bar
Hello foo
#>
Test-Print $names -Verbose
<#
# Results
Hello bat
Hello bar
Hello foo
VERBOSE: [localhost]:Hello bat
VERBOSE: [localhost]:Hello bar
VERBOSE: [localhost]:Hello foo
#>