WMI查询的输出未正确显示

时间:2016-06-29 12:14:26

标签: powershell powershell-v2.0 powershell-v3.0 powershell-v4.0

我正在查询WMI类Win32_ShadowCopy以获取磁盘信息以检查多个内容。状态代码,上次快照等。我可以在本地计算机上运行此脚本,并返回正确的信息。一旦我使用包含服务器和磁盘的Import-Csv来检查shadowcopy运行状况,它就会返回错误的输出。

示例输入1:

Server  Drive
inms145     C
inms145     D
inms145     E
mission-1   C
mission-1   Q
mission-1   V
mission-1   W
mission-1   T
mission-1   X
mission-1   U
mission-1   Z

示例输出(错误):

ServerName Last_Snapshot                             Volume                                 Condition        SCDrive   LsDrives                  Status
INMS145 20160621135802.865078-240   \\?\Volume{022366e6-d8a5-49fc-8c82-263032d6c6a3}\   OK - Status Code: 12    E   C D E C Q V W T X U Z   Enabled Correctly on E
INMS145 20160621135802.865078-240   \\?\Volume{022366e6-d8a5-49fc-8c82-263032d6c6a3}\   OK - Status Code: 12    E   C D E C Q V W T X U Z   Enabled Correctly on E

示例输入2:

Server  Drive
inms145     C
inms145     D
inms145     E

示例输出(正确):

ServerName Last_Snapshot                              Volume                               Condition        SCDrive    LsDrives        Status
INMS145 20160621135802.865078-240   \\?\Volume{022366e6-d8a5-49fc-8c82-263032d6c6a3}\   OK - Status Code: 12     E       C D E     Enabled Correctly on E
INMS145        N/A                                       N/A                               NOT ENABLED        C D E      C D E     Not Enabled on C D E

脚本:

$CSVPath  = "\\SERVER\E$\Utilities\Scripts\Set_DashboardData\ShadowCopy\Servers_Drives.csv"
$rPath    = "\\SERVER\E$\Utilities\Scripts\Set_DashboardData\ShadowCopy\Results.csv"
$Pshell2F = "\\SERVER\E$\Utilities\Scripts\Set_DashboardData\ShadowCopy\export-csv-pshell.2.ps1"
$SCHash   = @()
$SCArray  = @()

if ((Get-ItemProperty -Path "HKLM:\SOFTWARE\Microsoft\Windows NT\CurrentVersion" -Name ProductName).ProductName -like "*Windows Server 2008*") {
    $Pshell2F | Out-Null
    $SC_Get = Import-Csv $CSVPath | ForEach-Object {
        Get-WmiObject -ComputerName $_.server -Class Win32_ShadowCopy | foreach {
          $_ | Select-Object Volumename,@{n="SCDrive";e={$vol08=$_.volumename;(gwmi -Class Win32_Volume | select deviceid | ? {$_.deviceid -eq $vol08}).DeviceID}}
        }
    }
} elseif ((Get-ItemProperty -Path "HKLM:\SOFTWARE\Microsoft\Windows NT\CurrentVersion" -Name ProductName).ProductName -like "*Windows Server 2012*") {
    $SC_Get = Import-Csv $CSVPath | ForEach-Object {
        Get-WmiObject -ComputerName $_.server -Class Win32_ShadowCopy | foreach {
            $_ | Select-Object  PSComputerName,InstallDate,Volumename,State,@{n="SCDrive";e={$vol12=$_.volumename;(Get-Volume | ? {$_.path -eq $vol12}).DriveLetter}},@{n="LsDrives";e={Import-Csv $CSVPath | select -ExpandProperty drive}}
        }
    }
}

$SC_Get | foreach {
    $props = @{
        ServerName      = $_.PSComputerName
        Last_Snapshot   = $_.InstallDate
        Volume          = $_.Volumename
        Condition       = $_.State
        SCDrive         = $_.SCDrive
        LsDrives        = $_.LsDrives
        Status          = $_.State
    }
    New-Object PsObject -Property $props
} | ForEach-Object {
    $SCHash = New-Object -TypeName PSObject -Property @{
        'ServerName'    = $_.ServerName
        'Last_Snapshot' = $_.Last_Snapshot
        'Volume'        = $_.Volume
        'Condition'     = $_.Condition
        'SCDrive'       = $_.SCDrive
        'LsDrives'      = $_.LsDrives
        'Status'        = $_.Status
    }
    #The altered data is put into the empty array created earlier and the select statement specifies the order to put it in. 
    $SCArray += $SCHash | Select-Object ServerName,Last_Snapshot,Volume,Condition,SCDrive,LsDrives,Status
}

$SC = $SCArray

$SCFilt = $SCArray | Select-Object -ExpandProperty LsDrives | Where-Object { $_ -notmatch $($SC | Select-Object -ExpandProperty SCDrive)} | select -Unique

$SCArray | ForEach-Object {
    if ($_.LsDrives -match $_.SCDrive -and $_.Last_Snapshot -like "") {
        $_.Status = "Enabled - No Snaps"
        $_.Condition = "NO SNAPSHOTS - Status Code: " + ($($_.Condition))
    }

    if ($_.LsDrives -match $_.SCDrive -and $_.Last_Snapshot -like "**" -and $_.Status -ne "12") {
        $_.Status = "Enabled-Snaps-State doesnt equal 12"
        $_.Condition = "NOT OK - Status Code: " + ($($_.Condition))
    }

    if ($_.LsDrives -match $_.SCDrive -and $_.Last_Snapshot -like "**" -and $_.Status -eq "12") {
        $_.Status = "Enabled Correctly on " + ($($_.SCDrive))
        $_.Condition = "OK - Status Code: " + ($($_.Condition))
    }
}

$SCArray | select -Unique | Export-Csv $rPath -NoTypeInformation -Force | Out-Null

$SCArray | ForEach-Object {
    if ($SCFilt) {
        $_.Status = "Not Enabled on " + ($($SCfilt))
        $_.Condition = "NOT ENABLED"
        $_.SCDrive = "$($SCFilt)"
        $_.Last_Snapshot = 'N/A'
        $_.Volume = 'N/A'
    }
}

$SCArray | select -Unique | Export-Csv $rPath -NoTypeInformation -Force -Append | Out-Null

如何让输出与列表中的下一个服务器相关联?它还组合了驱动器列,而不是运行来自其他服务器的信息。如果我运行$SCArray | select -Unique,它将只返回inms145,即使mission-01在该列表中。

0 个答案:

没有答案