locked
Trying to parse textfile in order to read group members RRS feed

  • Question

  • Hi,

    I'm trying to use this input CSV:

    GL-APP_Xenapp,Online-Werkplek-XA.CSV
    GL-APP_MSOffice_Std,Office2010ProPlus-NL.CSV

    To create output CSV's that hold all group members who's account is enabled.

    This is the script I use:

    import-module ActiveDirectory
    $filepath = '.\license.txt'
    Import-CSV $filepath -Header Group,OutFile | Foreach-Object {
    write-output $_.Group $_.Outfile
    Start-Transcript -Path C:\_Claranet\Scripts\psout.log -Append; Get-ADGroup -Filter 'name -eq "$_.Group"' | Get-ADGroupMember -Recursive | Where { $_.objectClass -eq "user" } | Get-ADUser -properties * | where {$_.enabled -eq $true}  | where {$_.lockedout -eq $false} | where {$_.SamAccountName -ne "claratest"} | select Name -unique > $_.OutFile ;Stop-Transcript
    }

    The problem is, although the write-output statement clearly shows the correct values, $_.Group does not contain the group name. I'm clueless, frankly.

    Friday, July 14, 2017 12:49 PM

Answers

  • Why do you use Get-ADGroup -Filter 'name -eq "$_.Group"', instead can't you go with Get-ADGroupMember $_.group straight away.

    Just use $_.Group without quotes.

    Friday, July 14, 2017 12:59 PM

All replies

  • It is because by the time you get to the pipe to Get-ADGroup, the $_ object is no longer from the Import-CSV, so you need to save the results to a variable

    Actually above statement is incorrect, but saving to a variable is still needed especially for the Outfile as by the time you get down the pipe line it is not the correct object. Looking at your filter, that is setup incorrectly it should

    Get-ADGroup -Filter "name -eq '$($_.Group)'"

    Double quotes on the outside so it gets expanded, plus you need to wrap in parens to retrieve the value of the object property first.

    Import-CSV $filepath -Header Group,OutFile | Foreach-Object {
    write-output $_.Group $_.Outfile
    
      $group = $_.Group
      $outFile = $_.OutFile
    
    .......
    }

    Also try using the code snippet tool, when posting your code.


    If you find that my post has answered your question, please mark it as the answer. If you find my post to be helpful in anyway, please click vote as helpful. (99,108,97,121,109,97,110,50,64,110,121,99,97,112,46,114,114,46,99,111,109|%{[char]$_})-join''




    • Edited by clayman2 Friday, July 14, 2017 1:03 PM typo
    Friday, July 14, 2017 12:59 PM
  • Why do you use Get-ADGroup -Filter 'name -eq "$_.Group"', instead can't you go with Get-ADGroupMember $_.group straight away.

    Just use $_.Group without quotes.

    Friday, July 14, 2017 12:59 PM
  • S_K_P makes a valid point, below is a modified script with the removal of Get-ADGroup as it is not needed, and the multiple where's are not needed, this can be done via the filter, plus you should not do -Prop * unless it is truly needed, you should specify the properties you want, which in this case only LockedOut is needed as all others are default properites

    import-module ActiveDirectory
    $filepath = '.\license.txt'
    Import-CSV $filepath -Header Group,OutFile | Foreach-Object {
      $group = $_.Group
      $outFile = $_.OutFile
      write-output $group $outFile
        Get-ADGroupMember $group -Recursive | Where { $_.objectClass -eq "user" } | 
    	  Get-ADUser -Filter "Enabled -eq $true -and LockedOut -eq $false -and sAmAccountName -ne 'claratest'" -properties LockedOut | 
    	    select Name -unique > $_.OutFile
    }



    If you find that my post has answered your question, please mark it as the answer. If you find my post to be helpful in anyway, please click vote as helpful. (99,108,97,121,109,97,110,50,64,110,121,99,97,112,46,114,114,46,99,111,109|%{[char]$_})-join''



    • Edited by clayman2 Friday, July 14, 2017 1:18 PM Removed transcript part based on jrv's suggestion
    Friday, July 14, 2017 1:10 PM
  • The first thing you need to do is learn how to format and structure your code and how to post it so that it is readable.

    $filepath = '.\license.txt'
    Import-CSV $filepath -Header Group, OutFile | 
    	Foreach-Object {
    		Write-Output $_.Group $_.Outfile
    		Get-ADGroupMember $_.Group -Recursive | 
    				Where-Object { $_.objectClass -eq 'user' } | 
    				Get-ADUser -Properties LockedOut |
    				Where-Object{$_.Enabled -eq $true -and $_.LockedOut -eq $false -and $_.SamAccountName -ne 'claratest' } | 
    				Select-Object Name -unique > $_.OutFile
    	}

    Don't just blindly paste things together.

    Pasting lines together using a ';' serves no purpose other than to make the code impossible to read and debug.

    Don't embed transcript calls into the middle of a loop.


    \_(ツ)_/




    • Edited by jrv Friday, July 14, 2017 1:14 PM
    Friday, July 14, 2017 1:12 PM
  • The first thing you need to do is learn how to format and structure your code and how to post it so that it is readable.

    $filepath = '.\license.txt'
    Import-CSV $filepath -Header Group, OutFile | 
    	Foreach-Object {
    		Write-Output $_.Group $_.Outfile
    		Get-ADGroupMember $_.Group -Recursive | 
    				Where-Object { $_.objectClass -eq 'user' } | 
    				Get-ADUser -Properties LockedOut |
    				Where-Object{$_.Enabled -eq $true -and $_.LockedOut -eq $false -and $_.SamAccountName -ne 'claratest' } | 
    				Select-Object Name -unique > $_.OutFile
    	}

    Don't just blindly paste things together.

    Pasting lines together using a ';' serves no purpose other than to make the code impossible to read and debug.

    Don't embed transcript calls into the middle of a loop.


    \_(ツ)_/




    jrv,

    This part should not work > $_.Outfile because at that point of the pipeline, would't the $_ object be coming from Get-ADUser?


    If you find that my post has answered your question, please mark it as the answer. If you find my post to be helpful in anyway, please click vote as helpful. (99,108,97,121,109,97,110,50,64,110,121,99,97,112,46,114,114,46,99,111,109|%{[char]$_})-join''

    Friday, July 14, 2017 1:20 PM
  • This part should not work > $_.Outfile because at that point of the pipeline, would't the $_ object be coming from Get-ADUser?


    If you find that my post has answered your question, please mark it as the answer. If you find my post to be helpful in anyway, please click vote as helpful. (99,108,97,121,109,97,110,50,64,110,121,99,97,112,46,114,114,46,99,111,109|%{[char]$_})-join''

    Not my code.  I just did a quick fix up to demonstrate a better way of doing the format.  The code still has to be debugged and does have some other design issues.

    Yes.  The object will not exist at that point so needs to be captured at the top of the loop.

    In a formal script I would use Out-File and not the redirectors.  Also Tee-Object should replace Write-Output so we see a copy of the actual output on the screen.


    \_(ツ)_/

    Friday, July 14, 2017 1:28 PM
  • OK makes since, I just saw that you did some edits to the script, so figured you modified it completely.

    If you find that my post has answered your question, please mark it as the answer. If you find my post to be helpful in anyway, please click vote as helpful. (99,108,97,121,109,97,110,50,64,110,121,99,97,112,46,114,114,46,99,111,109|%{[char]$_})-join''

    Friday, July 14, 2017 1:32 PM