PowerShell – How to Test a Functions Parameters

This post if from our Windows PowerShell Scripting and Tool Making online video training library.

Interface Video Training is available for a FREE 3-Day trial, no credit card necessary. The entire Video Training Library is available for only $25 per month.

One of the first things I do when I begin coding the Function, is to test and see if my parameters are actually working the way I want them to. Most of that testing will go here in the Process block.

001-how-to-test-a-Functions-Parametersin-PowerShell

When my function is executed, PowerShell will execute whatever’s in the Begin block first.

002-begin-how-to-test-a-Functions-Parametersin-PowerShell

If I’ve piped input into my function, then the process block will execute one time for each object that I piped in.

My pipeline variable – in other words, the parameter that accepted the pipeline input – will only contain one object at a time in that scenario.

003-computername-how-to-test-a-Functions-Parametersin-PowerShell

When PowerShell has run through all of the piped in objects, it will then execute the End block once.

If I don’t pipe anything in.

004-parameters-how-to-test-a-Functions-Parametersin-PowerShell

If I just run my command using parameters, then the Begin block will execute first, the Process block will execute one time, and my pipeline variable, or parameter, will contain all of the objects that were specified to that parameter.

Once the Process is finished executing one time, End will execute one time.

Keep in mind, if things are piped in, ComputerName will only contain one thing at a time. But if information is specified to ComputerName as a parameter – such as in this use case or this use case – ComputerName could contain one item or multiple items.

005-parameters-how-to-test-a-Functions-Parameters-in-PowerShell

I need to make sure it’s capable of receiving multiple items, so I’ll change its declaration to include the two square brackets. [string[]]$computerName

006-parameters-how-to-test-a-Functions-Parameters-in-PowerShell

That means it will always be an array, even though it might only contain one item. Because it’s an array, I’ll have to enumerate through it, getting one thing out of it at a time – Foreach computer in ComputerName. Within this Foreach construct, I should have a computer variable that contains only one computer name at a time, regardless of how my function was actually called.

007-foreach-parameters-how-to-test-a-Functions-Parameters-in-PowerShell

For right now, I’ll use Write-Host to output that, so I can test it.

008-write-host-parameters-how-to-test-a-Functions-Parameters-in-PowerShell

I’ll save the script now. Put this in the root of my C drive, so it’s easy to get to, or if you’ve got a convenient folder you can store it in. We’ll call it FunctionTest.

I cannot test this by running the script. If I run the script, it will declare the function, and then it will reach the end of the script and never actually run it.

For right now, just for testing purposes, I’ll copy and paste a few of my usage examples to the clipboard and bring those here and just paste them right into the bottom of the script.

009-parameters-how-to-test-a-Functions-Parameters-in-PowerShell

010-parameters-how-to-test-a-Functions-Parameters-in-PowerShell

I need to make sure I have this computers.txt file created.

011-computers-txt-parameters-how-to-test-a-Functions-Parameters-in-PowerShell

I do have a computers.txt. Let’s just edit it real quick with notepad to make sure it contains valid computer names. We’ll put in Localhost and Ussdc and save the file.

012-localhost-parameters-how-to-test-a-Functions-Parameters-in-PowerShell

Now I can start testing this. I really only want to do one at a time, so I’ll comment out my test lines, except the first one, and I’ll just run this.

013-copy-parameters-how-to-test-a-Functions-Parameters-in-PowerShell

Let’s talk about what I’m expecting to happen. I’m specifying a single parameter, ComputerName, and a single value for it.

014-computername-parameters-how-to-test-a-Functions-Parameters-in-PowerShell

I’m expecting my script to simply display that ComputerName because right now that’s all I’ve asked it to do, and it did.

015-run-computername-parameters-how-to-test-a-Functions-Parameters-in-PowerShell

I’ll comment that one out. We’ll try the next one.

016-computername-localhost-parameters-how-to-test-a-Functions-Parameters-in-PowerShell

This time I’m asking it to display two computer names, and it did.

017-run-computername-localhost-parameters-how-to-test-a-Functions-Parameters-in-PowerShell

Great, that’s working perfectly.

Those are examples of specifying information to the parameter. Let’s try piping in some information. This should pipe in by value, ValueFromPipeline.

018-get-info-parameters-how-to-test-a-Functions-Parameters-in-PowerShell

There are two computer names in that file, so it should display both of those computer names. And we had a problem. It could not find the file because it does not exist.

019-run-error-get-info-parameters-how-to-test-a-Functions-Parameters-in-PowerShell

When you’re working here in the ISE, it’s actually focused on a particular folder. Here it’s in the root of my profile. My computers.txt file is in the root of the drive. It’s easier to specify a complete path.

020-change-directory-get-info-parameters-how-to-test-a-Functions-Parameters-in-PowerShell

 

Try this again.

And it displayed both of the names.

021-run-change-directory-get-info-parameters-how-to-test-a-Functions-Parameters-in-PowerShell

We’ll undo the (c:\).

Now I’m testing to make sure that my Alias –Host also works.

022-run-host-parameters-how-to-test-a-Functions-Parameters-in-PowerShell

And that worked as well. It displayed that name, perfect.

023-run-host-parameters-how-to-test-a-Functions-Parameters-in-PowerShell

We can do one more test. Remember, I have one more use case over here.

024-run-filter-parameters-how-to-test-a-Functions-Parameters-in-PowerShell

This is just slightly more difficult to test. Go ahead and comment that one out. We’ll paste this one in.

025-get-adcomputer-parameters-how-to-test-a-Functions-Parameters-in-PowerShell

Because this is using the ADComputer cmdlet, I need to make sure I’m first importing the Active Directory module that contains that cmdlet. Get-ADComputer –filter * | Select-Object @{n=’ComputerName’ ;e={$_.Name}} | Get-DJSInfo

026-import-activedirectory-how-to-test-a-Functions-Parameters-in-PowerShell

What I’m doing here is retrieving the computers, changing their name property to a ComputerName property, and piping it to get DJOSInfo.

027-import-activedirectory-how-to-test-a-Functions-Parameters-in-PowerShell

I don’t know how many computers I have in Active Directory, but I’m expecting this to output all of their names right now. We’ll save this and run it.

028-run-import-activedirectory-how-to-test-a-Functions-Parameters-in-PowerShell

Great, that worked perfectly. It looks like it retrieved everything. It was able to successfully turn the name property into a ComputerName property. That attached to my ComputerName parameter by property name. That put them into the ComputerName variable, and I was able to display them.

029-run-import-activedirectory-how-to-test-a-Functions-Parameters-in-PowerShell

It looks like so far, I’m doing well. My initial test looks good. I can comment these out, but I’ll leave them in the script because I’ll need them again when I start doing my main coding.

Next we’ll look at how to write the Main Code for a Function

Posted in PowerShell | Posted in , , , , , , , , , , , | Leave a comment

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code class="" title="" data-url=""> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre class="" title="" data-url=""> <span class="" title="" data-url="">