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 to svangulick Cancel reply