0

I am working on a PowerShell script for a customer to pull information from Servers before they are decommissioned. I have had to jump through a couple of hoops, as they have 2008 and even some 2003 servers, which don't always support the same Get-CimInstance queries, but for the most part it is working as expected. One section I have as yet been unable to fix however is pulling a list of all local groups and their members, like so:

            $localGroups = Invoke-Command -ScriptBlock {
                [ADSI]$S = "WinNT://$($env:computername)"
                $S.children.Where({$_.class -eq 'group'}) |
                    Select @{Name="Name";Expression={$_.name.value}},
                           @{Name="Members";Expression={
                                [ADSI]$group = "$($_.Parent)/$($_.Name),group"
                                $members = $Group.psbase.Invoke("Members")
                                ($members | ForEach-Object {$_.GetType().InvokeMember("Name", 'GetProperty', $null, $_, $null)}) -join ";"
                           }
                           }
                } -ComputerName $sPC | Select PSComputername,Name,Members

This works just fine on Server 2012 and above, but fails on 2008 and below. The specific error states:

Method invocation failed because [System.DirectoryServices.DirectoryEntries] doesn't contain a method named 'Where'

So the OS does not like the $S.Children.Where() call. Just curious if anyone knows of another way to format it for use with 2008 and earlier. I could probably go the old WMI call route, but that takes forever to finish.

JLogan3o13
  • 103
  • 7

1 Answers1

0

I ended up finding a (fairly) quick way of doing it, works on all OS flavors:

function get-groups {
   param ($strcomputer)
   $groups = gwmi win32_group -ComputerName $strcomputer
   return $groups
}

function Get-LocalGroupMembers { 
   param( 
   [parameter(Mandatory=$true,ValueFromPipeline=$true,ValueFromPipelineByPropertyName=$true)] 
    [Alias("Name")] 
    [string]$ComputerName, 
    [string]$GroupName = "Administrators" 
   ) 

begin {} 

process { 
    $ComputerName = $ComputerName.Replace("`$", '') 
    $arr = @() 
    $wmi = Get-WmiObject -ComputerName $ComputerName -Query "SELECT * FROM Win32_GroupUser WHERE GroupComponent=`"Win32_Group.Domain='$ComputerName',Name='$GroupName'`"" 
        if ($wmi -ne $null)  { 
            foreach ($item in $wmi) { 
                $arr += ($item.PartComponent.Substring($item.PartComponent.IndexOf(',') + 1).Replace('Name=', '').Replace("`"", '')) 
            } 
        } 

    $hash = @{ComputerName=$ComputerName;Members=$arr} 
    return $hash 
} 

end{} 
}

$sPC = "Server2008R2"
$a = 1
$adapterInfo = Get-CimInstance -query "Select * From 
Win32_NetworkAdapterConfiguration Where IPenabled = 'TRUE'" -ComputerName $sPC
$serialNumber = Get-CimInstance -query "Select * From Win32_Bios" -ComputerName $sPC
$shares = Get-CimInstance -query "Select * From Win32_Share" -ComputerName $sPC

$localGroups = get-groups $sPC
$myObject = New-Object -TypeName PSObject

$myObject | Add-Member -MemberType NoteProperty -Name ComputerName -Value $adapterInfo.PSComputerName
$myObject | Add-Member -MemberType NoteProperty -Name Description -Value $adapterInfo.Description

$adapterInfo | ForEach-Object {
   $_.IPAddress |
      ForEach-Object {
         $myObject | Add-Member -MemberType NoteProperty -Name "IPAddress$($a)" -Value $_
         $a++
      }
}

$myObject | Add-Member -MemberType NoteProperty -Name SerialNumber -Value $serialNumber.SerialNumber

foreach ($share in $shares) {
   if ($share.Name -ne "C$" -and
       $share.Name -ne "D$" -and 
       $share.Name -ne "ADMIN$" -and 
       $share.Name -ne "IPC$") {
          $myObject | Add-Member -MemberType NoteProperty -Name "Share $($share.Name)" -Value $share.Path
    }   
}

foreach ($group in $localGroups) {
   $members = ((Get-LocalGroupMembers -ComputerName $sPC -GroupName $group.Name).Members) -join "; "
      if ($members.Length -ne 0) {
         $myObject | Add-Member -MemberType NoteProperty -Name "$($group.Name) Members" -Value $members
      }
}

$myObject | Export-Csv -Path "<Path  to Folder>\$($sPC).csv" -NoTypeInformation
JLogan3o13
  • 103
  • 7