Managing output
PowerShell does not have a means of strictly enforcing the output from a script or function.
Any statement, composed of any number of commands, variables, properties, and method calls, may generate output. This output will be automatically sent to the output pipeline by PowerShell as it is generated. Unanticipated output can cause bugs in code.
The following function makes use of the StringBuilder
type. Many of the methods in StringBuilder
return the StringBuilder
instance. This is shown here:
PS> using namespace System.Text
PS> $stringBuilder = [StringBuilder]::new()
PS> $stringBuilder.AppendLine('First')
Capacity MaxCapacity Length
-------- ----------- ------
16 2147483647 7
This is useful in that it allows chaining to build up a more complex string in a single statement. The following function makes use of that chaining to build up a string:
using namespace System.Text
function Get-FirstService...