Powershell Where-Object包含枚举数组

时间:2015-05-08 09:27:36

标签: powershell nlb

我希望来自file_get_contents(htmlspecialchars_decode($url));的项目包含州Get-NlbClusterNode"Converged"的枚举)

我尝试过类似的东西:

[Microsoft.NetworkLoadBalancingClusters.PowerShell.NodeStatusCode]

但这让我只匹配状态

我尝试了Get-NlbClusterNode | %{ $_.State | ?{$_.ToString() -eq "Converged"} } 运算符

contains

但这并不适用于以太。

这是PowerShell-Object

Get-NlbClusterNode | ?{ $_.State -ccontains "Converged"}
# or
Get-NlbClusterNode | ?{ $_.State -ccontains [Microsoft.NetworkLoadBalancingClusters.PowerShell.NodeStatusCode]::Converged}

这是类型<Objs Version="1.1.0.1" xmlns="http://schemas.microsoft.com/powershell/2004/04"> <Obj RefId="0"> <TN RefId="0"> <T>System.Collections.Generic.List`1[[Microsoft.NetworkLoadBalancingClusters.PowerShell.Node, Microsoft.NetworkLoadBalancingClusters.PowerShell, Version=6.3.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35]]</T> <T>System.Object</T> </TN> <LST> <Obj RefId="1"> <TN RefId="1"> <T>Microsoft.NetworkLoadBalancingClusters.PowerShell.Node</T> <T>System.Object</T> </TN> <ToString>Server02</ToString> <Props> <Obj N="Cluster" RefId="2"> <TN RefId="2"> <T>Microsoft.NetworkLoadBalancingClusters.PowerShell.Cluster</T> <T>System.Object</T> </TN> <ToString /> <Props> <S N="Name"></S> <S N="ClusterIPAddress">192.168.0.20</S> <S N="ClusterNetworkMask">255.255.255.0</S> <S N="ClusterMacAddress">03-12-c0-a8-56-ac</S> <S N="OperationMode">MULTICAST</S> <B N="BdaReverseHash">false</B> <B N="BdaTeamActive">false</B> <Nil N="BdaTeamId" /> <B N="BdaTeamMaster">false</B> </Props> <MS> <S N="ClusterName"></S> <S N="IPAddress">192.168.0.20</S> </MS> </Obj> <S N="Name">Server02</S> <S N="InterfaceName">NLB</S> <S N="Host">Server02</S> <Obj N="State" RefId="3"> <TN RefId="3"> <T>Microsoft.NetworkLoadBalancingClusters.PowerShell.NodeStatus</T> <T>System.Object</T> </TN> <ToString>Converged</ToString> <Props> <S N="NodeStatusCode">Converged</S> </Props> </Obj> <I32 N="HostPriority">4</I32> <S N="AdapterGuid">{8FEAE540-F32E-42D0-940B-6E34E5977E77}</S> <Obj N="InitialHostState" RefId="4"> <TN RefId="4"> <T>Microsoft.NetworkLoadBalancingClusters.PowerShell.NodeInitialHostState</T> <T>System.Enum</T> <T>System.ValueType</T> <T>System.Object</T> </TN> <ToString>Started</ToString> <I32>1</I32> </Obj> <B N="PersistSuspendOnReboot">false</B> <B N="MaskSourceMac">true</B> <I32 N="FilterIcmp">0</I32> <I32 N="GreDescriptorTimeout">10</I32> </Props> </Obj> <Obj RefId="5"> <TNRef RefId="1" /> <ToString>Server01</ToString> <Props> <Ref N="Cluster" RefId="2" /> <S N="Name">Server01</S> <S N="InterfaceName">NLB</S> <S N="Host">Server01</S> <Obj N="State" RefId="6"> <TNRef RefId="3" /> <ToString>Converged(default)</ToString> <Props> <S N="NodeStatusCode">Default</S> </Props> </Obj> <I32 N="HostPriority">3</I32> <S N="AdapterGuid">{B47F1065-2E61-49A9-BFBB-E172EA9444E6}</S> <Obj N="InitialHostState" RefId="7"> <TNRef RefId="4" /> <ToString>Started</ToString> <I32>1</I32> </Obj> <B N="PersistSuspendOnReboot">false</B> <B N="MaskSourceMac">true</B> <I32 N="FilterIcmp">0</I32> <I32 N="GreDescriptorTimeout">10</I32> </Props> </Obj> </LST> </Obj> </Objs>

Microsoft.NetworkLoadBalancingClusters.PowerShell.Node

3 个答案:

答案 0 :(得分:1)

我使用以下内容:

$objClusterNode = Get-NlbClusterNode $env:COMPUTERNAME
[string]$strStatus = $objClusterNode | select -expand state 
if ($strStatus -ne "Converged") {Do stuff}

像魅力一样!

答案 1 :(得分:0)

所以我也一直在研究这个,并提出以下建议。请注意,我的节点中有2台服务器,因此[0]返回1009代码,即“融合(默认)”[1]返回,代码1008为融合。

 if ((Get-NlbClusterNode -HostName <Name>).state[0].NodeStatusCode.value__ -ne "1008") {write-host "not 1008"} else {Write-Host "it is 1009"}

如果您只是文本输出,这也可以很快获得信息。我正在讨论如何自己处理不同的代码#s。

(Get-NlbClusterNode -HostName BN1WUSSQL6LA331).state   

答案 2 :(得分:0)

Get-NlbClusterNode | ?{$_.State.NodeStatusCode -eq [Microsoft.NetworkLoadBalancingClusters.PowerShell.NodeStatusCode]::Converged}

Get-NlbClusterNode | ?{$_.State.NodeStatusCode -eq "Converged"}

因为State属性具有另一个属性NodeStatusCode

PS C:\Windows\system32> Get-NlbClusterNode | %{$_.State | Get-Member}


   TypeName: Microsoft.NetworkLoadBalancingClusters.PowerShell.NodeStatus

Name           MemberType Definition
----           ---------- ----------
Equals         Method     bool Equals(System.Object obj)
GetHashCode    Method     int GetHashCode()
GetType        Method     type GetType()
ToString       Method     string ToString()
NodeStatusCode Property   Microsoft.NetworkLoadBalancingClusters.PowerShell.NodeStatusCode NodeStatusCode {get;set;}