-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSet-GuestUserPhoto.ps1
More file actions
218 lines (175 loc) · 7.26 KB
/
Copy pathSet-GuestUserPhoto.ps1
File metadata and controls
218 lines (175 loc) · 7.26 KB
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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
<#
.SYNOPSIS
Set photo thumbnail for Azure AD Guest users.
Thomas Stensitzki
THIS CODE IS MADE AVAILABLE AS IS, WITHOUT WARRANTY OF ANY KIND. THE ENTIRE
RISK OF THE USE OR THE RESULTS FROM THE USE OF THIS CODE REMAINS WITH THE USER.
Version 1.0, 2019-06-03
Ideas, comments and suggestions to support@granikos.eu
.LINK
http://scripts.Granikos.eu
.DESCRIPTION
This script set the AzureADThumbnailPhoto for guest users to a photo provided as jpg/png file.
The file can be up to 100KB in size. This is currently not checked in the script.
You can either update a single guest user or all guest users. When updating the user photo can
choose to set the photo forcibly or only if there is no photo set.
The changes are written to a log file. The log file functions are part of the GlobalFunctions module.
.NOTES
Requirements
- Utilizes the global function library, found here: http://scripts.granikos.eu
- AzureAD V2 module (aka AzureAdPreview), found here: https://go.granikos.eu/AzureADv2
Revision History
--------------------------------------------------------------------------------
1.0 Initial community release
.PARAMETER UserPrincipalName
The UPN of an AzureAD guest user which is normally the guest users external email address
.PARAMETER FilePath
The full filepath to the jpg/png file that you want to set
.PARAMETER GuestUsersToSelect
Switch to select, if you want to set the photo for a single user or all users
Single = just a single user
All = all guest users in your tenant
.PARAMETER UpdateMode
The update mode for setting guest user pictures
OverwriteIfPhotoExists = set the user photo regardless if there is an existing photo
SetIfNoPhotoExists = set the user photo only, if no user photo exists
.EXAMPLE
Set the photo ExternalUser.png for all guest users if no photo exists
.\Set-GuestUserPhoto.ps1 -FilePath D:\Photos\ExternalUser.png -GuestUsersToSelect All -UpdateMode SetIfNoPhotoExists
.EXAMPLE
Set the photo ExternalUser.png for guest user JohnDoe@varunagroup.de if no photo exists
.\Set-GuestUserPhoto.ps1 -FilePath D:\Photos\ExternalUser.png -GuestUsersToSelect Single -UserPrincipalName JohnDoe@varunagroup.de
#>
[CmdletBinding()]
Param(
[string]$UserPrincipalName = '',
[string]$FilePath = '',
[ValidateSet('All','Single')] # Available modes for selecting guest user target objects, default: SINGLE
[string] $GuestUsersToSelect = 'Single',
[ValidateSet('OverwriteIfPhotoExists','SetIfNoPhotoExists')] #
[string] $UpdateMode = 'SetIfNoPhotoExists'
)
# Some variables to declare
$ScriptDir = Split-Path -Path $script:MyInvocation.MyCommand.Path
$ScriptName = $MyInvocation.MyCommand.Name
function Import-RequiredModules {
<#
.SYNOPSIS
Import required PowerShell modules. If the modules are not available, script execution fails.
.EXAMPLE
Import-RequiredModules
.INPUTS
None
.OUTPUTS
None
#>
# Import GlobalFunctions
if($null -ne (Get-Module -Name GlobalFunctions -ListAvailable).Version) {
Import-Module -Name GlobalFunctions
}
else {
Write-Warning -Message 'Unable to load GlobalFunctions PowerShell module.'
Write-Warning -Message 'Open an administrative PowerShell session and run Import-Module GlobalFunctions'
Write-Warning -Message 'Please check http://bit.ly/GlobalFunctions for further instructions'
exit
}
# Import required PowerShell module for Azure AD
if($null -ne (Get-Module -Name AzureADPreview -ListAvailable).Version) {
# Import the most recent module, if module exists in multiple versions
Get-Module -Name AzureADPreview -ListAvailable | Select-Object -First 1 | Import-Module
}
else {
# Ooops
Write-Warning -Message 'Unable to load AzureADPreview (Azure AD Version 2) PowerShell module.'
exit
}
}
function Test-PhotoPath {
[boolean]$PhotoPathValid = $false
if(($FilePath -ne '') -and (Test-Path -Path $FilePath)) {
$PhotoPathValid = $true
}
else {
$message = ("The provided FilePath '{0}' is not valid." -f $FilePath)
$logger.Write($message,2)
Write-Warning -Message $message
exit
}
$PhotoPathValid
}
function Set-AzureADUserPhoto {
[CmdletBinding()]
param(
[Microsoft.Open.AzureAD.Model.User]$UserObject
)
if($UserObject.UserType -eq 'Guest') {
if($UpdateMode -eq 'OverwriteIfPhotoExists') {
# set thumbnail photo regardless, if there is an axisting photo
Set-AzureADUserThumbnailPhoto -ObjectId $UserObject.ObjectId -FilePath $FilePath
$logger.Write(('AzureADUserThumbnailPhoto set for user {0}' -f $UserObject.MailNickName))
}
elseif($UpdateMode -eq 'SetIfNoPhotoExists') {
$ThumbnailPhoto = $null
try {
$ThumbnailPhoto = Get-AzureADUserThumbnailPhoto -ObjectId $UserObject.ObjectId -ErrorAction SilentlyContinue
$logger.Write(('AzureADUserThumbnailPhoto NOT set for user {0}' -f $UserObject.MailNickName))
}
catch {
$ThumbnailPhoto = $null
}
if($null -eq $ThumbnailPhoto) {
Set-AzureADUserThumbnailPhoto -ObjectId $UserObject.ObjectId -FilePath $FilePath
$logger.Write(('AzureADUserThumbnailPhoto set for user {0}' -f $UserObject.MailNickName))
}
}
}
}
## MAIN #########################
# Import required modules first
Import-RequiredModules
# create new logger
$logger = New-Logger -ScriptRoot $ScriptDir -ScriptName $ScriptName -LogFileRetention 14
$logger.Purge() # Purge files based on file retention setting
$logger.Write('Script started')
$logger.Write(('UpdateMode: {0} | GuestUsersToSelect: {1}' -f ($UpdateMode), ($GuestUsersToSelect)))
switch($GuestUsersToSelect) {
'All' {
# Update all guest user objects in tenant
if(Test-PhotoPath) {
# Issue #1 implement automation capabilities
$null = Connect-AzureAD
# fetch all guest users from Azure AD
$AllGuestUsers = Get-AzureADUser -Filter "UserType eq 'Guest'"
$logger.Write(('Found {0} Azure AD guest users ' -f ($AllGuestUsers | Measure-Object).Count))
foreach($AzureADUser in $AllGuestUsers) {
# Set thumbnail photo for user
Set-AzureADUserPhoto -UserObject $AzureADUser
}
}
else {
$logger.Write(('Testing file path {0} failed' -f $FilePath),2)
}
}
default {
if($UserPrincipalName -ne '') {
if(Test-PhotoPath) {
# Issue #1 implement automation capabilities
Connect-AzureAD
# fetch single user from Azure AD
$AzureADUser = Get-AzureADUser -SearchString $UserPrincipalName.ToLower()
# Set thumbnail photo for user
Set-AzureADUserPhoto -UserObject $AzureADUser
}
else {
$logger.Write(('Testing file path {0} failed' -f $FilePath),2)
}
}
else {
$message = 'You have tried to update a single guest user object, but the UserPrincipal parameter is not set. Please try again.'
Write-Warning -Message $message
$logger.Write($message,2)
exit
}
}
}
$logger.Write('Script finished')