Created
October 2, 2023 15:14
-
-
Save gioxx/c454490eaaafb1acc3f2a8fcc62589fd to your computer and use it in GitHub Desktop.
Uno script che tramite Microsoft Graph modifica la Usagelocation dell'utente e - in seguito - assegna una licenza Exchange Online Plan 1. Vedi l'articolo completo sul blog all'indirizzo https://go.gioxx.org/graph-usagelocation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Param( | |
[Parameter(Position=0, Mandatory=$true, ValueFromPipeline=$true, HelpMessage="User principal name (es. [email protected])")] | |
[string] $UserPrincipalName | |
) | |
function priv_CheckMGGraphModule { | |
$mggConnected = $false | |
if ( (Get-Module -Name Microsoft.Graph -ListAvailable).count -gt 0 ) { | |
try { | |
Get-MgUser -ErrorAction Stop | |
$mggConnected = $true | |
} catch { | |
Write-Host "Please wait until I load Microsoft Graph, the operation can take a minute or more." -f "Yellow" | |
Import-Module Microsoft.Graph -ErrorAction SilentlyContinue | |
Import-Module Microsoft.Graph.Users -ErrorAction SilentlyContinue | |
Connect-MgGraph | |
$mggConnected = $true | |
} | |
} else { | |
Write-Host "Microsoft Graph PowerShell module is not available." -f "Yellow" | |
$Confirm = Read-Host "Are you sure you want to install module? [Y] Yes [N] No " | |
if ( $Confirm -match "[yY]" ) { | |
try { | |
Write-host "Installing Microsoft Graph PowerShell module ..." | |
Install-Module Microsoft.Graph -Repository PSGallery -Scope CurrentUser -AllowClobber -Force | |
Import-Module Microsoft.Graph -ErrorAction SilentlyContinue | |
Import-Module Microsoft.Graph.Users -ErrorAction SilentlyContinue | |
Connect-MgGraph | |
$mggConnected = $true | |
} catch { | |
""; Write-Host "Can't install and import Graph modules. `nPlease check logs." -f "Red" | |
exit | |
} | |
} else { | |
""; Write-Host "Microsoft Graph PowerShell module is required to run this script. `nPlease install module using Install-Module Microsoft.Graph cmdlet." -f "Red" | |
exit | |
} | |
} | |
return $mggConnected | |
} | |
$mggConnectedCheck = priv_CheckMGGraphModule | |
if ( $mggConnectedCheck -eq $true ) { | |
$P1SKU = Get-MgSubscribedSku -All | Where SkuPartNumber -eq 'EXCHANGESTANDARD' | |
Update-MgUser -UserId $UserPrincipalName -Usagelocation "IT" | |
Set-MgUserLicense -UserId $UserPrincipalName -RemoveLicenses @() -AddLicenses @{SkuId = $P1SKU.SkuId} | |
} else { | |
Write-Host "`nCan't connect or use Microsoft Graph modules. `nPlease check logs." -f "Red" | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment