Powershell - OU filtering when using get-adcomputer

I am trying to create a script that generates a list of computers based on certain properties that computers may have. For example, I try to make a list of computers running Windows XP and computers running Windows 7, throw their names into a CSV file and display the final score of each of them.

Here is my code so far

import-module ActiveDirectory
$computers = get-adcomputer -Filter 'ObjectClass -eq "Computer"' -properties "OperatingSystem"
$i = 0
$j = 0
Foreach ($computer in $computers) {
    if ($computer.operatingSystem -like "Windows 7*") {
        $i++
        '"{0}","{1}","{2}"' -f $computer.Name, $computer.OperatingSystem, "$computer.DistinguishedName" | Out-file -append C:\users\admin\desktop\test.txt
        }
    elseif ($computer.OperatingSystem -like "Windows XP*") {
        $j++
        '"{0}","{1}","{2}"' -f $computer.Name, $computer.OperatingSystem, "$computer.DistinguishedName" | Out-file -append C:\users\admin\desktop\test.txt
        }
    else {
        $_
        }

}
write-host "$i Win 7"
write-host "$j Win xp"
$k = $i+$j
write-host "$k Total"

Output Example:

104 Win 7
86 Win xp
190 Total

This script works, however, I would like to do it a little better, being able to say that the OU does not peek, but I can not understand it.

If anyone has an idea of ​​how to do this, or even just to make the above code better, I would love to hear it.

Thank!

+5
source share
2

-like, , DistinguishedName. , Get-ADComputer -Filter {(DistinguishedName -notlike "*OU=evil,*")} .

- . ,

# All the computers from the evil OU:
$evilOU = $computers| ? {$_.DistinguishedName -like "*ou=evil,*"}
# All the computers but the ones from the evil OU:
$goodOU = $computers| ? {$_.DistinguishedName -notlike "*ou=evil,*"}

, -and -or -like. * ? (where-object)

# All the computers save the ones from evil and wicked OU:
$goodOU = $computers| ? {
  $_.DistinguishedName -notlike "*ou=evil,*" -and $_.DistinguishedName -notlike "*ou=wicked,*"

}

+7

, , -searchbase GET-ADCOMPUTER cmdlt. , , , , get-adcomputer cmdlt .

.

import-module ActiveDirectory
$computers = get-adcomputer -Filter * -properties "OperatingSystem" -SearchBase "OU=evil,OU=workstations,DC=cs,DC=domain,DC=net"
$i = 0
$j = 0
Foreach ($computer in $computers) {
    if ($computer.operatingSystem -like "Windows 7*") {
        $i++
        '"{0}","{1}","{2}"' -f $computer.Name, $computer.OperatingSystem, "$($computer.DistinguishedName)" | Out-file -append C:\users\admin\desktop\test.txt
        }
    elseif ($computer.OperatingSystem -like "Windows XP*") {
        $j++
        '"{0}","{1}","{2}"' -f $computer.Name, $computer.OperatingSystem, "$($computer.DistinguishedName)" | Out-file -append C:\users\admin\desktop\test.txt
        }
    else {
        $_
        }

}
write-host "$i Win 7"
write-host "$j Win xp"
$k = $i+$j
write-host "$k Total"

, , , 7 xp, . OU ds.Domain.Net.

, , ".DistinguishedName", , , $computer.DistinguishedName , , :

"$computer.DistinguishedName"

"$($computer.DistinguishedName)"

"$ (<... > )" powershell, .

+1

All Articles