-
Notifications
You must be signed in to change notification settings - Fork 0
/
SetUPN.ps1
71 lines (55 loc) · 2.36 KB
/
SetUPN.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
<#
.SYNOPSIS
Get users from particular domain with "empty" UPN and set correct UPN for those users.
.DESCRIPTION
Get users from particular domain with "empty" UPN and set correct UPN for those users.
.INPUTS
DC
UPNdomain
.OUTPUTS
.NOTES
Version: 1.0
Author: Lubomir Goban
Creation Date: 16.07.2019
Updated:
.EXAMPLE
None
#>
Clear-Host
Import-Module ActiveDirectory
Import-Module ImportExcel #Comment out if not needed
# GET CURRENT DATE. IT IS USED IN EXCEL FILE NAME
$date = Get-Date
################################
### CREDENTIALS USED TO SET UPNs
$username = "<username>"
$pass = Get-Content "$Env:USERPROFILE\Documents\Scripts\sys_mca_sa.txt" | ConvertTo-SecureString
$cred = New-Object System.Management.Automation.PSCredential($username, $pass)
##########
### SERVER
$DC = "<server name>"
##############################
# DOMAIN FOR USERPRINCIPALNAME
$UPNdomain = "<domain>"
############################################
# PATH WHERE THE EXPORTED FILE WILL BE SAVED
$file = "$Env:USERPROFILE\Documents\Exports\processed_users_$date.xlsx"
Write-Progress -Activity "Loading users.."
###############################
# GET LIST OF USERS FROM DOMAIN
$fportalusers = Get-ADUser -Server $DC -Filter * -Properties UserPrincipalName |
Select-Object Name, SamAccountName, Surname, UserPrincipalName |
Where-Object { ($_.UserPrincipalName -eq $NULL) `
-or ($_.UserPrincipalName -like $_.Name) `
-or ($_.UserPrincipalName -like $_.Surname) `
-and ($_.Surname -ne $NULL) `
-and ($_.Name -match '\D\d{0}$') # This expression filter out names with any number. If we change number to 1 or so, it will show name with 1 number at the end.
}
Write-Progress -Activity "Exporting users.."
$fportalusers | Export-Excel -path $file -WorkSheetname Users-noUPN -ClearSheet -AutoSize -BoldTopRow -FreezeTopRow -AutoFilter
Write-Host "Users exported to Excel.. Done." -ForegroundColor Magenta `r`n
Write-Host "Your file is saved in $file" -ForegroundColor Green `r`n
############################
# SET UPN FOR EXPORTED USERS
Set-ADUser $_.SamAccountName -UserPrincipalName "$($_.SamAccountname)@$UPNdomain" -server $DC -credential $cred
Write-Host "UPNs has been set.." -ForegroundColor Green