Accept both AD Computer Objects and Strings as input to a custom cmdlet

Hi Guys,

I’m trying to convert any non-AD computer object input feed into my custom cmdlet into an Ad computer object. i.e. if you pass me an AD object (from get-adcomputer etc) along the pipeline, I just process it, however if you pass a string I will get it as a AD object, then process it.

So far I have tried this…

    if ($ComputerName.GetType() -isnot [Microsoft.ActiveDirectory.Management.ADAccount])
$computername = Get-adcomputer $computername


However if I do pipe “get-adcomputer servername” to my cmdlet I get the below error

You cannot call a method on a null-valued expression.
At line:68 char:13

  •     if ($ComputerName.GetType() -isnot [Microsoft.ActiveDirectory.Management ...
  •         ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    • CategoryInfo : InvalidOperation: (:slight_smile: [], RuntimeException
    • FullyQualifiedErrorId : InvokeMethodOnNull

Can anyone see what i’m doing wrong?

You’d make separate parameters, each in its own parameter set. Like how Stop-Service does it - you’ve got one set that accepts -Name , another that accepts -InputObject . You get one or the other, but never both, and you can check $PSBoundParameters to see which one you got.

So you’d have -ComputerName and -InputObject .

Thanks Don, that’s perfect.