Office 365: Script PowerShell pour supprimer des comptes utilisateur de collections de sites SharePoint Online
Le moteur de gestion des utilisateur de SharePoint Online est bien basé sur le core system de SharePoint, en l’occurence les SPUsers.
Cette liste intermédiaire est visible d’ailleurs par l’URL:
Le soucis est la suppression d’un compte utilisateur de cette base intermédiaire, car aucune interface ne permet cette suppression hormis la commande PowerShell suivante:
Le script suivant vous aide donc à effectuer ce nettoyage dans les collections de sites que vous souhaitez (selon les filtrages appliqués à la commande Get-SPOSite):
[string]$username = "AdminAccount@yourTenant.onmicrosoft.com"
[string]$PwdTXTPath = "C:\SECUREDPWD\ExportedPWD-$($username).txt"
[string]$SiteCollectionURL = "https://yourTenant.sharepoint.com"[string]$LoginAccounttoRemove = "i:0#.f|membership|User.Login@yourTenant.com"
function Load-DLLandAssemblies
{
[string]$defaultDLLPath = ""# Load assemblies to PowerShell session
$defaultDLLPath = "C:\Program Files\SharePoint Online Management Shell\Microsoft.Online.SharePoint.PowerShell\Microsoft.SharePoint.Client.dll"
[System.Reflection.Assembly]::LoadFile($defaultDLLPath)$defaultDLLPath = "C:\Program Files\SharePoint Online Management Shell\Microsoft.Online.SharePoint.PowerShell\Microsoft.SharePoint.Client.Runtime.dll"
[System.Reflection.Assembly]::LoadFile($defaultDLLPath)$defaultDLLPath = "C:\Program Files\SharePoint Online Management Shell\Microsoft.Online.SharePoint.PowerShell\Microsoft.Online.SharePoint.Client.Tenant.dll"
[System.Reflection.Assembly]::LoadFile($defaultDLLPath)
}
cls
Write-host " ------------------------------------------------------------ "
Write-host " Removing Specific Account from specific Site collection "
Write-host " ----------------------------------------------------------- "Load-DLLandAssemblies
$secureStringPwd = ConvertTo-SecureString -string (Get-Content $PwdTXTPath)
$adminCreds = New-Object System.Management.Automation.PSCredential $username, $secureStringPwdConnect-SPOService -Url https://yourTenant-admin.sharepoint.com -credential $adminCreds -ErrorAction SilentlyContinue -ErrorVariable Err
#Retrieve all site collection infos
#$sitesInfo = Get-SPOSite -Template "STS#0" -Limit ALL | Sort-Object -Property url | Select *
#$sitesInfo = Get-SPOSite -Filter {Url -like "https://yourTenant.sharepoint.com/sites/YourSiteCollection"} -Limit ALL | Sort-Object -Property url | Select *
$sitesInfo = Get-SPOSite -Template "BLANKINTERNET#0" -Limit ALL | Sort-Object -Property url | Select *[int]$i = 1;
Write-host " ===>>> ", $sitesinfo.count + " site collections found." -ForegroundColor green
foreach ($site in $sitesInfo)
{
$CheckUser = $null
Write-host " ------------------------------------------------------------ "
Write-host "SiteColl Number:", $i, "- of:", $sitesInfo.Count -ForegroundColor green
$i += 1;
Write-Host "SPO Site collection:", $site.Url, "- Title:", $site.Title -ForegroundColor magenta
Write-Host " => External Sharing:", $site.SharingCapability
Write-Host " => Site Template Used:", $site.Template$CheckUser = Get-SPOUser -Site $site.Url -LoginName $LoginAccounttoRemove
if($CheckUser.count -gt 0)
{
write-Host " >>>> Removing User Account:", $LoginAccounttoRemove -ForegroundColor magenta
$CheckUser | Format-Table
Remove-SPOUser -Site $site.Url -LoginName $LoginAccounttoRemove
}
else
{
write-Host " >>>> User Account", $LoginAccounttoRemove, "does not exist into the site collection:", $site.Url -ForegroundColor Yellow
}
Write-host " ------------------------------------------------------------ "}
Vous pouvez bien sur modifier ce script selon votre besoin.
Fabrice Romelard
Version anglaise:
Sources:
- https://support.office.com/en-ie/article/how-to-remove-users-from-sharepoint-online-9e470280-e822-42ca-b3f5-744c12d0955a
- https://docs.microsoft.com/en-us/powershell/module/sharepoint-online/get-spouser?view=sharepoint-ps
- https://docs.microsoft.com/en-us/powershell/module/sharepoint-online/remove-spouser?view=sharepoint-ps
Commentaires
Enregistrer un commentaire