Skip to content

Instantly share code, notes, and snippets.

@gioxx
Created October 2, 2023 15:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gioxx/c454490eaaafb1acc3f2a8fcc62589fd to your computer and use it in GitHub Desktop.
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
Param(
[Parameter(Position=0, Mandatory=$true, ValueFromPipeline=$true, HelpMessage="User principal name (es. mario.rossi@contoso.com)")]
[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