2013-02-20 37 views
7

प्रदर्शन कारणों से, जहां भी संभव हो, संबंधित PowerCLI आदेशों के बजाय गेट-व्यू का उपयोग करने के लिए मैंने अपनी वीएमवेयर दैनिक रिपोर्ट को फिर से लिखना शुरू कर दिया है। इसके साथ एक मामूली असुविधा यह है कि दृश्य वस्तुओं को अक्सर कई गुण होते हैं, जिनमें से कई स्वयं वस्तुएं हैं। कुछ गुण चार या अधिक स्तरों को घोंसले में घिरे होते हैं।वस्तु के गुणों के माध्यम से पुनरावर्ती गणना कैसे करें?

तो मैं एक ऐसा फ़ंक्शन बनाने की कोशिश कर रहा हूं जो किसी ऑब्जेक्ट के सभी गुणों को उस संपत्ति के पूर्ण पथ के साथ आउटपुट करेगा। इसके बाद विशिष्ट गुणों को आसान बनाने के लिए, जहां ऑब्जेक्ट को पाइप किया जा सकता है। तो $ वी में जमा एक VMware.Vim.VirtualMachine वस्तु पर मेजबान से संबंधित एक संपत्ति को खोजने के लिए, मैं की तरह कुछ टाइप करेंगे:

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

और आदर्श, इस $ वी के सभी नेस्टेड संपत्तियों की एक सूची वापस होगा जो शब्द "होस्ट" है।

मैं यह कैसे कर सकता हूं?

उत्तर

11

शायद वहाँ यह करने के लिए एक आसान तरीका है, लेकिन यहाँ है कि मैं क्या के साथ आया है:

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 
} 

जब यह चल आदेश का उपयोग

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

यह रिटर्न

$_.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 

यह देखते हुए कि VMware.Vim.VirtualMachine ऑब्जेक्ट में 5087 नेस्टेड गुण हैं, यह फिन के लिए एक आसान तरीका है डी जो आप खोज रहे हैं। उम्मीद है कि यह किसी और की मदद कर सकता है।