rodneyviana / ODSyncService

OneDrive service/DLL for Sync State
MIT License
93 stars 26 forks source link

Filtering Results #48

Open SynapseCarbon opened 4 months ago

SynapseCarbon commented 4 months ago

I was close to clicking "Submit New Issue" when I finally figured things out myself, but thought this might be a useful tip to save somebody else time.

I've been trying to filter the results from Get-OdStatus when multiple folders / sites are being sync'd by OneDrive and was puzzled why the usual PS where-object didn't work. I was trying:

Get-ODStatus | Where-Object { $_.Displayname -eq 'OneDrive - Personal' }

This would still return all five OneDrive instances on my laptop (I was expecting just my personal OD). I worked out that Get-OdStatus returns an array of objects and where-object doesn't go far enough "inside" to filter anything by a property.

I did get it working using the foreach loop below but thought that seemed a lot of code for a simple task, so kept on trying things.

$HomeDrive = 'OneDrive - Personal'
$ODStatusResults = Get-OdStatus
Foreach ( $result in $ODStatusResults ) 
{
    if ($result.DisplayName -eq $HomeDrive) {
        Write-Host "Found " $result.DisplayName " " $result.StatusString
    }
}

This post on Stack Overflow gave me enough of a clue that I needed to use SyncRoot property, expand that with ExpandProperty and then use where-object. The final ExpandProperty parameter returns StatusString as a String so you can use it in If statement.

Get-ODStatus | Select-Object -ExpandProperty SyncRoot | where-object {$_.DisplayName -eq 'OneDrive - Personal'} | Select-object -ExpandProperty StatusString