Calculated Property not working

This topic contains 5 replies, has 2 voices, and was last updated by  Allison Peirce 3 weeks, 1 day ago.

  • Author
    Posts
  • #75848

    Allison Peirce
    Participant

    Creating list of users in AD groups. Input is a CSV file of groups, content per line is DN, Name, GroupType, GroupScope – extracted using 'get-qadgroup'. Code builds one output file per input group. each file has a unique line for the group, and then one line for each member. First column of output file named 'Level' and the content should be "GROUP" for the first data line, and 'Member" for the remainder. Here's the problem – I'm using 'get-qadgroupmember' and performing a check with foreach{if($_.type -eq "group") and adding a line to an output text file if the member is a group = that works. My select-object with a calculated property is not working – instead of the word "Member" appearing in the first column of the output CSV, I am getting nothing – no errors either. Please review my code, and thanks.

    $seed = "Level,DN,DisplayName,ClassName,Type,LogonName"    # .csv file seed record
    # find members in security groups
    $fn = 1                                                    # counter for file names
      $csv = import-csv list.of.secgroups.csv                  # contains group list - DN, Name, Grouptype, Groupscope
        foreach ($group in $csv)  
    {
      Add-Content file$fn.secgroups.output.csv "$seed"         #prep output csv 
    # build unique first line in file for group 
      $grp = new-object psobject 
      $grp | add-member -membertype NoteProperty -name "Level" -value "GROUP" 
      $grp | Add-Member -MemberType NoteProperty -name "DN" -value $group.DN
      $grp | Add-Member -MemberType NoteProperty -Name "DisplayName" -Value $group.name
      $grp | export-csv file$fn.secgroups.output.csv -append -notype -force
    # get members from each group
      get-qadgroupmember $group.dn -sizelimit(0) -ea Continue |
          foreach{if($_.type -eq "group") {add-content output.list.txt "$file$fn $_ contains groups"} else {$_} 
        Select-Object @{Name="Level"; Expression = {"Member"}}, DN, DisplayName, Classname, Type, LogonName } `
        | export-csv file$fn.secgroups.output.csv  -delim "," -append -notype -force 
            $fn = $fn + 1 
    }
    
  • #75850

    Don Jones
    Keymaster

    So, with respect, your code is a bit of a mess, so you'll have to help me follow your intent :).

      get-qadgroupmember $group.dn -sizelimit(0) -ea Continue |
          foreach{if($_.type -eq "group") {add-content output.list.txt "$file$fn $_ contains groups"} else {$_} 
        Select-Object @{Name="Level"; Expression = {"Member"}}, DN, DisplayName, Classname, Type, LogonName } `
        | export-csv file$fn.secgroups.output.csv  -delim "," -append -notype -force 
            $fn = $fn + 1 
    

    It doesn't appear as if anything is being piped to Select-Object. Am I reading that right? The ForEach contains an If{}, and then an Else{}, and then the Select with nothing in front of it?

  • #75857

    Don Jones
    Keymaster

    And as a note, it's totally legal to space things out a bit more for readability.

    get-qadgroupmember $group.dn -sizelimit(0) -ea Continue |
    foreach{
      if($_.type -eq "group") {
        add-content output.list.txt "$file$fn $_ contains groups"
      } else {
        $_
      } 
      Select-Object @{Name="Level"; Expression = {"Member"}}, DN, DisplayName, Classname, Type, LogonName 
    } |
    export-csv file$fn.secgroups.output.csv  -delim "," -append -notype -force 
    

    That's what makes me think Select is getting nothing. I kind of expected a pipe character after the closing of the Else. But I'm not sure if that's what you intended or not?

    • #75859

      Allison Peirce
      Participant

      Thanks for the reply – I'm getting back into PowerShell after a long absence.
      The Select is getting everything except the calculated property 'Member'. My output CSV file has the properties for each user The curly brace after the select-object is the closing one from the foreach{if(.. statement – and there is also a backtick (`) after that brace.
      This worked when it was simply get-qadgroupmember | Select-object | export-csv. Once I inserted the 'foreach{if(' is when I started having issues.

  • #75862

    Don Jones
    Keymaster

    There might be something bad in your pasted code, then, because I'm not seeing the curly brackets line up the way you're describing. In any event, your custom property hash table is absolutely correct, so if it's not working, then I suspect Select-Object isn't getting what you think. As I noted, you don't have a pipe character in front of Select-Object – that's different from your simpler example. Try reformatting your code a bit and confirm that it's the way you think.

    You can use an alternate syntax:

    $obj | Add-Member -Member NoteProperty -Name Level -Value "Member"
    

    Which will add your property to whatever object is in $obj.

    • #75865

      Allison Peirce
      Participant

      Thanks for the insight – I got it to work with the following mod – moved the foreach{if( line after the select-object and before the export-csv – piped all the way through

      get-qadgroupmember $group.dn -sizelimit(0) -ea Continue |
          Select-Object @{Name="Level"; Expression = {"Member"}}, DN, DisplayName, Classname, Type, LogonName |
            foreach{
      	    if($_.type -eq "group") {
      		  add-content output.list.txt file$fn $_ contains groups"
      		} else {
      		  $_
      		}
      		} |
      export-csv file$fn.secgroups.output.csv  -delim "," -append -notype
      

You must be logged in to reply to this topic.