![]() |
![]() | ![]() | ![]() | ![]() | ![]() | ![]() | ![]() |
|
Welcome to Vista Forums we are your forum to discuss Windows Vista x64 and x86 systems. Whether you need help or just want to post an idea you have on Vista, this is the forum for you.
br> br> |
| |||||||
![]() |
| | Thread Tools | Display Modes |
| | #1 (permalink) |
| Guest | formatting AD output I have the standard ad search script... $objDomain = New-Object System.DirectoryServices.DirectoryEntry $objSearcher = New-Object System.DirectoryServices.DirectorySearcher $objSearcher.SearchRoot = $objDomain #$objSearcher.PageSize = 10000 $objSearcher.Filter = "(&(objectCategory=Computer) (operatingSystem=*server*))" $colProplist = "name","operatingsystemservicepack","Created" foreach ($i in $colPropList){$objSearcher.PropertiesToLoad.Add($i)} $colResults = $objSearcher.FindAll() foreach ($objResult in $colResults) # {Write $ObjResult.Properties} {$objItem = $objResult.Properties; $objItem.created; $objItem.name; $ObjItem.operatingsystemservicepack}# | export-csv "C:\servers.csv"} the output gives me %Servername% Service Pack 1 %Servername% Service Pack 2 How can I alter the output to %Servername% , Service Pack 1 I have tried adding things along the line of..... + "," + but does not seem to work. Many thanks Kev |
My System Specs![]() |
| | #2 (permalink) | ||||||||||||
| Guest | Re: formatting AD output On Jan 31, 11:04*am, Kev T <K...@xxxxxx> wrote:
-aleksandar http://powershellers.blogspot.com | ||||||||||||
My System Specs![]() | |||||||||||||
| | #3 (permalink) | ||||||||||||
| Guest | Re: formatting AD output Try: (...) $colResults | foreach { $objItem = $_.Properties $obj = new-object psobject add-member -inp $obj noteproperty name $($objItem.name) add-member -inp $obj noteproperty operatingsystemservicepack $($objItem.operatingsystemservicepack) $obj } | export-csv C:\servers.csv -noType ----- Shay Levi $cript Fanatic http://scriptolog.blogspot.com
| ||||||||||||
My System Specs![]() | |||||||||||||
| | #4 (permalink) | ||||||||||||||||||||||||
| Guest | Re: formatting AD output Shay, superb thank you, just what i was looking for. Just google'ing why it works now... ![]() Alexandiar gives me a undefined error just using "," thanks again guys Kev "Shay Levi" wrote:
| ||||||||||||||||||||||||
My System Specs![]() | |||||||||||||||||||||||||
| | #5 (permalink) | ||||||||||||||||||||||||||||||||||||
| Guest | Re: formatting AD output Hi Shay, I have a similar problem as Kev howerver I'm passing arguments from the commandline so I can use the AD search script as a function in my profile. I have got as far as finding a quick fix that transposes the results to coloumns but i can only get it to work with two arguments as the hash table is (2*2). I need it to work with x number of arguments or at least 3*3. Please could you show me (or point me in the right direction) to modify the following already plagerised snippet. CODE TO TRANSPOSE OUTPUT : $list = 1,one,2,two,3,three $hash = @{} ; while ($list) { $key, $value, $list = $list; $hash[$key]=$value } gives output 1 one 2 two 3 three 4 four FUNCTION : Function adsmore { $argument0 = $args[0] $argument1 = $args[1] $argument2 = $args[2] $argument3 = $args[3] $argument4 = $args[4] $dom = [System.DirectoryServices.ActiveDirectory.Domain]::getcurrentdomain().name #Create New object to search AD $domain = New-Object DirectoryServices.DirectoryEntry #Set Search $search = [System.DirectoryServices.DirectorySearcher]$domain #Choose Search Filter objectClass "(&(objectCategory=Computer) (operatingSystem=*Server*))" = servers or replace (objectCategory=User),Object=Group .. Etc #So first is the Object type or class USER,GROUP,COMPUTER etc, the second is the attribute within that objectclass. $search.Filter = "(&(objectClass=$argument0)($argument1=$argument2)($argument3=$argument4))" #Go Get it $user = $search.Findall() foreach($users in $user){ $Objitem = $users.properties; $objItem.name; $objitem.$argument1; $objitem.$argument2; $objitem.$argument3; $objitem.$argument4} } "Kev T" wrote:
| ||||||||||||||||||||||||||||||||||||
My System Specs![]() | |||||||||||||||||||||||||||||||||||||
| | #6 (permalink) | ||||||||||||||||||||||||||||||||||||
| Guest | Re: formatting AD output Hi Kev Doe's this helps? #### test.ps1 ### $filter="(&(objectClass={0})" -f $args[0] for($i=1; $i -lt $args.count; $i+=2){ $filter+="({0}={1})" -f $args[$i],$args[$i+1] } $filter ################# run the script: PS > .\args.ps1 "arg1" "one" 1 "two" 2 "three" 3 (&(objectClass=arg1)(one=1)(two=2)(three=3) ----- Shay Levi $cript Fanatic http://scriptolog.blogspot.com
| ||||||||||||||||||||||||||||||||||||
My System Specs![]() | |||||||||||||||||||||||||||||||||||||
| | #7 (permalink) | ||||||||||||||||||||||||||||||||||||||||||||||||
| Guest | Re: formatting AD output The closing LDAP parenthesis is missing ![]() $filter="(&(objectClass={0})" -f $args[0] for($i=1; $i -lt $args.count; $i+=2){ $filter+="({0}={1})" -f $args[$i],$args[$i+1] } $filter+=")" $filter ----- Shay Levi $cript Fanatic http://scriptolog.blogspot.com
PS>>
| ||||||||||||||||||||||||||||||||||||||||||||||||
My System Specs![]() | |||||||||||||||||||||||||||||||||||||||||||||||||
| | #8 (permalink) | ||||||||||||||||||||||||||||||||||||||||||||||||
| Guest | Re: formatting AD output Hi Shay, Sorry misunderstanding - I need the OUTPUT re formatted from a list to columns..... I had already got the arguments working correctly within the LDAP query however your way is much better if the LDAP can handle grabbing the args directly from the command line - I had problems hence the $argument0 = $arg[0] polava! What I'm after is the outputted list of args to be reformatted to produce a table of columns. - note for each AD Object returned there are 3 properties that echo out, currently the default output is a list - I would like to transpose them. The following will transpose an object with two properties returned but not three - or more - hence the post! $list = blah blah $hash = @{} ; while ($list) { $key, $value, $list = $list; $hash[$key]=$value } EG ..\adsmore.ps1 arg1 arg2 arg3 arg4 arg5 gives this output in the shell $objItem.name $objitem.$argument2 $objitem.$argument4 $objItem.name $objitem.$argument2 $objitem.$argument4 $objItem.name $objitem.$argument2 $objitem.$argument4 etc I need the output to then be transposed to $objItem.name $objitem.$argument2 $objitem.$argument4 $objItem.name $objitem.$argument2 $objitem.$argument4 $objItem.name $objitem.$argument2 $objitem.$argument4 $objItem.name $objitem.$argument2 $objitem.$argument4 $objItem.name $objitem.$argument2 $objitem.$argument4 etc "Shay Levi" wrote:
| ||||||||||||||||||||||||||||||||||||||||||||||||
My System Specs![]() | |||||||||||||||||||||||||||||||||||||||||||||||||
| | #9 (permalink) | |||||||
| Guest | Re: formatting AD output If I get you now ![]() You can build a custom object and the send it to format-table $obj = new-object psobject add-member -inp $obj noteproperty name $objItem.name add-member -inp $obj noteproperty argument2 $objItem.$argument2 add-member -inp $obj noteproperty argument4 $objItem.$argument4 $obj It should be formatted to the console as: name argument2 argument4 ----- ---------- ---------- val val val val val val val val val (...) If I didn't follow you this time too, plz post your complete script. ----- Shay Levi $cript Fanatic http://scriptolog.blogspot.com
| |||||||