PowerShell ValidateScript for and array of GUIDs as an argument

Not sure if that title make sense, and I am not sure I understand this code, but it seems to work:

Function TestArrayOfGUIDsArguemnt {
PARAM(
[ValidateScript({-not @( $_ | where {$_ -notmatch("^(\{){0,1}[0-9a-fA-F]{8}\-[0-9a-fA-F]{4}\-[0-9a-fA-F]{4}\-[0-9a-fA-F]{4}\-[0-9a-fA-F]{12}(\}){0,1}$") }).Count})]$ArguemntToTest
)
write-host "Valid!"
}

Tests:

TestArrayOfGUIDsArguemnt -ArguemntToTest "Jeff"
TestArrayOfGUIDsArguemnt : Cannot validate argument on parameter 'ArguemntToTest'.

TestArrayOfGUIDsArguemnt -ArguemntToTest "9e4c1911-f42a-42d9-af05-2a3df2823197"
Valid!

TestArrayOfGUIDsArguemnt -ArguemntToTest "9e4c1911-f42a-42d9-af05-2a3df2823197","557b8323-f630-40d3-b438-b2229806c47d"
Valid!

TestArrayOfGUIDsArguemnt -ArguemntToTest "9e4c1911-f42a-42d9-af05-2a3df2823197","557b8323-f630-40d3-b438-b2229806c47d","Jeff"
TestArrayOfGUIDsArguemnt : Cannot validate argument on parameter 'ArguemntToTest'.

Hope that helps some one?

I got the idea from here

2 Responses to PowerShell ValidateScript for and array of GUIDs as an argument

  1. Magnus April 24, 2013 at 2:37 am #

    Instead of using the [ValidateScript] attribute, why not specify that the input should be an array of Guids?

    Function TestArrayOfGUIDsArgument {
    Param([Guid[]] $ArgumentToTest)
    Write-Host “Valid!”
    }

    Looks better, agree? 🙂

  2. jbmurphy April 25, 2013 at 10:11 am #

    Did not know that one! Thanks

Powered by WordPress. Designed by WooThemes