Provisioning dei siti personali di Sharepoint (OneDrive) su Tenant
Può capitare la necessità di pre-popolare i siti OneDrive degli utenti prima che gli stessi effettuino il login; necessità abbastanza comune in scenari di migrazione da una piattaforma cloud a Microsoft 365.
Ecco uno script che soddisfa l’esigenza:
# ==== CONFIGURAZIONE ==== $tenantId = "ID Tenant M365" $clientId = "ID App EntraID" $clientSecret = "Valore Client Secret" $hostname = "-my.sharepoint.com" # Es. contoso-my.sharepoint.com $outputFile = "OneDriveProvisioningReport.csv" # ==== OTTIENI TOKEN ==== $tokenRequest = Invoke-RestMethod -Method Post -Uri "https://login.microsoftonline.com/$tenantId/oauth2/v2.0/token" -Body @{ client_id = $clientId scope = "https://graph.microsoft.com/.default" client_secret = $clientSecret grant_type = "client_credentials" } $accessToken = $tokenRequest.access_token $headers = @{ Authorization = "Bearer $accessToken" } # ==== OTTIENI UTENTI ==== $users = @() $uri = "https://graph.microsoft.com/v1.0/users?$filter=accountEnabled eq true&$select=id,userPrincipalName&$top=999" do { $response = Invoke-RestMethod -Uri $uri -Headers $headers -Method Get $users += $response.value $uri = $response.'@odata.nextLink' } while ($uri) $emails = $users | Select-Object -ExpandProperty mail # ==== COSTRUISCI E VERIFICA ONEDRIVE ==== $results = @() foreach ($user in $users) { $upn = $user.userPrincipalName $id = $user.id $escapedUpn = $upn -replace "@", "_" -replace "\.", "_" $sitePath = "personal/$escapedUpn" $driveUri = "https://graph.microsoft.com/v1.0/sites/{$hostname}:/{$sitePath}:/drive" try { $drive = Invoke-RestMethod -Headers $headers -Uri $driveUri -Method Get $available = $true } catch { $available = $false } $results += [PSCustomObject]@{ UserPrincipalName = $upn ObjectId = $id OneDriveUrl = "https://$hostname/$sitePath" DriveAvailable = $available } } # ==== STAMPA STATUS ONEDRIVE ==== foreach ($user in $users) { $upn = $user.userPrincipalName $userId = $user.id $escapedUpn = $upn -replace "@", "_" -replace "\.", "_" $sitePath = "personal/$escapedUpn" $driveUrl = "https://$hostname/$sitePath" $driveUri = "https://graph.microsoft.com/v1.0/users/$userId/drive" try { # Primo tentativo: verifica se OneDrive è già disponibile $response = Invoke-RestMethod -Headers $headers -Uri $driveUri -Method Get Write-Host "✅ $upn → OneDrive disponibile → $driveUrl" -ForegroundColor Green } catch { $errorMessage = $_.Exception.Message if ($errorMessage -like "*404*") { # Provisioning NON ancora avvenuto → forzo il provisioning con una GET Write-Host "🕒 $upn → OneDrive non ancora disponibile, provisioning innescato..." -ForegroundColor Yellow # Questa chiamata serve solo a innescare il provisioning try { $response = Invoke-RestMethod -Headers $headers -Uri $driveUri -Method Get } catch { # Ignora l'errore, l'importante è che il provisioning sia partito } } else { Write-Host "❌ $upn → Errore imprevisto: $errorMessage" -ForegroundColor Red } } }