Search code examples
powershellpowershell-4.0powershell-remoting

Unable to capture workstations details with Empty group


I have the below code to capture members of local group named Remote Desktop and provide a report of total workstation and number of workstation with RDP users configured. But somehow it fails to capture the machine details if the local group Remote Desktop have no members.

I even tried if($members -eq $null) to capture workstation list but no luck. Any help will be much appreciated.

$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

Solution

  • Thanks Doug for the edit. I have managed to get the null group with the below

     $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
        }