powershell - Export only the machines that meets criteria -
powershell - Export only the machines that meets criteria -
i have script works great export machines meet 1 of 3 conditions in foreach statement. right exports machines, have clean manually in excel.
#create ldap searcher object , pass in dn of domain wish query $searcher = new-object system.directoryservices.directorysearcher([adsi]"ldap://dc=ten,dc=thomsonreuters,dc=com") #pass in ceriteria searching for. #in case we're looking computers enabled , running windows 7 $searcher.filter = "(&(objectcategory=computer)(objectclass=computer)(!useraccountcontrol:1.2.840.113556.1.4.803:=2)(operatingsystem=windows 7*))" $searcher.pagesize = 100000 # populate general sheet(1) info $results = $searcher.findall() $results | foreach-object { $_.getdirectoryentry() } | select @{ n = 'cn'; e = { ($_.cn) } }, @{ n = 'distinguishedname'; e = { $_.distinguishedname } }, @{ n = 'extensionattribute7'; e = { $_.extensionattribute7 } }, @{ n = 'extensionattribute1'; e = { $_.extensionattribute1 } }, @{ n = 'newcomputername'; e = { 'filler' } } | export-csv 'c:\temp\windows7_only.csv' -notype -force $csv = import-csv -path "c:\temp\windows7_only.csv" foreach ($row in $csv) { if (($row.cn -notmatch '^u\d{7}') -and ($row.distinguishedname -like "*laptops*") -and ($row.extensionattribute7 -match '^u\d{7}$') -and ($row.cn -notmatch '\d{3}')) { $row.newcomputername = $row.extensionattribute7 + "-tpl-zz" } elseif (($row.cn -notmatch '^u\d{7}') -and ($row.distinguishedname -like "*desktops*") -and ($row.extensionattribute7 -match '^u\d{7}$') -and ($row.cn -notmatch '\d{3}')) { $row.newcomputername = $row.extensionattribute7 + "-tpd-zz" } elseif (($row.cn -notmatch '^u\d{7}') -and ($row.distinguishedname -like "*virtual*") -and ($row.extensionattribute7 -match '^u\d{7}$') -and ($row.cn -notmatch '\d{3}')) { $row.newcomputername = $row.extensionattribute7 + "-tpv-zz" } } $csv | export-csv c:\temp\fixed.csv -notypeinformation -force
try this:
$csv = $csv | { ($_.cn -notmatch '^u\d{7}') -and ($_.cn -notmatch '\d{3}') -and ($_.extensionattribute7 -match '^u\d{7}$') -and (($_.distinguishedname -like "*laptops*") -or ($_.distinguishedname -like "*desktops*") -or ($_.distinguishedname -like "*virtual*")) }
before final line. should filter out ones haven't met of criteria in foreach loop
powershell
Comments
Post a Comment