Powershell Core 6.2 Cookbook
上QQ阅读APP看书,第一时间看更新

There's more...

Did you know that ForEach-Object can also initiate method calls? If you take a look at the syntax of ForEach-Object, you will notice a second parameter set that has a MemberName parameter. It does not matter if the member you are accessing is a method or a property. Try it now by spawning 10 processes! Take care to use another process, for example gedit if you are on Linux and use Gnome Desktop:

# Foreach-Object and members
# We create 10 notepads and use the WaitForExit method on all of them
$notepads = 1..10 | ForEach-Object {Start-Process -FilePath notepad -PassThru}

# We execute the WaitForExit method with one argument, the timeout
$notepads | ForEach-Object -MemberName WaitForExit -ArgumentList 500
$notepads | ForEach-Object -MemberName Kill