Need help in Optimizing the PS command to extract network name resource of a Role running in Failover Cluster Manager

Surabhi Jaiswal 41 Reputation points
2021-01-29T18:46:23.973+00:00

I need help in optimizing the below PS cmdlet. The intent of the below cmdlet is to extract the name of the 'Network Name' of a particular "Generic Service" role running in the failover cluster.

------------------------

(Get-ClusterResource |
Where-Object {$.ResourceType.Name -eq "Network Name" -and
$.OwnerGroup -eq
(Get-ClusterResource |
Where-Object {$.ResourceType.Name -eq "Generic Service" -and $.Name -like "Rhapsody*"} ).OwnerGroup.Name}
).name

OUTPUT:
RhapsodyHA_AP

------------------------

Below is the list of resources in my test cluster for example. The highlighted row I need to extract.
61991-image.png

Thanks for the help in advance!!

-Surabhi

Windows Server Clustering
Windows Server Clustering
Windows Server: A family of Microsoft server operating systems that support enterprise-level management, data storage, applications, and communications.Clustering: The grouping of multiple servers in a way that allows them to appear to be a single unit to client computers on a network. Clustering is a means of increasing network capacity, providing live backup in case one of the servers fails, and improving data security.
963 questions
Windows Server PowerShell
Windows Server PowerShell
Windows Server: A family of Microsoft server operating systems that support enterprise-level management, data storage, applications, and communications.PowerShell: A family of Microsoft task automation and configuration management frameworks consisting of a command-line shell and associated scripting language.
5,415 questions
0 comments No comments
{count} votes

Accepted answer
  1. Rich Matheisen 45,186 Reputation points
    2021-01-29T20:40:39.22+00:00

    What sort of "optimization" do you think the script needs?

    Does untangling the code help? (I have no clusters to work with so I can't test this):

    $cl = Get-ClusterResource
    $ogn = ($cl |
            Where-Object {$_.ResourceType.Name -eq "Generic Service" -and $_.Name -like "Rhapsody*"}).OwnerGroup.Name
    
    $cl |
        Where-Object {$_.ResourceType.Name -eq "Network Name" -and $_.OwnerGroup -eq $ogn} |
                Select-Object -ExpandProperty Name
    

1 additional answer

Sort by: Most helpful
  1. Titan 206 Reputation points
    2021-01-30T05:29:12.563+00:00

    Hello @Surabhi Jaiswal !

    You have two complete itereations thru the ClusterResource collection, here is an approach with one complete iteration. Does this work for you?

    Get-ClusterResource |  
    Where-Object {  
      $_.ResourceType.Name -in "Generic Service","NetworkName" -and $_.OwnerGroup.Name -like 'Rhapsody*'  
    } | Where-Object ResourceType.Name -eq 'Networkname' | Select -ExpandProperty Name  
    

    I've tested it with this little code snippet:

    $o = `  
    [PSCustomObject]@{Name = 'Cluster Disk 1'; State = 'Online'; OwnerGroup = 'RhapsodyHA_AP'; ResourceType = 'Physical Disk'},  
    [PSCustomObject]@{Name = 'Cluster IP Address'; State = 'Online'; OwnerGroup = 'Cluster Group'; ResourceType = 'IP Address'},  
    [PSCustomObject]@{Name = 'Cluster Name'; State = 'Online'; OwnerGroup = 'Cluster Group'; ResourceType = 'NetworkName'},  
    [PSCustomObject]@{Name = 'HIF Testing'; State = 'Online'; OwnerGroup = 'HIF Testing'; ResourceType = 'NetworkName'},  
    [PSCustomObject]@{Name = 'IP Address 192.168.2.1'; State = 'Online'; OwnerGroup = 'RhapsodyHA_AP'; ResourceType = 'IP Address'},  
    [PSCustomObject]@{Name = 'IP Address 192.168.2.23'; State = 'Online'; OwnerGroup = 'HIF Testing'; ResourceType = 'IP Address'},  
    [PSCustomObject]@{Name = 'Philips Healthcare Integration Found'; State = 'Online'; OwnerGroup = 'HIF Testing'; ResourceType = 'Generic Service'},  
    [PSCustomObject]@{Name = 'Rhapsody 6.6.1'; State = 'Online'; OwnerGroup = 'RhapsodyHA_AP'; ResourceType = 'Generic Service'},  
    [PSCustomObject]@{Name = 'RhapsodyHA_AP'; State = 'Online'; OwnerGroup = 'RhapsodyHA_AP'; ResourceType = 'NetworkName'}  
      
    $o |  
    Where-Object {  
      $_.ResourceType -in "Generic Service","NetworkName" -and $_.OwnerGroup -like 'Rhapsody*'  
    } | Where-Object ResourceType -eq 'Networkname' | Select -ExpandProperty Name  
    

    Best whishes

    ---
    If this answer was helpful, accept and upvote it please.