-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathNew-ImmutableID.ps1
71 lines (65 loc) · 2.25 KB
/
New-ImmutableID.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
function New-ImmutableID
{
<#
.SYNOPSIS
Generates new ImmutableID for AD Users.
.DESCRIPTION
Looks up AD user objects based on UserPrincipalName, and generates
an ImmutableID by converting the ObjectGUID to a Base64 string.
.PARAMETER Name
$UserPrincipalName
.EXAMPLE
New-ImmutableID [email protected]
.EXAMPLE
New-ImmutableID -UserPrincipalName [email protected]
.EXAMPLE
Import-CSV Users.csv | New-ImmutableID
#>
[CmdletBinding()]
param
(
[Parameter(Mandatory=$True,
Position=0,
ValueFromPipeline=$True,
ValueFromPipelineByPropertyName=$True)]
[ValidateNotNullOrEmpty()]
[Alias('UPN')]
[string[]]$UserPrincipalName
)
begin
{
$Output = New-Object -TypeName System.Collections.ArrayList
$ForestName = ([System.DirectoryServices.ActiveDirectory.Forest]::GetCurrentForest()).Name
$ADsPath = [ADSI]"GC://$ForestName"
$Search = New-Object -TypeName ADSISearcher -ArgumentList $ADsPath
}
process
{
$Search.Filter = "(&(objectCategory=User)(UserPrincipalName=$UserPrincipalName))"
$Results = $Search.FindAll()
foreach ($Result in $Results)
{
$ADUser = $Result | Select-Object -ExpandProperty Properties |
Select-Object @{Name='ObjectGUID';Expression={$_.objectguid}},
@{Name='UserPrincipalName';Expression={$_.userprincipalname}},
@{Name='ImmutableID';Expression={$_.extensionattribute15}}
if ($ADUser.ImmutableID -ne $null)
{
$ID = $ADUser.ImmutableID
$UPN = $ADUser.UserPrincipalName
Write-Error "ImmutableID $ID already exists for $UPN"
}
else
{
$ByteArray = ([GUID]$ADUser.ObjectGUID).ToByteArray()
$ImmutableID = [system.convert]::ToBase64String($ByteArray)
$Properties = @{
ImmutableID = $ImmutableID
Userprincipalname = $ADUser.userPrincipalName}
$Object = New-Object -TypeName PSObject -Property $Properties
[Void]$Output.Add($Object)
}
}
}
end {Write-Output -InputObject $Output}
}