'"Break" causes a PowerShell script to fail
(GSV).Name|%{If($_ -like "*net*"){ $_ ; Break }};Pause
When I run the above line from a PowerShell window, "Netlogon" will be output.
When I run a PS1 file that contains the same line, a PowerShell window will appear and then disappear immediately, and nothing will be output.
What is wrong with that line?
Solution 1:[1]
From the about_Break help topic:
Do not use break outside of a loop,
switch, ortrapWhen
breakis used outside of a construct that directly supports it (loops,switch,trap), PowerShell looks up the call stack for an enclosing construct. If it can't find an enclosing construct, the current runspace is quietly terminated.This means that functions and scripts that inadvertently use a break outside of an enclosing construct that supports it can inadvertently terminate their callers.
Using break inside a pipeline break, such as a
ForEach-Objectscript block, not only exits the pipeline, it potentially terminates the entire runspace.
Rewrite your script to use Where-Object to filter the input, then use Select-Object -First 1 to terminate the pipeline once an object matches the filter:
(GSV).Name |Where-Object { $_ -like "*net*" } |Select-Object -First 1; Pause
In this particular case you can also use Get-Service's filtering capabilities to narrow the initial query to only matching services, voiding the need for Where-Object all together:
Get-Service -Name *net* |Select -ExpandProperty Name -First 1
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Mathias R. Jessen |
