2013-02-20 48 views
7

Tôi đã bắt đầu viết lại báo cáo VMware hàng ngày của mình để sử dụng Get-View, thay vì các lệnh PowerCLI liên quan ở bất kỳ nơi nào có thể, vì lý do hiệu suất. Một sự bất tiện nhỏ với điều này là các đối tượng xem được trả lại thường có nhiều thuộc tính, nhiều trong số đó là các đối tượng. Một số thuộc tính được lồng sâu từ bốn cấp trở lên.Làm thế nào để đệ quy liệt kê thông qua các thuộc tính của đối tượng?

Vì vậy, tôi đang cố tạo một hàm sẽ xuất tất cả các thuộc tính của một đối tượng, cùng với đường dẫn đầy đủ đến thuộc tính đó. Điều này sau đó có thể được chuyển đến Where-Object, để làm cho việc tìm kiếm các thuộc tính cụ thể dễ dàng hơn. Vì vậy, để tìm thấy một tài sản liên quan đến host trên một đối tượng VMware.Vim.VirtualMachine lưu trong $ v, tôi sẽ gõ cái gì đó như:

Get-Properties -Object $v | ? {$_ -match "Host"} 

Và lý tưởng, điều này sẽ trả về một danh sách tất cả các thuộc tính lồng nhau của $ v mà chứa từ "Máy chủ".

Tôi làm cách nào để thực hiện việc này?

Trả lời

11

Có lẽ đó là một cách dễ dàng hơn để làm điều này, nhưng đây là những gì tôi đã đưa ra:

function Get-Properties($Object, $MaxLevels="5", $PathName = "`$_", $Level=0) 
{ 
    <# 
     .SYNOPSIS 
     Returns a list of all properties of the input object 

     .DESCRIPTION 
     Recursively 

     .PARAMETER Object 
     Mandatory - The object to list properties of 

     .PARAMETER MaxLevels 
     Specifies how many levels deep to list 

     .PARAMETER PathName 
     Specifies the path name to use as the root. If not specified, all properties will start with "." 

     .PARAMETER Level 
     Specifies which level the function is currently processing. Should not be used manually. 

     .EXAMPLE 
     $v = Get-View -ViewType VirtualMachine -Filter @{"Name" = "MyVM"} 
     Get-Properties $v | ? {$_ -match "Host"} 

     .NOTES 
      FunctionName : 
      Created by : KevinD 
      Date Coded : 02/19/2013 12:54:52 
     .LINK 
      http://stackoverflow.com/users/1298933/kevind 
    #> 

    if ($Level -eq 0) 
    { 
     $oldErrorPreference = $ErrorActionPreference 
     $ErrorActionPreference = "SilentlyContinue" 
    } 

    #Initialize an array to store properties 
    $props = @() 

    # Get all properties of this level 
    $rootProps = $Object | Get-Member -ErrorAction SilentlyContinue | Where-Object { $_.MemberType -match "Property"} 

    # Add all properties from this level to the array. 
    $rootProps | ForEach-Object { $props += "$PathName.$($_.Name)" } 

    # Make sure we're not exceeding the MaxLevels 
    if ($Level -lt $MaxLevels) 
    { 

     # We don't care about the sub-properties of the following types: 
     $typesToExclude = "System.Boolean", "System.String", "System.Int32", "System.Char" 

     #Loop through the root properties 
     $props += $rootProps | ForEach-Object { 

        #Base name of property 
        $propName = $_.Name; 

        #Object to process 
        $obj = $($Object.$propName) 

        # Get the type, and only recurse into it if it is not one of our excluded types 
        $type = ($obj.GetType()).ToString() 

        # Only recurse if it's not of a type in our list 
        if (!($typesToExclude.Contains($type))) 
        { 

         #Path to property 
         $childPathName = "$PathName.$propName" 

         # Make sure it's not null, then recurse, incrementing $Level       
         if ($obj -ne $null) 
         { 
          Get-Properties -Object $obj -PathName $childPathName -Level ($Level + 1) -MaxLevels $MaxLevels } 
         } 
        } 
    } 

    if ($Level -eq 0) {$ErrorActionPreference = $oldErrorPreference} 
    $props 
} 

Khi chạy nó bằng cách sử dụng lệnh

Get-Properties -Object $v | ? {$_ -match "Host" } 

nó trả

$_.Capability.HostBasedReplicationSupported 
$_.Client.CertificateError.Method.DeclaringType.Assembly.HostContext 
$_.Client.CertificateError.Method.Module.Assembly.HostContext 
$_.Client.CertificateError.Method.ReflectedType.Assembly.HostContext 
$_.Client.CertificateError.Method.ReturnType.Assembly.HostContext 
$_.Client.ServiceContent.HostProfileManager 
$_.Client.ServiceContent.HostProfileManager 
$_.Client.ServiceContent.HostProfileManager.Type 
$_.Client.ServiceContent.HostProfileManager.Value 
$_.Config.Hardware.Device.Backing.HostPointingDevice 
$_.Config.Hardware.Device.Backing.HostPointingDevice 
$_.Config.Hardware.Device.Backing.HostPointingDevice 
$_.Config.Hardware.Device.Backing.HostPointingDevice 
$_.Config.Hardware.Device.Backing.HostPointingDevice 
$_.Config.Hardware.Device.Backing.HostPointingDevice 
$_.Config.Hardware.Device.Backing.HostPointingDevice 
$_.Config.Hardware.Device.Backing.HostPointingDevice 
$_.Config.Hardware.Device.Backing.HostPointingDevice 
$_.Config.Hardware.Device.Backing.HostPointingDevice 
$_.Config.Hardware.Device.Backing.HostPointingDevice 
$_.Config.Hardware.Device.Backing.HostPointingDevice 
$_.Config.Tools.SyncTimeWithHost 
$_.Guest.HostName 
$_.Guest.IpStack.DnsConfig.HostName 
$_.Guest.Net.DnsConfig.HostName 
$_.Runtime.Host 
$_.Runtime.Host 
$_.Runtime.Host.Type 
$_.Runtime.Host.Value 
$_.Summary.Guest.HostName 
$_.Summary.QuickStats.HostMemoryUsage 
$_.Summary.Runtime.Host 
$_.Summary.Runtime.Host 
$_.Summary.Runtime.Host.Type 
$_.Summary.Runtime.Host.Value 

Xét rằng đối tượng VMware.Vim.VirtualMachine có 5087 thuộc tính lồng nhau, đây là cách dễ dàng hơn nhiều để vây d những gì bạn đang tìm kiếm. Hy vọng rằng điều này có thể giúp người khác.

Các vấn đề liên quan