如何在Powershell中找到对象的“子属性”?

时间:2018-10-28 13:22:19

标签: powershell properties powercli

我想我只是不知道我要寻找的什么名字,但这可能是一千次问到的。

我不久前启动了PowerShell,我一直在努力了解在哪里可以找到对象的“子属性”(如果可以将其称为)。

例如,我使用的是VMware PowerCLI,并试图弄清楚如何找到VM的IP地址。

例如,我使用了Get-VM命令,并将其通过管道传递给get成员时,得到了以下内容:

PS C:\Users\eitan.rapaport> get-vm "*VRA*" | gm


   TypeName: VMware.VimAutomation.ViCore.Impl.V1.VM.UniversalVirtualMachineImpl

Name                    MemberType Definition
----                    ---------- ----------
ConvertToVersion        Method     T VersionedObjectInterop.ConvertToVersion[T]()
Equals                  Method     bool Equals(System.Object obj)
GetClient               Method     VMware.VimAutomation.ViCore.Interop.V1.VIAutomation VIObjectCoreInterop.GetClient()
GetConnectionParameters Method     VMware.VimAutomation.ViCore.Interop.V1.VM.RemoteConsoleVMParams RemoteConsoleVMIn..
GetHashCode             Method     int GetHashCode()
GetType                 Method     type GetType()
IsConvertableTo         Method     bool VersionedObjectInterop.IsConvertableTo(type type)
LockUpdates             Method     void ExtensionData.LockUpdates()
ObtainExportLease       Method     VMware.Vim.ManagedObjectReference ObtainExportLease.ObtainExportLease()
ToString                Method     string ToString()
UnlockUpdates           Method     void ExtensionData.UnlockUpdates()
CoresPerSocket          Property   int CoresPerSocket {get;}
CustomFields            Property   System.Collections.Generic.IDictionary[string,string] CustomFields {get;}
DatastoreIdList         Property   string[] DatastoreIdList {get;}
DrsAutomationLevel      Property   System.Nullable[VMware.VimAutomation.ViCore.Types.V1.Cluster.DrsAutomationLevel] ..
ExtensionData           Property   System.Object ExtensionData {get;}
Folder                  Property   VMware.VimAutomation.ViCore.Types.V1.Inventory.Folder Folder {get;}
FolderId                Property   string FolderId {get;}
Guest                   Property   VMware.VimAutomation.ViCore.Types.V1.VM.Guest.VMGuest Guest {get;}
GuestId                 Property   string GuestId {get;}
HAIsolationResponse     Property   System.Nullable[VMware.VimAutomation.ViCore.Types.V1.Cluster.HAIsolationResponse]..
HardwareVersion         Property   string HardwareVersion {get;}
HARestartPriority       Property   System.Nullable[VMware.VimAutomation.ViCore.Types.V1.Cluster.HARestartPriority] H..
Id                      Property   string Id {get;}
MemoryGB                Property   decimal MemoryGB {get;}
MemoryMB                Property   decimal MemoryMB {get;}
Name                    Property   string Name {get;}
Notes                   Property   string Notes {get;}
NumCpu                  Property   int NumCpu {get;}
PersistentId            Property   string PersistentId {get;}
PowerState              Property   VMware.VimAutomation.ViCore.Types.V1.Inventory.PowerState PowerState {get;}
ProvisionedSpaceGB      Property   decimal ProvisionedSpaceGB {get;}
ResourcePool            Property   VMware.VimAutomation.ViCore.Types.V1.Inventory.ResourcePool ResourcePool {get;}
ResourcePoolId          Property   string ResourcePoolId {get;}
Uid                     Property   string Uid {get;}
UsedSpaceGB             Property   decimal UsedSpaceGB {get;}
VApp                    Property   VMware.VimAutomation.ViCore.Types.V1.Inventory.VApp VApp {get;}
Version                 Property   VMware.VimAutomation.ViCore.Types.V1.VM.VMVersion Version {get;}
VMHost                  Property   VMware.VimAutomation.ViCore.Types.V1.Inventory.VMHost VMHost {get;}
VMHostId                Property   string VMHostId {get;}
VMResourceConfiguration Property   VMware.VimAutomation.ViCore.Types.V1.VM.VMResourceConfiguration VMResourceConfigu..
VMSwapfilePolicy        Property   System.Nullable[VMware.VimAutomation.ViCore.Types.V1.VMSwapfilePolicy] VMSwapfile..

如您所见,没有任何关于IP的内容。

我正在在线研究此问题,发现我应该运行以下命令:

Get-VM | Select Name, @{N="IP Address";E={@($_.guest.IPAddress[0])}}

哪个使我想到了我的问题。如何在命令的特定成员/属性下找到属性?在该示例中,我如何研究“来宾”的“子属性”?

3 个答案:

答案 0 :(得分:3)

好的,找到了。

在这种情况下,它将是get-vm "*VRA*" | select -ExpandProperty guest | gm

答案 1 :(得分:0)

PowerShell中introspection的另一种方法是只获取类名称,然后在API中查找它。

PS C:\> $Files = Get-ChildItem *.* -File
PS C:\> $Files[0].GetType().FullName
System.IO.FileInfo

然后您可以使用C#或PowerShell搜索该类以找到API listing

您可以使用the VMWare PowerCLI doc做类似的事情。左侧的“所有类型”列表是模块使用的所有类的列表。

答案 2 :(得分:0)

最近两天我一直在处理这个问题,所以我想我会分享我的解决方案:

首先,我将VM对象保存为一个变量,以使其易于访问而无需每次搜索。

$vm = Get-vm -Name steve

获取VM对象的所有属性

$vm | Select-Object *

如果只想获取名称,则可以这样访问它:

$vm.Name

如果要获取Guest对象的属性(这是VM对象的属性):

$vm.Guest | Select-Object *

您可以通过VM对象访问来宾对象的属性,如下所示:

$vm.Guest.VmName

想要访客的IP地址吗?

$vmip = $vm.Guest.IPAddress

而且,因为这花了我一段时间才能弄清楚,如果返回的是ipv4和ipv6,而您想过滤出ipv6:

$vmip = $vm.Guest.IPAddress  | ?{$_ -notmatch ':'}