比较对象返回意外输出

Compare-Object returning unexpected output

我创建了一个小函数,可以将 Get-Volume 捕获到本地文件中。下次函数运行时,它会将新 Get-Volume 的输出与先前保存到文件系统的输出进行比较。

此函数非常适合服务,但奇怪的是返回一个卷 'different',尽管我们从输出中可以看出它不是。

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
    }
}

我的预期结果是属性的所有 returns as equal/unchanged (==) as none 都发生了变化,如下面的输出中清楚所示。然而出于某种原因,Compare-Object 添加的 SideIndicator 属性 表示标记为 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      : <=

奇怪的是 DriveLetter 属性 比较错误
没有卷的卷(如恢复分区)。

大概你必须包含一个 Select-Object 并计算 属性
它还检查DriveLetter [string]::IsNullOrEmpty()
避免将 $Null 与 Export-Csv

的字符串化输出 "" 进行比较

您的脚本,有点精简:

## Q:\Test18\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