Quick tip: Validate IP address parameter in custom functions

This is a quick tip for the situations where you are building a function with an IP address parameter. There are many ways of validating that a valid IP address are filled out, but this is by far the easiest.

function Invoke-MyFunction {
    [CmdletBinding()]
    param (
        [Parameter()]
        [ipaddress] $IPAddress
    )
    Write-Output $IPAddress
}

That’s it. Only a valid IP address are now allowed in the -IPAddress parameter.

2 comments

    1. Yes, I had forgot it myself when I needed it. So I thought I’d blog about it. Just as much for myself so I don’t forget again 😀

      Like

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s