User Creation

Welcome Forums General PowerShell Q&A User Creation

This topic contains 5 replies, has 4 voices, and was last updated by

Jon
 
Participant
3 months, 1 week ago.

  • Author
    Posts
  • #130962

    Participant
    Points: 54
    Rank: Member

    I have been to create a script for user creation, before i get in to the fancy stuff of adding groups & moving to correct OU.

    I am having trouble import from csv, as my output is, well not complete. Can someone help with debugging it.

    I'm just not sure why this is doing it.

     

    This is my CSV Layout;

    Fistname,Lastname,Location,Role,Manager
    First,Last,Loction,Role,Manager

     

    This is my script;

    $Users = Import-Csv -Delimiter "," -Path "C:\User.csv"
    foreach ($User in $Users)
    {
    $Firstname = $User.Firstname
    $Lastname = $User.Lastname
    $Location = $User.Location
    $Role = $User.Role
    $Manager = $User.Manager
    }

    #Configures Variabes for account creation
    #Account Name
    $sAMAccountName = $User.Firstname + "." + $User.Lastname
    #Display Name
    $displayName = $User.Firstname + " " + $User.Lastname
    #Common Name for account
    $cn = $User.Firstname + " " + $User.Lastname

    write-output $sAMAccountName
    write-output $displayName
    write-output $cn

     

    This is my output

    .Last
    Last
    Last

  • #131091

    Keymaster
    Points: 1,811
    Helping HandTeam Member
    Rank: Community Hero

    It helps tremendously if you take a moment to format your code so we can read it more easily. The problem is here:

    
    foreach ($User in $Users)
    {
    $Firstname = $User.Firstname
    $Lastname = $User.Lastname
    $Location = $User.Location
    $Role = $User.Role
    $Manager = $User.Manager
    }
    
    

    This is running through every line in your CSV file, and so all $Firstname and so forth will contain at the end are the values form the last line. You probably need ALL of your code within the ForEach loop, not just this portion.

  • #133214

    Participant
    Points: 54
    Rank: Member

    Thanks Don, After TS & googling, i was able to get this to work as i wanted.

  • #133397

    Participant
    Points: 54
    Rank: Member

    In Addition to this, I would need it to wait/loop until this user was found in 365. Now I have written the following

    Import-Module MSonline
    Connect-MsolService
    
    $UserNameCheck =$false
    $UserName = Read-Host "Enter User Name"
    
    Do{
      $User = Get-MsolUser -SearchString "$UserName"
         If ($UserName -eq $User)
            {
             $UserNameCheck = $true
              "Found"
            } 
         Else
             {
              $UserNameCheck = $false
              Write-warning "Not Found"
             }
      }Until ($UserNameCheck -ne $false)

     

    The problem is this does not seem to work. I have entered a user that exists, but the result I get is my own error of Not Found.

    Assistants would be grateful.

    So what I'm attempting to create is a script that will create a new user, but then wait for that user to be created in 365, before adding the correct groups to the user.

    I have managed to piece together what I wanted, but I now need it to loop until the user is found before continuing.

  • #133413

    Moderator
    Points: 1,991
    Helping Hand
    Rank: Community Hero

    Check the output of Get-MsolUser cmdlet. I never used it, but guessing it returns an object and not user as string. Then your comparison with $UserName won't work. you will have to do $User.Name -eq $UserName (if Name is the property which exposes user name)

  • #133418
    Jon

    Participant
    Points: 89
    Rank: Member

    You didn't say what commands you are using to get the user into O365 but I use new-remotemailbox, and then kick off a delta sync. I have found that it takes about 90 seconds from when I run the delta sync for the user to show up in O365. Once I run the delta sync, I put in a sleep timer for 90 seconds and then do the following:

    
    Do
    
    {
    
    Get-MsolUser -UserPrincipalName $upn -ErrorAction SilentlyContinue -ErrorVariable o365error | Out-Null
    
    } #enddostatement
    
    Until (!$o365error)
    
     
    
    

The topic ‘User Creation’ is closed to new replies.

denizli escort samsun escort muğla escort ataşehir escort kuşadası escort