无法使用空组捕获工作站详细信息

Unable to capture workstations details with Empty group

我有下面的代码来捕获名为 远程桌面 的本地组的成员,并提供一份关于工作站总数和配置了 RDP 用户的工作站数量的报告。但是如果本地组 Remote Desktop 没有成员,它就无法捕获机器详细信息。

我什至尝试 if($members -eq $null) 来捕获工作站列表,但没有成功。任何帮助将不胜感激。

$ExportPath = 'c:\temp\Workstations.csv'
$OUData = import-csv c:\temp\workstations_OU.csv

$selectprops = @(
    'Name',
    'DNSHostName',
    'Description',
    'OperatingSystem',
    'LastLogonDate',
    'CanonicalName',
    @{N='OUPath';e={$OUpath}},
    @{N='Server';e={$Server}}
)

$OUexport = foreach($OU in $OUdata)
{
    $OUpath = $OU.ou
    $server = $OU.server
    Get-ADComputer -Filter * -Property * -SearchBase $OUpath -Server $server | 
        Select-object $selectprops | Export-Csv -NoType -Append -force $ExportPath
}

$OUData = import-csv c:\temp\Workstations.csv

$RDP = foreach($data in $OUdata)
{
    $ComputerName = $data.Name
    $Server = $data.Server

    Get-ADComputer $ComputerName -Property memberof -Server $server | Select-object memberof | %{
        if ($_.MemberOf -like '*RemoteDesktop*') {$remote_desktop = "Enabled"}  else {$remote_desktop = "Disabled"}}

    $Description = $data.description
    $DNSHostname = $Data.DNSHostName
    $LogonDate = $Data.LastLogonDate
    $Operatingsystem =  $Data.OperatingSystem

    $ping = Test-Connection -ComputerName $DNSHostname -Count 1 -ErrorAction SilentlyContinue

    if ($ping)
    {
        $ping_status = "up"
        $error.clear()

        Try
        {  
            $Group = [ADSI]("WinNT://$DNSHostname/Remote Desktop")
            $members = @($group.psbase.Invoke("Members")) 
                $Path  = $Null
                $Name = $Null
    
            if($members -eq $null)
            {
                [pscustomobject]@{
                    Workstation = $ComputerName
                    Comp_Description = $Description
                    DNS_Hostname = $DNSHostname
                    RDP_Status = $remote_desktop
                    PING_Status = $ping_status
                    LastLogonDate = $LogonDate
                    OPeratingSystem = $Operatingsystem
                    ADSPath  = $path
                    Name = $name
                }
            }
            else
            {
                $members | foreach {
                    $name = $_.GetType().InvokeMember("Name", 'GetProperty', $null, $_, $null)
                    $path = $_.GetType().InvokeMember("ADsPath", 'GetProperty', $null, $_, $null)

                    [pscustomobject]@{
                        Workstation = $ComputerName
                        Comp_Description = $Description
                        DNS_Hostname = $DNSHostname
                        RDP_Status = $remote_desktop
                        PING_Status = $ping_status
                        LastLogonDate = $LogonDate
                        OPeratingSystem = $Operatingsystem
                        ADSPath  = $path
                        Name = $name
                    }
                }
            }
        }
        Catch
        {
            [pscustomobject]@{
                Workstation = $ComputerName
                Comp_Description = $Description
                DNS_Hostname = $DNSHostname
                RDP_Status = $remote_desktop
                PING_Status = $ping_status
                LastLogonDate = $LogonDate
                OPeratingSystem = $Operatingsystem
                ADSPath  = $null
                Name = $null
            }
        }
    }
    else
    {
        $ping_status = "down"

        [pscustomobject]@{
            Workstation = $ComputerName
            Comp_Description = $Description
            DNS_Hostname = $DNSHostname
            RDP_Status = $remote_desktop
            PING_Status = $ping_status
            OperatingSystem = $Operatingsystem
            LastLogonDate = $LogonDate
            ADSPath  = $null
            Name = $null
        }
    }
}

$RDP | export-csv c:\temp\RDP.csv -NoTypeInformation -force

感谢道格的编辑。我已经设法通过以下

获得空组
 $members = @($group.psbase.Invoke("Members")) 
        $Path  = $Null
        $Name = $Null
 $member_count = ($members | measure).count
 if($member_count -eq "0")
 {
 [pscustomobject]@{
        Workstation = $ComputerName
        Comp_Description = $Description
        DNS_Hostname = $DNSHostname
        RDP_Status = $remote_desktop
        PING_Status = $ping_status
        LastLogonDate = $LogonDate
        OPeratingSystem = $Operatingsystem
        ADSPath  = $path
        Name = $name
        FullName = $fullname
 
 }
 }
 else
 {
 $members | foreach {
        $name = $_.GetType().InvokeMember("Name", 'GetProperty', $null, $_, $null)
        $path = $_.GetType().InvokeMember("ADsPath", 'GetProperty', $null, $_, $null)
[pscustomobject]@{
        Workstation = $ComputerName
        Comp_Description = $Description
        DNS_Hostname = $DNSHostname
        RDP_Status = $remote_desktop
        PING_Status = $ping_status
        LastLogonDate = $LogonDate
        OPeratingSystem = $Operatingsystem
        ADSPath  = $path
        Name = $name
        FullName = $fullname
    }