PowerShell: AD tasks for user conversions

6 min readGinja

I wrote a Commandlet that handles the tasks of converting a contractor to an employee.  When time allows I will go through it line-by-line and probably tweak it a lot along the way.

function Convert-ToEmployee {
    param ($empName)
    
    if ($empName) {
        
        $domaingroup          = "Domain Employees"
        $myName               = (Get-ChildItem Env:Username).Value
        [int]$myLength        = ($myName.length) - 1
        [string]$myInitials   = ($myName.SubString($mylength,1) + $myName.SubString(0,1)).ToUpper()
        $date                 = (Get-Date).ToShortDateString()
        
        $ticketN              = Read-Host "Ticket #?"
        
        $notes                = "$myInitials $date $ticketN"
        $notesArray           = $notes.split(" ")
        
        $note                 = (Get-QADUser $empName).Notes
        [string]$note        += "`r`n$Notes" 
        
        Write-Host "-----------------" -fore gray
        Write-Host "1. PDX"            -fore red
        Write-Host "2. SFO"            -fore red
        Write-Host "3. SEA"            -fore red
        Write-Host "4. Remote"         -fore red
        Write-Host "5. Give me a list" -fore red
        Write-Host "-----------------" -fore gray
        
        $selLocation = Read-Host "Please select a location."
        
        switch ($selLocation) {
        
            "1" {
                $location = "PDX " +  (Read-Host "Cube #?")
                $allemployeesgroup = Get-QADGroup "All Employees @ Portland"
                $homedirectory = "xxxfile01"
        
            }
        
            "2" {
                $location = "SFO"
                $allemployeesgroup = Get-QADGroup "All Employees @ San Francisco"
                $homedirectory = "xxxfile01"
        
            }
        
            "3" {
                $location = "SEA"
                $allemployeesgroup = Get-QADGroup "All Employees @ Seattle"
                $homedirectory = "xxxfile01"
        
            }
        
            "4" {
                $location = "Remote"
                $allemployeesgroup = Get-QADGroup "All Employees @ Remote" 
                $homedirectory = "xxxfile01"
        
            }
        
            "5" {
                
                $allemployeesgroup = Get-Grouplist "*All Employees*" -newHire "y"   
                $location = Read-Host "Please specify location."
        
            }
        
        }
        
        Write-Host 
        
        $title      = Read-Host "Enter their new title"
        $department = Read-Host "Enter their new department"
        
        Write-Host "--------------------------" -fore gray
        Write-Host "Convert:" (Get-QADUser $EmpName).Name "To Employee" -fore red
        Write-Host "--------------------------" -fore gray
        Write-Host "All Employees Group:" $allemployeesgroup " " -fore red
        Write-Host "Location/Office    :" $location          " " -fore red
        Write-Host "Title              :" $title             " " -fore red
        Write-Host "Title              :" $department        " " -fore red
        Write-Host "--------------------------" -fore gray
        Write-Host "------------------------------------------------" -fore gray
        Write-Host "Tasks:" -fore red
        Write-Host "------------------------------------------------" -fore gray
        Write-Host "1. Removing account expiration."
        Write-Host "2. Moving to Employees OU."
        Write-Host "3. Setting location."
        Write-Host "4. Removing Contractor Groups."
        Write-Host "5. Adding to domain employees and all employees."
        Write-Host "------------------------------------------------" -fore gray
        Write-Host "Proceed:"
        Write-Host "1. Yes"
        Write-Host "2. No"
        
        $proceed = Read-Host "Please select a number."
        
        Switch ($proceed) {
        
            "1" { 
        
                Write-Host "Removing Expiration, changing office, title, department, company, and notes..."
            
                Set-QADUser $empName -AccountExpires $null -Office $location -Title $title -Description $title -Department $department -Company $company -Notes $note -Credential $identity | Out-Null
            
                Write-Host "Removing from Contractor groups..."
            
                $groups = (Get-QADUser $empName).MemberOf
            
                foreach ($g in $groups) {
            
                    if ($g -like "*contractor*") {
                
                    Remove-QADGroupMember $g $empName -Credential $identity | Out-Null
                
                    }
                
                }
        
                Write-Host "Adding to Employee groups..."
                
                Add-QADGroupMember $allemployeesgroup $empName -Credential $identity | Out-Null
                Add-QADGRoupMember $domaingroup $empName -Credential $identity       | Out-Null
                
                Write-Host "Moving to Employee's OU..."
                
                Move-QADObject $usrInfo.SamAccountName -NewParentContainer $eOu -Credential $identity | Out-Null
            
            
            }
            
            "2" {
            
            Write-Host "Aborted."
        
            }
        }
   }
}

 

 

Comments

No comments yet. Be the first!