Quantcast
Channel: Directory Services forum
Viewing all articles
Browse latest Browse all 31638

Change UPN for a group of Users

$
0
0
<#
1. Import Data from a CSV file
2. Process each object in CSV file:
a. Foreach Object
b. Validate the Existence of the User in AD: 
-> if found - Proceed to Step 3
-> If Not Found, Update the Array Variable used for Exporting the Script Execution outcome And/OR Update the Log file
3. Trigger or Attempt to Change the UPN using the value using the reference variable corresponding to the user which is available on the CSV File.
a. IF Successful , Update the Output Array with outcome being successful And/Or write to a Log
b. If Not Successful (On Error), Get the Error Exception indicating the reason for failure and update the array with the exception

4. Export Data to CSV File to Review the execution and outcome of Set-ADUser triggered for UPN Change`

#>


$FileData = Import-CSv "CSV File Path"
$OutputData = @()

Import-module activeDirectory
$UserCount = ($FileData| Measure).Count
$StartCount = 0
Foreach($User in $FileData)
{
$StartCount += 1
$CurrentUser = $User.SamAccountName
$CurrentUPN = ""
$NewUPN = ""
Write-Host "Processing User [$CurrentUser - $StartCount] - Out of [$UserCount]" -ForeGroundColor CYAN
$UserInfo = Get-ADUser $CurrentUser -Properties SamAccountName, UserPrincipalName|Select SamAccountName,UserPrincipalName
if($UserInfo)
{
$CurrentUPN = $UserInfo.UserPrincipalName
$NewUPN = $CurrentUser + "@UPNSuffix"
Set-ADUser $CurrentUser -UserPrincipalName $NewUPN -WhatIf
$OutObj = "" | Select User,CurrentUPN,NewUPN,ChangeStatus
$OutObj.User = $CurrentUser
$OutObj.CurrentUPN = $CurrentUPN
$OutObj.NewUPN = $NewUPN
$OutObj.ChangeStatus = "UPN Change Successful"
$OutputData += $OutObj
$OutObj
}
Else
{
$CurrentUPN = "$CurrentUser - Not IN AD"
$NewUPN = "$CurrentUser - Not IN AD"
$OutObj = "" | Select User,CurrentUPN,NewUPN,ChangeStatus
$OutObj.User = $CurrentUser
$OutObj.CurrentUPN = $CurrentUPN
$OutObj.NewUPN = $NewUPN
$OutObj.ChangeStatus = "User Not In AD"
$OutputData += $OutObj
$OutObj
}
}
$OutputData | Export-Csv UPNChangeStatus.Csv -NTI

Viewing all articles
Browse latest Browse all 31638

Trending Articles