Search code examples
powershellpowershell-remoting

How to accept either a "live" object or a deserialized object of the same type in a param block?


I have a script that deals with Active Directory User objects (Microsoft.ActiveDirectory.Management.ADUser). I explicitly list the type in the function that processes these objects:

function Write-ADUser {
    param (
        [Microsoft.ActiveDirectory.Management.ADUser]$user
    )
(...)

I also want this function to be able to take objects from remote sessions. The challenge is that objects returned from remote sessions are of the deserialized variety:

C:\> icm -session $sess { get-aduser -identity testuser -credential $cred } | gm

   TypeName: Deserialized.Microsoft.ActiveDirectory.Management.ADUser

Is there a way to have my function param block accept either the "live" object or the deserialized variant? My function doesn't need to use methods - the deserialized variant has (or can be made to have) what I need.


Solution

  • The parameter sets idea was interesting and a helpful lead. After reviewing the documentation, this is the best option I could come up with:

    function Write-ADUser {
        [CmdletBinding()]
        param (
            [Parameter(Mandatory=$true, Position=0, ValueFromPipeline=$true)]
            [ValidateScript({
                if ($_.GetType().Name -notin @('ADUser', 'PSObject')) {
                    throw ("Error:  Invalid type ````{0}'' - expecting ADUser.") -f $_.GetType().Name
                } else {
                    $true
                }
             })]
            $user
        )
    
        ...
    

    One other comment. When looking into parameter sets I kept getting an error about ADUser. However, upon further digging I believe that error is because the Microsoft Active Directory PowerShell module isn't installed on my test computer. Therefore, the 'ADUser' type isn't defined. Because I want this script to run on computers that don't necessarily have the ADModule I am using the above logic. However, if I could guarantee that ADModule was present then I think parameter sets would be the way to go.

    Apologies for not providing clearer requirements. I'm still learning PowerShell...

    Note - updated based on feedback from @zett42