Words: 443
Time to read: around 3 minutes
Continue reading “Finding Aliases for Parameters in PowerShell”
Going concise as possible WAS bad practice at one stage!
Words: 443
Time to read: around 3 minutes
Continue reading “Finding Aliases for Parameters in PowerShell”
You think this will take me hours? Ha! Think again.
Words: 437
Time to read: ~ 2 minutes
Script Link:Â https://github.com/shaneis/RandomScripts/blob/master/WhereParameterNameDoesNotMatchColumnName.ps1
Continue reading “Finding Parameters that do not match Column Names”
Getting Get-Help Help
The following is a recounting of an issue that I had and how I went about resolving it. No computers were harmed in the making of this post.
Ask me for one PowerShell command that everyone should know and I can answer you: Get-Help
.
Fairly descriptive name if you ask me. Today I’m focusing on using Get-Help
selectively to help me figure out why my custom function just won’t accept parameters!
We are going to need a custom test function for the audience to play along with at home, luckily Shane’s got you covered.
This is a Tactical Estimation of Shane’s Test function – aka T.E.S.T. function; very simple but all the important parts are there.
Function Test-FunctionByParameter { [cmdletbinding()] Param( [Parameter(Mandatory = $true, ValueFromPipelineByPropertyName = $true)] [string]$Parameter ) process { "Success, I'm [$Parameter]" } }
If I’ve done my maths right, and I always do my maths right (as far as you know), then this function should take input from the pipeline and output it in the string “Success, I’m …”
Get-Service -Name *sql* | Select Name -first 1 | Test-FunctionByParameter
Huh, parameter problem.
I thought this was supposed to work like this. You pipe in information, magic happens, and functions work, no?
Well, when in doubt, Get-Help
.
Before I go any further though, just so that everyone knows how to use Get-Help
, I’m going to show you one of the secret techniques for using Get-Help
.
Get-Help *help*
When you think about it, why is there even a function called help
?
As far as I’m aware it’s basically the same as Get-Help
except it automatically pipes the output to | more
so we get pages rather than a wall of text.
Is there more that we can do with Get-Help
though? Is there a way that we can return the examples only? Syntax only? Parameters only?
Is there not a way that we can do such things?!
Okay I cheated on the first one; examples are pretty easy. PowerShell actually already takes care of that for you.
Get-Help Get-Help -examples
The other two, while not laid out for you as pretty as that, are not that difficult to do. What needs to be remembered about Get-Help
is that it is a cmdlet. And what do cmdlets normally output?…
What?! No! Objects!
They normally output Objects! Wow…next time just pipe it to Get-Member
if you don’t know.
Now I first saw this done in a blog post by Adam Bertram ( blog | twitter ) but I do believe that it warrants further highlighting.
If you did pipe Get-Help
to | Get-Member
you would have seen a NoteProperty called syntax, so if we want the syntax for a cmdlet, we can specify that using:
(Get-Help Get-Help).syntax
So for parameters we need…yup .parameters
.
(Get-Help Get-Help).parameters
Hmm, not as handy as I thought it would be. What happens if we pipe that to Get-Member
(Alias gm
as I’m getting lazy here)?
(Get-Help Get-Help).parameters | gm
Let’s try that and see what we get, shall we?
(Get-Help Get-Help).parameters.parameter
And the answer comes always before you smash your screen in rage.
If we pipe the above information to Get-Member
again, we get more useful information this time (I’m not going to show it, you know how to pipe to gm by now).
I’m from a database background so can we make this pretty, all I care about is the name and the pipeline input.
(Get-Help Get-Help).parameters.parameter | Select-Object -Property name,pipelineinput
You know one of these days I should really read this help file (you should too) because half way down the results of the following code is some interesting info…
help about_pipelines
METHODS OF ACCEPTING PIPELINE INPUT
Cmdlets parameters can accept pipeline input in one of two different ways:
— ByValue: Parameters that accept input “by value” can accept piped objects
that have the same .NET type as their parameter value or objects that can be
converted to that type.For example, the Name parameter of Start-Service accepts pipeline input
by value. It can accept string objects or objects that can be converted to
strings.— ByPropertyName: Parameters that accept input “by property name” can accept piped
objects only when a property of the object has the same name as the parameter.For example, the Name parameter of Start-Service can accept objects that have
a Name property.(To list the properties of an object, pipe it to Get-Member.)
Some parameters can accept objects by value or by property name. These parameters are
designed to take input from the pipeline easily.
So that’s the problem?! The names need to match up! I can do that with Select-Object
!
All I need to do is add a custom label using @{Label='<custom label>';Expression={'<custom expression>'}}
Get-Service -Name *sql* | Select-Object -First 1 -Property @{l='Parameter';e={$_.Name}} | Test-FunctionByParameter
So now when I run a command and get the crazy…
The input object cannot be bound to any parameters for the command either because the command does not take pipeline input or the input and its properties do not match any of the parameters that take pipeline input.
I can just run:
(Get-Help <cmdlet name>).parameters.parameter | Select-Object Name,pipelineInput
And know exactly where to fix! 🙂