Need some help with PS scripts

This topic contains 4 replies, has 4 voices, and was last updated by  Joel Sallow 3 months, 1 week ago.

  • Author
    Posts
  • #98502

    eksbs
    Participant

    Hi,

    I'm need some help with creating PS script, what I need is PS that creates bulk OU, Group, User in my AD.

    I have been trying it all morning to get this done, I get close but it fails.

    I want to import all from one CSV file.

  • #98505

    Don Jones
    Keymaster

    Did you have a question? We're not able to write an entire script for you.

  • #98584

    postanote
    Participant

    Break what you are doing into steps, one-liners

    • create OU's
    • create groups
    • create users
    • add user to groups
    • etc..

    then put into a function.

    Do this one at time using the Windows Server Active Directory Administrative Center. It will writ eth code for you to tweak later for bulk operations.

  • #98611

    eksbs
    Participant

    Thank you D.J. & postanote.

    Here is my existing script

    Import-Module ActiveDirectory
    Import-Csv "C:\Users-Imported\NewUsers100.csv" | ForEach-Object {
    $userPrincinpal = $_."samAccountName" + "@TestDomain.Local"
    New-ADUser -Name $_.Name `
    -Path $_."ParentOU" `
    -SamAccountName $_."samAccountName" `
    -UserPrincipalName $userPrincinpal `
    -AccountPassword (ConvertTo-SecureString "MyPassword123" -AsPlainText -Force) `
    -ChangePasswordAtLogon $true `
    -Enabled $true
    Add-ADGroupMember "Remote Desktop Users" $_."samAccountName";
    }

    ==============================================================================================

    Here is my file:
    GivenName,Surname,DisplayName,Name,samAccountName,ParentOU,NewOU
    Bim,bayle,bim byle,Byle Bim,bbim,"OU=Enterprise Administration,DC=pmo,DC=cloud,DC=com",OU=testou
    Kim,Kayle,Kim Kyle,Kyle Kim,kkim,"OU=Enterprise Administration,DC=pmo,DC=cloud,DC=com",OU=testou1
    Jim,Jayle,Jim Jayle,Jim Jayle,jjayle,"OU=Enterprise Administration,DC=pmo,DC=cloud,DC=com",OU=testou2
    Tim,Tayle,Tim Tayle,Tim Tayle,ttayle,"OU=Enterprise Administration,DC=pmo,DC=cloud,DC=com",OU=existingOU

    ===========================================================================================================

    It fails since I don't have "testou, testou1, testou2" created and I know that I need to use New-ADOrganizational Unit but not sure what is the best way to do it and to omit "existingOU"

  • #98619

    Joel Sallow
    Participant

    I would probably recommend splitting these tasks — get a list of the OUs you plan on creating, create them, then create your users. Trying to do it all at once seems more likely to give you a headache when you could be making it a good bit easier on yourself. 🙂

    You could even automate that step (getting the OU list you want) if your file is big enough, but you'll need a way to exclude the existing OUs, maybe...

    Something like...

    $NewOUList = Import-Csv -Path 'C:\Path\To\File.csv' |
        Select-Object -Property ParentOU, NewOU |
        ForEach-Object {
            $_.NewOU, $_.ParentOU -join ','
        }
    # Use $NewOUList to make each OU here

You must be logged in to reply to this topic.