Forum Discussion
Bhavpreet Bains
Oct 09, 2018Iron Contributor
onedrive list of admin
Hello, We have a user who was accidentally added as site collection admin to various OneDrive accounts. Is it possible to pull a list of all site collection admin for all OneDrive accounts? I...
Alex Carlock
Jan 30, 2020Iron Contributor
I wish I had a solution, but I don't. I can just add a "Me Too". The only thing I can think of is to write a script that does the following:
- Check the admins
- If I get results, great
- If I get no results, grant myself access
- Get the list
- Remove my access
I'm amazed that Get-SPOSite and Get-SPOUser have this limitation for Global admins and SharePoint admins, and that Microsoft hasn't worked around this by providing an Audit report from the Office 365 or SharePoint Admin sites.
adrianhalid
Jul 20, 2021Copper Contributor
Import-Module Microsoft.Online.SharePoint.PowerShell -DisableNameChecking
$AdminAccount = "administrator@company.com"
$AdminCenterURL = "https://company-admin.sharepoint.com/"
#Connect to SharePoint Online Admin Center
Connect-SPOService -Url $AdminCenterURL
#Get All OneDrive for Business Sites in the Tenant
$OneDriveSites = Get-SPOSite -Limit ALL -includepersonalsite $True -Filter "Url -like '-my.sharepoint.com/personal/'"
#Loop through each OneDrive Site
Foreach($Site in $OneDriveSites)
{
Write-host "Scanning site:"$Site.Url -f Yellow
try{
$checkadmin = Get-SPOUser -Site $Site.Url | Where {$_.IsSiteAdmin -eq $true -and $_.LoginName -eq $AdminAccount}
$setAdmin = $false;
#Add Temp Site Admin
if($checkadmin.Count -eq 0){
#Write-host "Add Temp Admin:"$Site.URL -f Gray
Set-SPOUser -Site $Site -LoginName $AdminAccount -IsSiteCollectionAdmin $True | Out-Null
$setAdmin = $true
}
}catch{
#Write-Host "Error:" $_.Exception.Message
if($_.Exception.Message -like "Access is denied*"){
#Write-host "Add Temp Admin:"$Site.URL -f Gray
Set-SPOUser -Site $Site -LoginName $AdminAccount -IsSiteCollectionAdmin $True | Out-Null
$setAdmin = $true
}
}
#Get All Site Collection Administrators
$SiteAdmins = Get-SPOUser -Site $Site.Url | Where {$_.IsSiteAdmin -eq $true -and $_.LoginName -ne $AdminAccount -and $_.LoginName -ne $Site.Owner}
if($SiteAdmins.Count -gt 0){
#Iterate through each admin
Foreach($Admin in $SiteAdmins)
{
Write-host "Found other Admin:"$Admin.LoginName -f Blue
}
}
#Remove Temp Site Administrator if added
if($setAdmin -eq $true){
#Write-host "Remove Temp Admin:"$Site.URL -f Gray
Set-SPOUser -site $Site -LoginName $AdminAccount -IsSiteCollectionAdmin $False | Out-Null
}
}
- adrianhalidDec 28, 2021Copper Contributor
Where ever you see a Write-Host you could just output to a file on your disk.
Try using Add-Content or Out-File.
https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.management/add-content?view=powershell-7.2
https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.utility/out-file?view=powershell-7.2
- Lisa GentryDec 20, 2021Copper Contributor
- divadiowNov 08, 2021Copper Contributor