powershell - Export only the machines that meets criteria -
I have a script that works great, but I would like to export only those machines in foreach in three situations Let's complete one statement. Now it exports all the machines, which I have to manually clean in Excel.
# Create an LDAP searcher object and pass it in the domain of the domain that we want to ask $ finder = new - object system. Directory Services ([ADSI] "LDAP: // DC = Ten, DC = Thomsonar Reuters, DC = Com") # We are searching for posts in Cetitia. # In this case, we are looking for computers that are capable and running Windows 7 $ explorers. Filters = "(and (ObjectCigra = Computer) (Object Class = Computer) (! UserAccent Control: 1.2.840.113556.1.4.803: = 2) (Operating System = Windows 7 *))" $ Search. PageSize = 100000 # Normal sheet (1) $ result = $ finder with general information F Fund () $ results | {$ _ For e-objects. GetDirectoryEntry ()} | Select @ {n = 'CN'; E = {($ _ cn)}}, @ {n = 'distinguishing' name '; E = {$ _. Distinguished Name}}, @ {n = 'Extension 7'; E = {$ _. Extension-segment 7}}, @ {n = 'ext. 1'; E = {$ _. Extension used 1}}, @ {n = 'NewComputerName'; E = {'filler'}} | Export-Csv 'C: \ temp \ Windows7_Only.csv' -new type $ csv = import-csv-path "c: \ Temp \ Windows7_Only.csv" foreach (IN $ csv $ line) {if (($ line CN- NotatMatch '^ U \ D {7}') - and ($ line.defitted name-such as "* Laptop *") - and ($ lineExtensitivist 7-match 'u.de {7} $') - And ($ Row.CN -notmatch '\ d {3}')) {$ row.NewComputerName = $ row.extensionattribute7 + "-TPL-ZZ"} Otherwise (($ row.CN -notmatch '^ U \ d { 7} ') and ($ line.modified name-such as "* Desktop *") - and ($ line.execentivit 7-match' ^ u \ d {7} $ ') -and ($ line.cn -notmatch' \ D {3} ')) {$ row.NewComputer = $ Row.extensionattribute7 + "-TPD-ZZ"} Otherwise (($ line.cn -notmatch '^ u \ d {7}') -and ($ line.name as specified- "virtual *") - And $ ($ lineExtensitivative 7-match '^ U \ D {7} $') -and ($ line.cn -notmatch '\ d {3}')) {$ row NewComputerName = $ row.extensionattribute7 + "- TPV-ZZ "}} $ csv | Export-csv c: \ temp \ fixed.csv- any type of information -
< P> Try it out: $ csv = $ csv | Where {($ _. CN-Notmatch '^ U \ D {7}') - and ($ _ .nnmachch '\ d {3}') - and ($ _. Expanded pause 7- 'Dunk' U / D {7} $ ') - and (($ _. Coveted name-such as "* laptop *") -or ($ _. Specific names-like "* desktop *") -or ($ _. "))} Before the last line, it should filter any people who have not received any criteria in foreach loop .
Comments
Post a Comment