Modules/OneDrive/sandbox.ps1

if (-not(Test-Path Env:PowerShellLogs)) { 
    $value = Read-Host "Enter Path of PowerShell Logs Directory" 
    $logPath = $value
    [System.Environment]::SetEnvironmentVariable('PowerShellLogs', $value, [System.EnvironmentVariableTarget]::Machine)
}
if (-not(Test-Path Env:PowerShellCreds)) { 
    $value = Read-Host "Enter Path of PowerShell Credentials Directory"
    $credPath = $value
    [System.Environment]::SetEnvironmentVariable('PowerShellCreds', $value, [System.EnvironmentVariableTarget]::Machine)
}

Clear-Host

$logPath = $Env:PowerShellLogs
$credPath = $Env:PowerShellCreds
$now = (Get-Date).ToString("yyyyMMddhhmm")
Start-Transcript -LiteralPath "$logPath\WJCCS-OneDrive-$now.log"
Write-Host "Log File Folder: $logPath"
Write-Host "Credential File Folder: $credPath"

Write-Host -NoNewline "Checking for SharePoint Online Credential File"
$AdminName = "marathon@wjccschools.org"
$CredsFile = "$credPath\$($AdminName.Replace("@","-").Replace(".","-")).txt"

$FileExists = Test-Path $CredsFile
if ($FileExists -eq $false) {
    Write-Host -f Yellow " .....Not Found!"
    Write-Host -f Yellow "Creating new credential file"
    Write-Host ""
    Write-Host "Enter your password: "
    Read-Host -AsSecureString | ConvertFrom-SecureString | Out-File $CredsFile
    $password = get-content $CredsFile | convertto-securestring
    $spoCredential = new-object -typename System.Management.Automation.PSCredential -argumentlist $AdminName, $password
} else {
    Write-Host -f Green " .....Found!"
    Write-Host 'Using stored credential file' -ForegroundColor Green
    $password = get-content $CredsFile | convertto-securestring
    $spoCredential = new-object -typename System.Management.Automation.PSCredential -argumentlist $AdminName, $password
}

Try {
    Start-Transcript "$($env:PowerShellLogs)\WJCCSchools-OneDriveFix.log"
    Write-Host -NoNewline "Connecting to SPO Admin Center"
    $adminConnection = Connect-PnPOnline https://wjccschools-admin.sharepoint.com -Credential $spoCredential -ReturnConnection
    Write-Host -ForegroundColor Green " .....Done!"

    $runAccount = "marathon@wjccschools.org"
    Write-Host -NoNewline "Getting List of OneDrive Sites"
    $OneDrive = Get-PnPTenantSite -Connection $adminConnection -IncludeOneDriveSites -Filter "Url -like '-my.sharepoint.com/personal/'" | Where-Object { $_.Owner -notlike '*@student.wjccschools.org' } | Select-Object Title, Owner, URL 
    Write-Host -ForegroundColor Green " .....Done!"
    Write-Host ""

    $OneDrive | ForEach-Object {
        $siteURL = $_.URL
        Write-Host -NoNewline "Adding Site Admin ($($runAccount)) to $($siteURL)"
        Set-PnPTenantSite $siteURL -Owners $runAccount -Connection $adminConnection
        Write-Host -ForegroundColor Green " .....Done!"
    }
    Write-Host ""
    Write-Host -NoNewline "Disconnectiong from $($adminConnection.Url)"
    Disconnect-PnPOnline -Connection $adminConnection
    Write-Host -ForegroundColor Green " .....Done!"
    Write-Host ""

    $OneDrive | ForEach-Object {
        Try {
            Write-Host "Processing $($_.Owner)"
            $Owner = $_.Owner
            $siteURL = $_.URL

            Write-Host -NoNewline "Connecting to $($siteURL)"
            $connectSite = Connect-PnPOnline $siteURL -Credential $spoCredential -ReturnConnection
            Write-Host -ForegroundColor Green " .....Done!"

            Write-Host -NoNewline "Getting Site Admins"
            $admins = Get-PnPSiteCollectionAdmin -Connection $connectSite
            Write-Host -ForegroundColor Green " .....Done!"

            $admins | Where-Object { $_.Email -ne $runAccount -and $_.Email -ne $Owner } | Foreach-Object {
                Write-Host -NoNewline "Removing $($_.Email)"
                Remove-PnPSiteCollectionAdmin -Owners $_.Email -Connection $connectSite
                Write-Host -ForegroundColor Green " .....Done!"
            }          
            Write-Host -NoNewline "Removing $($runAccount)"
            Remove-PnPSiteCollectionAdmin -Owners $runAccount -Connection $connectSite
            Write-Host -ForegroundColor Green " .....Done!"  
        }
        catch {
            Write-Host -ForegroundColor Red " .....Error!"
            Write-Error $_.Exception.Message
        }
        finally {
            Write-Host ""
        }
    }

    Write-Host -NoNewline "Disconnectiong from $($connectSite.Url)"
    Disconnect-PnPOnline -Connection $connectSite
    Write-Host -ForegroundColor Green " .....Done!"
}
catch {
    Write-Host -ForegroundColor Red " .....Error!"
    Write-Error $_.Exception.Message
}
finally {
    Stop-Transcript
}