Powershell尝试和捕获循环

时间:2017-09-26 02:14:48

标签: powershell

我正在尝试为我们的ESX场中的Windows 7计算机提取所有C:\驱动器使用情况的报告。我的初始ps代码将提取所有Windows 7机器n到csv文件(下面的示例图片)。我想首先使用DNS名称查询磁盘空间,如果由于RPC错误而失败,则应该捕获错误并使用IP地址再次运行命令。这是因为有些情况是dns条目没有在我们的DNS上注册,并且在查询期间ESX也会看到169.x.x.x IP,然后首先通过DNS名称进行轮询。

问题在于,我无法使用catch命令捕获RPC故障。在下面的示例中,我故意用虚拟dns名称填充.csv文件,希望它能捕获错误并在catch部分执行命令。不幸的是,它似乎没有起作用。

这里的任何人都可以对这一点有所了解吗?

CSV File Sample

csv file

$Win7JumpboxList = import-csv 'D:\Win7DiskSpace\Win7PoweredOn.csv' |
ForEach-Object {
    try {
        Get-WmiObject -Class win32_volume -ComputerName $_.DnsName -Filter "drivetype=3 and DRIVELETTER = 'C:' " | Select-Object @{LABEL='Computer';EXPRESSION={$_.pscomputername}}, DriveLetter, @{LABEL="Capacity (GB)";e={[math]::round($_.Capacity/1GB, 2)}}, @{LABEL="FreeSpace (GB)";e={[math]::round($_.FreeSpace/1GB, 2)}},@{LABEL="FreeSpace (%)";e={[int]($_.Freespace*100/$_.Capacity)}} -ErrorAction Stop
    }
    catch [System.Runtime.InteropServices.COMException] {
        Get-WmiObject -Class win32_volume -ComputerName $_.IPAddress -Filter "drivetype=3 and DRIVELETTER = 'C:' " | Select-Object @{LABEL='Computer';EXPRESSION={$_.pscomputername}}, DriveLetter, @{LABEL="Capacity (GB)";e={[math]::round($_.Capacity/1GB, 2)}}, @{LABEL="FreeSpace (GB)";e={[math]::round($_.FreeSpace/1GB, 2)}},@{LABEL="FreeSpace (%)";e={[int]($_.Freespace*100/$_.Capacity)}}
    }
}

$Win7JumpboxList | Export-Csv D:\Win7DiskSpace\Win7DiskSpace.csv -NoTypeInformation -force

2 个答案:

答案 0 :(得分:1)

对Get-WmiObject cmdlet使用ErrorAction而不是Select-Object

try {
    Get-WmiObject -Class win32_volume -ComputerName $_.DnsName -Filter "drivetype=3 and DRIVELETTER = 'C:' " -ErrorAction Stop | Select-Object @{LABEL='Computer';EXPRESSION={$_.pscomputername}}, DriveLetter, @{LABEL="Capacity (GB)";e={[math]::round($_.Capacity/1GB, 2)}}, @{LABEL="FreeSpace (GB)";e={[math]::round($_.FreeSpace/1GB, 2)}},@{LABEL="FreeSpace (%)";e={[int]($_.Freespace*100/$_.Capacity)}} 
}
catch [System.Runtime.InteropServices.COMException] {
    Get-WmiObject -Class win32_volume -ComputerName $_.IPAddress -Filter "drivetype=3 and DRIVELETTER = 'C:' " | Select-Object @{LABEL='Computer';EXPRESSION={$_.pscomputername}}, DriveLetter, @{LABEL="Capacity (GB)";e={[math]::round($_.Capacity/1GB, 2)}}, @{LABEL="FreeSpace (GB)";e={[math]::round($_.FreeSpace/1GB, 2)}},@{LABEL="FreeSpace (%)";e={[int]($_.Freespace*100/$_.Capacity)}}
}

答案 1 :(得分:0)

改为使用PowerCLI:

C:> $vms = get-vm
C:> foreach ( $vm in $vms) {
C:> foreach ( $vmdisk in $vm.guest.Disks ) {
>>  $vm | select Name,
>>     @{n='hostname';e={$vm.guest.hostname}},
>>     @{n='IPAddress';e={$vm.guest.IPAddress}},
>>     @{n='Disk';e={$vmdisk.Path}},
>>     @{n='CapacityGB';e={$vmdisk.CapacityGB}},
>>     @{n='FreeSpaceGB';e={$vmdisk.FreeSpaceGB}}
>> }
>> }


Name        : somevm
hostname    : somevm.mydomain.org
IPAddress   : {192.168.130.141, 6c9e::9n5b:cbdb:6ce5:fb72}
Disk        : C:\
CapacityGB  : 98.533199310302734375
FreeSpaceGB : 57.00951385498046875

Name        : anothervm
hostname    : anothervm.mydomain.org
IPAddress   : {192.168.130.142, 6c9e::9n5b:cbdb:6ce5:fb73}
Disk        : C:\
CapacityGB  : 98.533199310302734375
FreeSpaceGB : 54.06373657644533