[PS SCRIPT]: OneDrive Documents Redirection and Status check

How do you ensure each endpoint is properly synced and redirected? What if important data is lost due to a misconfiguration?

Run this script from RMM or Intune Proactive Remediations in the user context. Exit 1 and output are generated when issues occur.

Do you need help with your Modern Workplace? Check out our service page!

#OneDrive Documents Redirection and Status check
#2024.01.21 [email protected]

function CheckSyncStatus () {
    $OneDriveDocs = [Environment]::GetFolderPath("MyDocuments")
    $Shell = (New-Object -ComObject Shell.Application).NameSpace((Split-Path $OneDriveDocs))
    $Status = $Shell.getDetailsOf($Shell.ParseName((Split-Path $OneDriveDocs -Leaf)),303)

    return $Status
}

$documents = [Environment]::GetFolderPath("MyDocuments")
$user = whoami

if (!($user.Contains("azuread"))) {
    Write-Output "non-azuread user"
    exit 0
}

if (!($documents.Contains("OneDrive -"))) {
    Write-Output "Document location error $documents"
    exit 1
}

if (!(Get-Process -Name "onedrive")) {
    Start-Process "C:\Program Files\Microsoft OneDrive\OneDrive.exe" -ArgumentList "/background"

    Start-Sleep -Seconds 600
}

if ((CheckSyncStatus -eq "Sync pending") -or (CheckSyncStatus -eq "Syncing")) {
    start-sleep 600
}

if ((CheckSyncStatus -eq "Always available on this device") -or (CheckSyncStatus -eq "Available when online")) {
    Write-Output "OneDrive status ok $Status"
    exit 0
} else {
    Write-Output "OneDrive status error $Status"
    exit 1
}

Don’t miss out on this related post! [PS SCRIPT]: Detecting Unlinked OneDrive SharePoint Document Libraries on Endpoints – Prof-IT Services

No responses yet

Leave a Reply

Your email address will not be published. Required fields are marked *