locked
Adding AD User to Security Group from .txt RRS feed

  • Question

  • Hello everyone,

    im going to write a powershell-script to add AD-User to AD-Security-Group from a
    .txt-file. My file is like:

    <distinguishedNameUser1>;<distinguishedNameGroup1>
    <distinguishedNameUser2>;<distinguishedNameGroup2>
    <distinguishedNameUser3>;<distinguishedNameGroup3>
    ...

    Could be a for-each loop the solution to master this?
    I know the commandlet to use but the question for me is how to iterate through the pair of variables and call the commandlet with them.

    Any help is greatly appreciated
    Vollstracker
    • Edited by Vollstracker Wednesday, November 4, 2015 9:57 PM style
    Wednesday, November 4, 2015 9:54 PM

Answers

  • Import-Csv file.txt -Head user,group -delim ';'|
         ForEach-Object{
               $_.user
               $_.group
         }


    \_(ツ)_/


    • Edited by jrv Wednesday, November 4, 2015 10:58 PM
    • Marked as answer by Vollstracker Thursday, November 5, 2015 4:28 PM
    Wednesday, November 4, 2015 10:58 PM

All replies

  • What does your script look like so far?


    -- Bill Stewart [Bill_Stewart]

    Wednesday, November 4, 2015 9:58 PM
  • Hi,

    You can use -split:

    $str = '<distinguishedNameUser1>;<distinguishedNameGroup1>'
    $user,$group = $str -split ';'
    
    $user
    $group


    Wednesday, November 4, 2015 10:45 PM
  • Import-Csv file.txt -Head user,group -delim ';'|
         ForEach-Object{
               $_.user
               $_.group
         }


    \_(ツ)_/


    • Edited by jrv Wednesday, November 4, 2015 10:58 PM
    • Marked as answer by Vollstracker Thursday, November 5, 2015 4:28 PM
    Wednesday, November 4, 2015 10:58 PM
  • Thank you (:
    Thursday, November 5, 2015 4:29 PM
  • Hello

    I used the hint of jrv now my script look like the following:

    [CMDletBinding()]
    
    Param(
    [Parameter(Mandatory=$True, Position = 1)]
    [string]$textPath
    )
    
    Import-Csv $textPath -Head user,group -delim ';'|
         ForEach-Object{
               #Write-Output $_.user
               #Write-Output $_.group
               Add-ADGroupMember -Identity $_.group  
               -Members $_.user
    
                
         }
    Move-Item -Path $textPath -Destination "\\Archive"
    Remove-Item -Path $textPath 

    I am calling this script from VBA. Is there a way to check if the script was successfull. The return value from the scripts are 4 Digits.

    Thursday, November 5, 2015 4:33 PM