-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathUpdate-Domain.psm1
133 lines (105 loc) · 4.55 KB
/
Update-Domain.psm1
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
#Region Main Function
function Update-Domain {
<#
.SYNOPSIS
This CMDLET is used to perform a bulk update of UserName's Domain on Office 365
.DESCRIPTION
This works together with the *Get-Msoluser* CMDLET
This also works with a CSV file with a column named *UserPrincipalName* to bulk update Office 365 User's Domain
.EXAMPLE
Get-MsolUser | Update-Domain -Domain "contoso.com"
Get-MsolUser | Update-Domain -Dn "contoso.com"
Get-MsolUser | Update-Domain "contoso.com"
Get-MsolUser | Update-Domain -Domain "contoso.com" -Verbose
--------------------------------------------------------------------------------------
--This sets all the users domain in Office 365 to *contoso.com*--
** fabikram@contoso.onmicrosoft.com to fabikram@contoso.com **
** ibhadogiemu@contoso.onmicrosoft.com to ibhadogiemu@contoso.com **
======================================================================================
Import-Csv -Path .\DomainList.csv | Update-Upn -Domain "contoso.com"
--------------------------------------------------------------------------------------
--This sets all the users domain in the CSV File on Office 365 to *contoso.com*--
** fabikram@contoso.onmicrosoft.com to fabikram@contoso.com **
** ibhadogiemu@contoso.onmicrosoft.com to ibhadogiemu@contoso.com **
.INPUTS
Pipe line outputs from *Get-MsolUser*
Pipe line outputs from *Imported Csv Files*
.OUTPUTS
None
.NOTES
When Using a CSV File, remember to name the column header which has the usernames as *UserPrincipalName*
#>
[CmdletBinding(PositionalBinding = $false, SupportsShouldProcess = $true, ConfirmImpact = 'High')]
param (
[Parameter(Mandatory = $true,
ValueFromPipeline = $false,
Position = 0,
HelpMessage = "Enter a Verified Domain name on your Office 365 Tenant")]
[Alias("Dn")]
[String]
$Domain,
[Parameter(Mandatory = $true,
ValueFromPipeline = $true,
ValueFromPipelineByPropertyName = $true)]
[String]
$UserPrincipalName
)
begin {
[Int32]$Script:Divisor = 0
[Int32]$Script:i = 0
}
# process {
# }
end {
$input | Measure-Size
Write-Warning "This is a WARNING -----Running this will change the Domain Name of all piped in Username on Office 365" -WarningAction Inquire
foreach ($UserPrincipalNames in $input) {
if ($PSCmdlet.ShouldProcess($UserPrincipalNames.UserPrincipalName, "UPN update")) {
#Region Get Prefix
$Symbol = $UserPrincipalNames.UserPrincipalName.IndexOf("@")
$Prefix = $UserPrincipalNames.UserPrincipalName.Substring(0, $Symbol)
$NewUpn = ($Prefix + "@" + $Domain)
#Region Get Prefix
#set write-progress incremental variable
[Int32]$Script:i = [Int32]$Script:i + 1
Write-Progress -Activity "Updating" -CurrentOperation "Updating UPN" -Status ($UserPrincipalNames.UserPrincipalName + " is Complete:") -PercentComplete ([Int32]$Script:i/[Int32]$Script:Divisor*100);
Write-Verbose -Message ("I am trying to update " + $UserPrincipalNames.UserPrincipalName + " to " + $NewUpn)
Start-Sleep -m 1
try {
#Region Set New UserName
Set-MsolUserPrincipalName -UserPrincipalName $UserPrincipalNames.UserPrincipalName -NewUserPrincipalName $NewUpn
#EndRegion Set New UserName
}
catch {
Write-Error ";( Error is above"
break #break the code
}
}
else {
}
}
}
}
#EndRegion Main Function
# #Region Counter
function Measure-Size {
[CmdletBinding(PositionalBinding = $false)]
param (
[Parameter(Mandatory = $true,
Position = 0,
ValueFromPipeline = $true)]
[String]
$UserPrincipalName
)
# begin {
# }
process {
#Divisor Count
foreach ($UserPrincipalNames in $_.UserPrincipalName) {
$Script:Divisor = $Script:Divisor + 1
}
}
end {
}
}
#EndRegion Counter