Arbiter
Arbiter

Reputation: 470

Compare-Object returning unexpected output

I have created a small function that captures Get-Volume into a local file. The next time the function runs, it compares the output of a fresh Get-Volume with the one previously saved to the file system.

This function works perfectly for services but is strangely returning a volume as 'different' even though we can see from the output it is not.

function Compare-Volumes{
    $Path = "$Env:PROGRAMDATA\VACS\states\"
    $File = "volumes.csv"
    $Volumes = Get-Volume | Select-Object OperationalStatus, HealthStatus, DriveType, FileSystemType, DedupMode, UniqueId, AllocationUnitSize, DriveLetter, FileSystem, FileSystemLabel, Size

    if (![System.IO.File]::Exists($Path+$File)){
        $Volumes | Export-CSV -Path $Path$File -Force
    }else{
        # Load file to object, get differences, submit to API, replace previous snapshot in file with new one
        $Snapshot = Import-CSV -Path "$Path$File"
        $StatusChanges = Compare-Object -ReferenceObject ($Snapshot) -DifferenceObject ($Volumes) -Property OperationalStatus, HealthStatus, DriveType, FileSystemType, DedupMode, UniqueId, AllocationUnitSize, DriveLetter, FileSystem, FileSystemLabel, Size -IncludeEqual
        $StatusChanges
        $Volumes | Export-CSV -Path $Path$File -Force
    }
}

My expected results are that everything returns as equal/unchanged (==) as none of the properties change, as is clear in the output below. Yet for some reason, the SideIndicator property added by Compare-Object is indicating value differences for the volume labelled Recovery.

OperationalStatus  : Unknown
HealthStatus       : Healthy
DriveType          : CD-ROM
FileSystemType     : Unknown
DedupMode          : Disabled
UniqueId           : \\?\Volume{2b4803c9-1ebe-11e6-9bed-005056c00008}\
AllocationUnitSize : 0
DriveLetter        : E
FileSystem         : 
FileSystemLabel    : 
Size               : 0
SideIndicator      : ==

OperationalStatus  : OK
HealthStatus       : Healthy
DriveType          : Fixed
FileSystemType     : NTFS
DedupMode          : NotAvailable
UniqueId           : \\?\Volume{f688d14f-0ee7-11e5-b210-806e6f6e6963}\
AllocationUnitSize : 4096
DriveLetter        : C
FileSystem         : NTFS
FileSystemLabel    : Windows
Size               : 953903214592
SideIndicator      : ==

OperationalStatus  : Unknown
HealthStatus       : Healthy
DriveType          : CD-ROM
FileSystemType     : Unknown
DedupMode          : Disabled
UniqueId           : \\?\Volume{f688d152-0ee7-11e5-b210-806e6f6e6963}\
AllocationUnitSize : 0
DriveLetter        : D
FileSystem         : 
FileSystemLabel    : 
Size               : 0
SideIndicator      : ==

OperationalStatus  : OK
HealthStatus       : Healthy
DriveType          : Fixed
FileSystemType     : NTFS
DedupMode          : NotAvailable
UniqueId           : \\?\Volume{f688d14e-0ee7-11e5-b210-806e6f6e6963}\
AllocationUnitSize : 4096
DriveLetter        : 
FileSystem         : NTFS
FileSystemLabel    : Recovery
Size               : 6291451904
SideIndicator      : =>

OperationalStatus  : OK
HealthStatus       : Healthy
DriveType          : Fixed
FileSystemType     : NTFS
DedupMode          : NotAvailable
UniqueId           : \\?\Volume{f688d14e-0ee7-11e5-b210-806e6f6e6963}\
AllocationUnitSize : 4096
DriveLetter        : 
FileSystem         : NTFS
FileSystemLabel    : Recovery
Size               : 6291451904
SideIndicator      : <=

Upvotes: 1

Views: 105

Answers (1)

user6811411
user6811411

Reputation:

Strangely it is the DriveLetter property which compares falsely
with volumes which doesn't have one (like the recovery partition).

Presumably you'll have to include a Select-Object with a calculated property
which also checks the DriveLetter [string]::IsNullOrEmpty()
to avoid comparing $Null with the stringified output "" of Export-Csv

Your script, a bit streamlined:

## Q:\Test\2018\12\31\SO_53990220.ps1

function Compare-Volumes{
    $FilePath = Join-Path "$Env:PROGRAMDATA\VACS\states\" "volumes.csv"

    $Volumes = Get-Volume | Select-Object OperationalStatus,HealthStatus,DriveType,
        FileSystemType, DedupMode,UniqueId,AllocationUnitSize,FileSystemLabel,FileSystem,Size,
        @{n='DriveLetter';e={if([string]::IsNullOrEmpty($_.DriveLetter)){""}else{$_.DriveLetter}}}

    if (Test-Path $FilePath){
        # Load file to object, get differences, submit to API, replace previous snapshot in file with new one
        $Snapshot = Import-CSV -Path $FilePath
        $StatusChanges = Compare-Object -ReferenceObject ($Snapshot) -DifferenceObject ($Volumes) `
          -IncludeEqual -Property OperationalStatus,HealthStatus,DriveType,FileSystemType,
          DedupMode,UniqueId,AllocationUnitSize,FileSystemLabel,FileSystem,Size,
          DriveLetter
        $StatusChanges
    }
    $Volumes | Export-CSV -Path $FilePath -Force -NoTypeInformation
}

Compare-Volumes

Upvotes: 1

Related Questions