我创建了一个小函数,将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 : <=
奇怪的是,DriveLetter属性比较错误 没有卷的卷(如恢复分区)。
据推测,您必须包含具有计算属性的Select-Object
这也检查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