Creating default value for a variable

Welcome Forums General PowerShell Q&A Creating default value for a variable

This topic contains 2 replies, has 2 voices, and was last updated by

3 years, 7 months ago.

  • Author
  • #28210

    Points: 3
    Rank: Member

    I have a script that checks the resources of a Failover cluster. What I would like to have is a default value of the state set to offline to show offline resources if the parameter state is omitted. However what I am finding is that the way I have written is showing online resources even if the -parameter state is omitted

    My script is below:

    # ===========================================================================
    # Created with: SAPIEN Technologies, Inc., PowerShell Studio 2015 v4.2.89
    # Created on: 5/08/2015 11:59 a.m.
    # Created by: weiyentan
    # Organization:
    # Filename: Get-ClusterResourceState.ps1
    # ===========================================================================
    Get-ClusterResourceState -clustername 'Cluster1'
    This will show you the resources of the cluster Cluster1 that is listed OFFLINE
    PS C:\> Get-ClusterResourceState -clustername 'Cluster1' -state 'online'
    This will check the cluster Cluster1 and check all the resources that are online.

    [Parameter(Mandatory = $true,
    Position = 1)]
    [Parameter(Mandatory = $false,
    Position = 2)]
    [string]$state = 'offline'
    Import-Module FailoverClusters
    Get-Cluster $clustername | Get-ClusterResource | Where-Object {"$_.state -eq '$state'"}

    I think the problem that I am having is in this line: {"$_.state -eq '$state'"}

    But I am not quite sure how to make that variable have a default value of offline. It shows online resources.

    I have tried this line : {$_.state -eq '$state'} but that doesn't work either.

    Any help appreciated.

  • #28211

    Points: 0
    Rank: Member

    You've got some quirky quotation marks happening. It should look like this:

    Where-Object { $_.State -eq $state }
    # or
    Where-Object { $_.State -eq "$state" }

    In this case, the double quotes in the second example are irrelevant, since $state is a string and that's the whole value anyway.

  • #28212

    Points: 3
    Rank: Member

    Thanks for the reply Dave. That seems to have done the trick!

The topic ‘Creating default value for a variable’ is closed to new replies.

denizli escort samsun escort muğla escort ataşehir escort kuşadası escort