none
Powershell IF statement Help RRS feed

  • Question

  • Hi,

    I have a cluster, and would like to run a script based on the state of one of the nodes.

    if i run the following command it will provide me with the output below

    Get-ClusterNode -Cluster MY-CLUS1
    
    Name                 ID    State                                                                                                             
    ----                 --    -----                                                                                                             
    Node1             2     Down                                                                                                              
    Node2             1     Up    

    what I would like to do is specifically this:

    If node1 State is equal to Down, then do some action.

    but I don't know how to get the state value for that specific node from that get-cluster command

    Thanks

    Steve

    Friday, November 7, 2014 12:16 PM

Answers

  • I dont have the cluster cmdlets , but lets compare with Get-Service:

    Get-Service win*
    
    
    Status   Name               DisplayName
    ------   ----               -----------
    Stopped  WinHttpAutoProx... WinHTTP Web Proxy Auto-Discovery Se...
    Running  Winmgmt            Windows Management Instrumentation
    Running  WinRM              Windows Remote Management (WS-Manag...

    Now have a look at the properties of the result:

    PS C:\Users\a023114.ds> Get-Service win* | gm TypeName: System.ServiceProcess.ServiceController Name MemberType Definition ---- ---------- ---------- Name AliasProperty Name = ServiceName
    ... DisplayName Property System.String DisplayName {get;set;}
    ... Status Property System.ServiceProcess.ServiceControllerStatus Status {get;}


    Then let's confirm that we can use the properties:

    PS C:\> Get-Service | where { $_.Name -eq "WinRM" } | foreach { Write-Host "Status =" $_.Status " // Name =" $_.Name }
    
    Status = Running  // Name = WinRM

    So lets try this as a first step:

    Get-ClusterNode -Cluster MY-CLUS1 | foreach { 
        if ( ($_.Name -eq "Node1") -and ($_.State -eq "Down") ) { 
            #do something 
        }  
    }
    

    When searching for  Get-ClusterNote  on TechNet, you will find http://technet.microsoft.com/en-us/library/ee460990.aspx . Here you can see that you can specify also the node name:

    $CluNode = Get-ClusterNode -Cluster MY-CLUS1 -Node Node1 if ($CluNode.State -eq "Down") { #do something }

    • Proposed as answer by bmwinstead Friday, November 7, 2014 2:00 PM
    • Marked as answer by Milkientia Monday, November 10, 2014 10:58 AM
    Friday, November 7, 2014 1:55 PM

All replies

  • I dont have the cluster cmdlets , but lets compare with Get-Service:

    Get-Service win*
    
    
    Status   Name               DisplayName
    ------   ----               -----------
    Stopped  WinHttpAutoProx... WinHTTP Web Proxy Auto-Discovery Se...
    Running  Winmgmt            Windows Management Instrumentation
    Running  WinRM              Windows Remote Management (WS-Manag...

    Now have a look at the properties of the result:

    PS C:\Users\a023114.ds> Get-Service win* | gm TypeName: System.ServiceProcess.ServiceController Name MemberType Definition ---- ---------- ---------- Name AliasProperty Name = ServiceName
    ... DisplayName Property System.String DisplayName {get;set;}
    ... Status Property System.ServiceProcess.ServiceControllerStatus Status {get;}


    Then let's confirm that we can use the properties:

    PS C:\> Get-Service | where { $_.Name -eq "WinRM" } | foreach { Write-Host "Status =" $_.Status " // Name =" $_.Name }
    
    Status = Running  // Name = WinRM

    So lets try this as a first step:

    Get-ClusterNode -Cluster MY-CLUS1 | foreach { 
        if ( ($_.Name -eq "Node1") -and ($_.State -eq "Down") ) { 
            #do something 
        }  
    }
    

    When searching for  Get-ClusterNote  on TechNet, you will find http://technet.microsoft.com/en-us/library/ee460990.aspx . Here you can see that you can specify also the node name:

    $CluNode = Get-ClusterNode -Cluster MY-CLUS1 -Node Node1 if ($CluNode.State -eq "Down") { #do something }

    • Proposed as answer by bmwinstead Friday, November 7, 2014 2:00 PM
    • Marked as answer by Milkientia Monday, November 10, 2014 10:58 AM
    Friday, November 7, 2014 1:55 PM
  • Thanks for this, this worked perfectly.

    Steve

    Monday, November 10, 2014 8:22 AM