Page 1 of 2

Need some assistance formatting output

Posted: Thu Feb 08, 2018 7:22 am
by notarat
Can't seem to locate the information I'm looking for that explains how to do what I want, and I could use some assistance if you don't mind.

I can generally muddle my way through PowerShell but I've run up against a problem for which I can't seem to find an answer.

I have a PowerShell script that pulls a list of computers and the applications to which they are mapped. The script pulls the information in the following format

Computer1,App1,App2,App3,App4,App5 (can be over 100 applications)
Computer2,App2,App4
Computer3,App1,App3,App5
(can be over 800 computers)

Code: Select all

$dt = @()
$dt=(get-date -UFormat "%Y%m%d")
$computers = (Get-Content c:\inputfiles\ngmclist.txt)
foreach ($computer in $computers) {
    $strCMP=GET-ADComputer $computer –Properties cn,MemberOf 
    $strGRP=($strCMP.MemberOf | Sort Name | foreach {($_.Split(','))[0].Substring(3)}) -Join ","
    $strCMP=($strCMP.cn | foreach {($_.Split('='))[0]}) -Join ","
    $strDone=$strCMP+","+$strGRP
    Add-Content c:\outputfiles\$dt"ComputersandGroupsList.txt" -value $strdone 
}
I need to change the formatting of the output so the output of the script looks like:
Computer1,App1
Computer1,App2
Computer1,App3
Computer1,App4
Computer1,App5
Computer2,App2
Computer2,App4
Computer3,App1
Computer3,App3
Computer3,App5
(can be over 4000 rows)

I can't seem to find info in any of my normal powershell forums that would allow me to format the data in this way.

Can I please get some help?

Re: Need some assistance formatting output

Posted: Thu Feb 08, 2018 9:22 am
by cody m
One possibility would be to loop through each App and print them out that way.

Code: Select all

$dt = @()
$dt=(get-date -UFormat "%Y%m%d")
$computers = (Get-Content c:\inputfiles\ngmclist.txt)
foreach ($computer in $computers) {
    $strCMP=GET-ADComputer $computer –Properties cn,MemberOf 
    $strGRP=($strCMP.MemberOf | Sort Name | foreach {($_.Split(','))[0].Substring(3)}) -Join ","
    $strCMP=($strCMP.cn | foreach {($_.Split('='))[0]}) -Join ","
    foreach($Cmp in $strCMP)
    {
        $strDone=$strCMP+","+$Cmp
        Add-Content c:\outputfiles\$dt"ComputersandGroupsList.txt" -value $strdone 
    }
}

Re: Need some assistance formatting output

Posted: Thu Feb 08, 2018 10:55 am
by notarat
Cody,

Thanks for the response.

I think there may be a hiccup in your pasted example. It produces the following output:

comp1,comp1
comp2,comp2
comp3,comp3
comp4,comp4
.
.
.
.
comp100,comp100

I'm still plugging away

Re: Need some assistance formatting output

Posted: Thu Feb 08, 2018 12:11 pm
by jvierra
The code you posted and your requested output are not in any way related.

What are "App1", "App2", etc?

The code seems to want to get the groups a computer belongs to. Some of the lines in the script do absolutely nothing. THe line producing output gets the computer name in two ways.

Re: Need some assistance formatting output

Posted: Thu Feb 08, 2018 1:12 pm
by notarat
I've managed to get to this point but, from what I am hearing from the person requesting the data, the script looks like it's pulling only Group Membership, not "nested" group membership

Code: Select all

$CMPS = @()
$dt = @()
$dt=(get-date -UFormat "%Y%m%d")
$computers = (Get-Content c:\inputfiles\ngmclist.txt)
foreach ($computer in $computers) {
    $CMPM=GET-ADComputer $computer –Properties cn,MemberOf
    $CMPN=$CMPM.Name 
    $GRP=($CMPM.MemberOf | Sort Name | foreach {($_.Split(','))[0].Substring(3)}) -Join ","+"`r`n"+$CMPN+","
    #$CMPS=($CMP.cn | foreach {($_.Split('='))[0]}) -Join ","
     $Done=$CMPN+","+$GRP
     write-host $done 
    # Add-Content C:\Outputfiles\testout.txt -Value $done
    }
Any idea why it's missing certain nested application memberships?

Re: Need some assistance formatting output

Posted: Thu Feb 08, 2018 1:20 pm
by jvierra

Re: Need some assistance formatting output

Posted: Thu Feb 08, 2018 1:25 pm
by notarat
jvierra wrote: Thu Feb 08, 2018 12:11 pm The code you posted and your requested output are not in any way related.

What are "App1", "App2", etc?

The code seems to want to get the groups a computer belongs to. Some of the lines in the script do absolutely nothing. THe line producing output gets the computer name in two ways.
Yes. Actually they are related. The script I posted (as poor as you think it is) lists the data in the original format, for example: (what I refer to as an "application packaging instance", you may refer to as the product's name...Adobe Acrobat Pro, or MS Visio, or Citrix)
Computer1,ApplicationPackagingInstance1,ApplicationPackagingInstance2,ApplicationPackagingInstance3,etc.
Computer2,ApplicationPackagingInstance1,ApplicationPackagingInstance2
Computer3,ApplicationPackagingInstance2,ApplicationPackagingInstance3
Computer4,ApplicationPackagingInstance1,ApplicationPackagingInstance3

I was looking for a way to re-format the retrieved data into the following format:

Computer1,ApplicationPackagingInstance1
Computer1,ApplicationPackagingInstance2
Computer1,ApplicationPackagingInstance3
Computer1,ApplicationPackagingInstance4
Computer1,ApplicationPackagingInstance5
Computer2,ApplicationPackagingInstance2
Computer2,ApplicationPackagingInstance4
Computer3,ApplicationPackagingInstance1
Computer3,ApplicationPackagingInstance3
Computer4,ApplicationPackagingInstance2
Computer5,ApplicationPackagingInstance4

I'm nearly there on my own, but it seems that the script is evidently not pulling "nested" group membership.

Re: Need some assistance formatting output

Posted: Thu Feb 08, 2018 1:26 pm
by notarat
jvierra wrote: Thu Feb 08, 2018 1:20 pm Get-AdGroupMember "group" -recurse

https://docs.microsoft.com/en-us/powers ... w=win10-ps
Doesn't work when there are over 1000 computers in a group IIRC

Re: Need some assistance formatting output

Posted: Thu Feb 08, 2018 1:34 pm
by jvierra
The code you posted lists only group membership and has nothing to do with applications.

This is the code you posted:

foreach ($computer in $computers) {
$strCMP=GET-ADComputer $computer –Properties cn,MemberOf
$strGRP=($strCMP.MemberOf | Sort Name | foreach {($_.Split(','))[0].Substring(3)}) -Join ","
$strCMP=($strCMP.cn | foreach {($_.Split('='))[0]}) -Join ","
$strDone=$strCMP+","+$strGRP
Add-Content c:\outputfiles\$dt"ComputersandGroupsList.txt" -value $strdone
}


Note that there is no mention of any products - only computers ad "MemberOf". Did you post the wrong script?

Re: Need some assistance formatting output

Posted: Thu Feb 08, 2018 1:39 pm
by jvierra
notarat wrote: Thu Feb 08, 2018 1:26 pm
jvierra wrote: Thu Feb 08, 2018 1:20 pm Get-AdGroupMember "group" -recurse

https://docs.microsoft.com/en-us/powers ... w=win10-ps
Doesn't work when there are over 1000 computers in a group IIRC

Get-AdGroup 'groupname' -Properties Members | select -expand Members