Don’t do that: You are checking if the path provided in the parameter is valid or not.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
function Get-Something { Param ( [Parameter(Mandatory = $true, ValueFromPipeline = $true, Position = 0)] [string[]]$ComputerName ) Process { if (-not(Test-Connection -ComputerName $_ -Count 1 -Quiet)) { Write-Error -Message 'Computer not responding' } } } |
Do that: It is better to use the ValidateScript validation attribute.
It requires less lines of code and also the error generated is generated by PowerShell and automatically translated to the language of the user running that.
1 2 3 4 5 6 |
Param ( [Parameter(Mandatory = $true, ValueFromPipeline = $true, Position = 0)] [ValidateScript({Test-Connection -ComputerName $_ -Count 1 -Quiet})] [string[]]$ComputerName ) |