如何在参数块中接受 "live" 对象或相同类型的反序列化对象?

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

我有一个处理 Active Directory 用户对象的脚本 (Microsoft.ActiveDirectory.Management.ADUser)。我在处理这些对象的函数中明确列出了类型:

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

我也希望这个函数能够从远程会话中获取对象。挑战在于从远程会话返回的对象是反序列化的:

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

   TypeName: Deserialized.Microsoft.ActiveDirectory.Management.ADUser

有没有办法让我的函数参数块接受“实时”对象或反序列化变体?我的函数不需要使用方法 - 反序列化变体具有(或可以使其具有)我需要的东西。

参数集的想法很有趣,很有帮助。查看文档后,这是我能想到的最佳选择:

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
    )

    ...

另一条评论。在查看参数集时,我不断收到有关 ADUser 的错误。但是,经过进一步挖掘,我认为该错误是因为我的测试计算机上未安装 Microsoft Active Directory PowerShell 模块。因此,未定义 'ADUser' 类型。因为我希望此脚本在不一定具有我使用上述逻辑的 ADModule 的计算机上 运行。但是,如果我能保证 ADModule 存在,那么我认为参数集是可行的方法。

很抱歉没有提供更明确的要求。我还在学习 PowerShell...

注意 - 根据 @zett42

的反馈更新