Search for Windows 7 and 8 Machines only with this script

MilesLogan
MilesLogan used Ask the Experts™
on
Hi ALL

Subsun and Qlemo helped me with this last night .. I want to continue with this same script and add two more features.

1. Search the entire domain for Windows 7 and 8 machines only and get the output from the script below.
2. Have the option to modify the script so I can search a particular OU only instead of the entire domain .

As always .. thank you for all the help !

function get-localusers {
 param(
 [Parameter(Mandatory=$true,valuefrompipeline=$true)]
 [string]$strComputer
 )
 
 $Select = "Name","Class","Parent" | %{  
 Invoke-Expression "@{n='$_';e={ `$_.GetType().InvokeMember('$_', 'GetProperty', `$Null, `$_, `$Null) }}"
 }
 
 If (Test-Connection $strComputer -Count 2 -Quiet){
  try{
  $computer = [ADSI]("WinNT://" + $strComputer + ",computer")
  $AdminGroup = $computer.psbase.children.find("Administrators")
  $Adminmembers= $AdminGroup.psbase.invoke("Members") | Select $Select
    foreach ($admin in $Adminmembers) {
    $admin | Select @{N="ComputerName";E={$strComputer}},@{N="Administrators";E={"$(($_.parent -SPLIT "/")[-1])\$($_.Name)"}},Class,Parent
    }
 }catch{ 
 "" | Select @{N="ComputerName";E={$strComputer}},@{N="Administrators";E={"Access Denied"}},Class,Parent
 }
 }
Else {
 "" | Select @{N="ComputerName";E={$strComputer}},@{N="Administrators";E={"Not able to Ping"}},Class,Parent
 }
}
Get-Content "C:\Powershell\Servers.txt" | get-localusers | Select ComputerName,Administrators,Class,Parent | Export-Csv "C:\Powershell\LocalAdm$((get-date).toString('MM-dd-yyyy')).csv" -NTI

Open in new window

Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Subash SundharanIT Infrastructure Architect

Commented:
To Get all Win7 & 8 computers
Get-ADComputer -f {OperatingSystem -like "Windows 8*" -or OperatingSystem -like "Windows 7*"} | %{$_.Name} | Get-localusers | Select ComputerName,Administrators,Class,Parent | Export-Csv "C:\Powershell\LocalAdm$((get-date).toString('MM-dd-yyyy')).csv" -NTI

Open in new window


Use -SearchBase parameter to search in specific OU..

Hope you have read my article which explains the same.. :-)
http://www.experts-exchange.com/Programming/Languages/Scripting/Powershell/A_12657-Active-Directory-module-PowerShell-cmdlets-Part-2.html

Author

Commented:
Hi Subsun .. where would I add this to the existing script ?


Get-ADComputer -f {OperatingSystem -like "Windows 8*" -or OperatingSystem -like "Windows 7*"} | %{$_.Name} | Get-localusers | Select ComputerName,Administrators,Class,Parent | Export-Csv "C:\Powershell\LocalAdm$((get-date).toString('MM-dd-yyyy')).csv" -NTI
IT Infrastructure Architect
Commented:
Replace last line of code (line 27)..

Author

Commented:
Sorry for the late reply .. Worked perfect 👌  .. Thanks
Subash SundharanIT Infrastructure Architect

Commented:
No problem.. :-)

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial