我試圖獲取Windows系統上所有驅動器的加密狀態,並在自定義格式輸出中對該列表進行排序。我需要這個,因爲輸出到一個Nagios服務器;它會混淆Get-BitLockerVolume
的標準輸出格式並且太長。PowerShell:爲自定義輸出排序驅動器加密狀態
這是我到目前爲止。我試圖以這樣一種方式對輸出進行排序,即首先列出系統驅動器並給出安裝點(驅動器盤符)以及百分比。
[array]$DriveTypes = Get-BitLockerVolume | Sort-Object VolumeType | Select-Object VolumeType
[array]$DriveMounts = Get-BitLockerVolume | Sort-Object VolumeType | Select-Object MountPoint
[array]$WDEPercent = Get-BitLockerVolume | Sort-Object VolumeType | Select-Object EncryptionPercentage
for ($i = 0; $i -lt $DriveTypes.Count; $i++) {
if ($DriveIndex -eq $DriveTypes.Count) {
$TextDriveListing = $TextDriveListing + $DriveMounts.MountPoint+" ("+$DriveTypes.VolumeType+") at "+$WDEPercent.EncryptionPercentage+"%."
}
else {
$TextDriveListing = $TextDriveListing + $DriveMounts.MountPoint+" ("+$DriveTypes.VolumeType+") at "+$WDEPercent.EncryptionPercentage+"%, "
}
if ($WDEPercent.EncryptionPercentage -lt $ReqValue) {
$NoEncryptFlag = 1
}
}
我期望的輸出,例如,是這樣的:
C: (OperatingSystem) at 100%, D: (Data) at 0%.
我實際上最終是這樣的:
C: D: (OperatingSystem Data) at 100 0%, C: D: (OperatingSystem Data) at 100 0%,
我曾嘗試一些東西的答案派生來「 How to sort a Multi Dimensional Array in Powershell「來測試它,註釋掉我前面提到的for
區塊並放入:
$ListDrives | ForEach-Object {
Get-BitLockerVolume @{
MountPoint = $_[0]
EncryptionPercentage = $_[1]
}
} | Sort-Object VolumeType
Write-Host $ListDrives
那吐出這個錯誤:
Cannot index into a null array. At C:****************.ps1:142 char:3
- Get-BitLockerVolume @{
- ~~~~~~~~~~~~~~~~~~~~~~
- CategoryInfo : InvalidOperation: (:) [], RuntimeException
- FullyQualifiedErrorId : NullArray
我在做什麼錯?有什麼建議麼?
非常感謝!
難道你不想在'for'循環中使用'$ i'嗎? –