powershell - Adding objects to array to create multiple columns -
i'm trying displayname of software listed in addremoveprograms each computer, add array under name of computer, export. here's have:
$computers = gc "c:\get software.txt" $csv = "c:\get software.csv" $results = @() if (test-path $csv) { remove-item $csv } foreach($computer in $computers){ #get displayname of software installed on asset $software = get-wmiobject win32reg_addremoveprograms -computername $computer | select-object -expandproperty displayname $counter = 0 while ($counter -lt $software.count){ #create psobject. loops through software , adds $results. $obj = new-object psobject add-member –inputobject $obj –membertype noteproperty -name $computer -value $software[$counter] $counter++ $results+=$obj } } $results | export-csv $csv -notypeinformation
unfortunately, output lists first computer in csv. i've tried stepping through understand it, don't understand why can't add $obj variable $results different name property (in case i'm looping , making new name based on computer name in .txt file). seems take first input , won't put new column header spit out software. below example of i'm getting , i'd instead.
output
a01234 program1 program2 program3
expected output
a01234 b05678 c09123 program1 program97 program30 program2 program98 program31 program3 program99 program32
hopefully makes sense. assistance appreciated, i'm doing wrong objects , i'm not seeing it. thanks!
this do-able, isn't quite simple think. suggest setting things hashtable keys computer names , values array of software titles each computer. find out how many titles computer software has (that's how many rows you'll have, it's how many times you'll have loop), , create loop make many objects. each object have no properties start, , add property each computer in hashtable, , value property nth item listed in hashtable computer. here's code, might make make more sense:
$computers = 'a01234','b05678','c09123' $sftwrpercmp = [ordered]@{} foreach($computer in $computers){ $sftwrpercmp.add($computer,@()) #randomly generate 7-15 strings 'programs' current computer $software = 1..$(get-random -maximum 15 -minimum 7)|%{(get-random -inputobject $("abcdefghijklmnopqrstuvwxyz0123456789" -split ''|?{$_}) -count 6) -join ''} foreach($title in $software){ $sftwrpercmp["$computer"] += $title } } $maxtitlecount = $sftwrpercmp.values|%{$_.count}|sort|select -last 1 $results = @() for($i=0;$i -lt $maxtitlecount;$i++){ $record = new-object psobject $sftwrpercmp.keys | foreach{add-member -inputobject $record -notepropertyname $_ -notepropertyvalue $sftwrpercmp["$_"][$i]} $results += $record clear-variable record } $results|format-table -autosize
now not randomly generate software titles, you'll use existing $software = get-wmiobject
line that's in existing code instead since part @ least working you. should result in listings looking for. code resulted in this:
a01234 b05678 c09123 ------ ------ ------ cu7k5e 6gjwob 97h1ty 7vcz5t cipwvk 760nku chkpy0 j4b7d0 1qosd3 2yefr4 2vy6dm o68sku vi7zqg wljqn9 q5vjaz zqoknv r9kzg1 h2xzk4 s8izc4 grsmpu bizxka lavni0 tkbouc k9defu 3u7kvo jz3x4h a6gvuk 18ac5h nmi32q h14gpj 50ksz6 xu0fwc pan5tc 9wxr5u 531m04
Comments
Post a Comment