比较对象返回意外输出

时间:2018-12-31 18:04:49

标签: powershell compareobject

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

此功能非常适合服务,但奇怪的是,即使我们从输出中看到的不是,它也将卷返回为“不同”。

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

我的预期结果是,由于所有属性均未更改,因此所有内容返回的均等/不变(==),如下面的输出所示。但是由于某些原因,由SideIndicator添加的Compare-Object属性指示标记为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      : <=

1 个答案:

答案 0 :(得分:1)

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

大概必须包含一个具有计算属性的选择对象
还会检查DriveLetter [string]::IsNullOrEmpty()
避免将$ Null与Export-Csv的字符串化输出""进行比较

您的脚本有点简化:

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