locked
Export the result of script in CSV file output.. RRS feed

  • Question

  • I am using this script to add users in group and check before adding if user is part of the group or not But Now I need the output in CSV file containing the result of script.

    GroupName,GroupMembers,STATUs (Added successfully or not and if any error, get that error)

    Can anyone help me to get this done.

    Import-Module ActiveDirectory
    
    $group = Get-ADGroup "GroupName"
    
    $members = @()
    Get-ADGroupMember -Identity $group | Select-Object -ExpandProperty sAMAccountName | ForEach-Object{ $members += $_ }
    
    
    $users = Import-Csv "C:\somefile.csv"
    
    ForEach($user in $users) {
      If ($members -notcontains $user.sAMAccountName) {
        Add-ADGroupMember $group $user.sAMAccountName
    
        $members += $user.sAMAccountName
    
    
      }
    }
    

    Thursday, December 13, 2018 2:33 PM

Answers

  • try this.
    $userstoadd = (import-csv C:\user.csv).username
    $group = Get-ADGroup "GroupName"
    
    
    $result = @()
    
    foreach ( $usertoadd in $userstoadd ) 
      { $members = (get-adgroupmember $group).samaccountname
        if( $members -contains $usertoadd)
          { $status = "AlreadyAdded"
          }
        else {  Add-ADGroupMember $group $user.sAMAccountName
                $status = "addednow"
                }
        $prop = @{user = $usertoadd;
                  ADGroup = $($group.name);
                  Status = $status
                 }
       $list = new-object -typename psobject -property $prop
       $result += $list
    }
    $result | export-csv -path C:\temp\status_report.csv -notypeinformation

    Thursday, December 13, 2018 2:48 PM
  • Make sure to use recursive to get users in nested groups.

    #Import AD Module
    Import-Module ActiveDirectory
    
    #Assign Group Name
    $Group = Get-ADGroup "GroupName"
    
    #Assign Array
    $Members = @()
    
    #Get Recursive Lookup Of All users and All users in Nested Groups
    $Members=Get-ADGroupMember -Identity $Group -Recursive | Select-Object -ExpandProperty sAMAccountName
    
    #Import Users SamAccoutnNames from File
    $Users = Import-Csv "C:\somefile.csv"
    
    
    # Da Loop 
    ForEach($user in $users) {
    
        #Condition Insert into Group
      If ($members -notcontains $user.sAMAccountName){
    
            #Actually Add to Group
            Add-ADGroupMember $group $user.sAMAccountName
            #Add to Log File
            $user.sAMAccountName | OUt-File C:\temp\MyUsersAdded.txt -Append
        
      }
        #Add to Different Log File 
       $user.sAMAccountName | OUt-File C:\temp\MyUsersNotAdded.txt -Append
    
    }


    • Edited by ComputerScott Thursday, December 13, 2018 11:33 PM
    • Proposed as answer by ComputerScott Thursday, December 13, 2018 11:34 PM
    • Marked as answer by Mr. Raj Tuesday, December 18, 2018 2:16 PM
    Thursday, December 13, 2018 11:31 PM

All replies

  • try this.
    $userstoadd = (import-csv C:\user.csv).username
    $group = Get-ADGroup "GroupName"
    
    
    $result = @()
    
    foreach ( $usertoadd in $userstoadd ) 
      { $members = (get-adgroupmember $group).samaccountname
        if( $members -contains $usertoadd)
          { $status = "AlreadyAdded"
          }
        else {  Add-ADGroupMember $group $user.sAMAccountName
                $status = "addednow"
                }
        $prop = @{user = $usertoadd;
                  ADGroup = $($group.name);
                  Status = $status
                 }
       $list = new-object -typename psobject -property $prop
       $result += $list
    }
    $result | export-csv -path C:\temp\status_report.csv -notypeinformation

    Thursday, December 13, 2018 2:48 PM
  • Make sure to use recursive to get users in nested groups.

    #Import AD Module
    Import-Module ActiveDirectory
    
    #Assign Group Name
    $Group = Get-ADGroup "GroupName"
    
    #Assign Array
    $Members = @()
    
    #Get Recursive Lookup Of All users and All users in Nested Groups
    $Members=Get-ADGroupMember -Identity $Group -Recursive | Select-Object -ExpandProperty sAMAccountName
    
    #Import Users SamAccoutnNames from File
    $Users = Import-Csv "C:\somefile.csv"
    
    
    # Da Loop 
    ForEach($user in $users) {
    
        #Condition Insert into Group
      If ($members -notcontains $user.sAMAccountName){
    
            #Actually Add to Group
            Add-ADGroupMember $group $user.sAMAccountName
            #Add to Log File
            $user.sAMAccountName | OUt-File C:\temp\MyUsersAdded.txt -Append
        
      }
        #Add to Different Log File 
       $user.sAMAccountName | OUt-File C:\temp\MyUsersNotAdded.txt -Append
    
    }


    • Edited by ComputerScott Thursday, December 13, 2018 11:33 PM
    • Proposed as answer by ComputerScott Thursday, December 13, 2018 11:34 PM
    • Marked as answer by Mr. Raj Tuesday, December 18, 2018 2:16 PM
    Thursday, December 13, 2018 11:31 PM
  • Thanks both of  you.. Your solution worked as charm :)

    Tuesday, December 18, 2018 2:16 PM