How can I make the output of a script behave similar to Get-Process
cmdlet, where it will either return a human readable formatted output if invoked directly, or an object i.e. PSCustomObject which would be used in another script / pipeline?
Basically, the script understands its calling context.
Obviously, I can do MyScript.ps1
(dumps formatted text) or MyScript.ps1 -AsObject
but that doesn't seem to be conventional.
Also, if there's a term for this, please educate me.
CodePudding user response:
Scripts or functions may return any "object". Generally, if a script may or not return something the parameter used is -Passthru to instruct the script to return objects. For a script to generate some output, the cmdlet used is generally Write-Output.
In general a Get-*
cmdlet is about to return something that is to be used on subsequent cmdlets in the pipeline. This is not the case by example for Set-*
which may or not return objects. That's why -Passtru
parameter is more present on Set-*
so that you can continue the pipeline for any reason.