Powershell value

Sara 441 Reputation points
2023-05-17T13:19:15.22+00:00

I have a script where I am trying to get the status of the VIP and services and it returns the value as up, down, out of service as in the screenshot, but I want help with the script to be updated as, if the status is "down" to return value as 0, and if "up" as 1, and if "out of service" as 2.

User's image

<#
    .Synopsis
        This script will obtain status od vip's and services from exsre owned netscalers and send to graphite
#>

$Modules = @(
    "SecretServer"
    "Netscaler"
    "automation"
)

foreach ($Module in $Modules)
{
    if (-not(Get-Module $Module))
    {
        Import-AHModule $Module
    }
}

#Obtaining Credentials from Secret server
Write-verbose "$(Get-date): Creating Secret Server Login"
$SecurePassword = ConvertTo-SecureString $env:WinPassword -AsPlainText -Force -ErrorAction Stop
$SSCred = New-Object System.Management.Automation.PSCredential ($env:WinUserName,$SecurePassword) -ErrorAction Stop

$NScred = Get-SecretServerCredential -SamAccountName "nsex" -OperatorCredentials $SSCred -ErrorAction Stop -Verbose:$false
$Netscalers = Get-NSExsrePrimary | select-object -ExpandProperty DNSName

$RunDate = Get-Date

Foreach ($Netscaler in $Netscalers)
{
$null = Connect-NetScaler -hostname $Netscaler -Credential $NSCred 
    
    $VIPs = Get-NSStat -type lbvserver  
    $services = Get-NSStat -type service 

    Foreach ($vip in $vips){

    #VIP status
    $VIP | Select-Object @{Name="Name";e={"metrics.winops.vip.$($vip.name).status.2m.vipstate"}},
        @{Name="Value";Expression={$_.state}},
        @{Name="Date";Expression={$RunDate}}

   Foreach ($service in $services){
    # service status
    $Service | Select-Object @{Name="Name";e={"metrics.winops.service.$($service.name).status.2m.servicestate"}},
        @{Name="Value";Expression={$_.state}},
        @{Name="Date";Expression={$RunDate}}
   } 

  }
}
Windows for business Windows Server User experience PowerShell
0 comments No comments
{count} votes

Accepted answer
  1. Rich Matheisen 47,901 Reputation points
    2023-05-17T21:30:38.0133333+00:00

    Here's a pretty straightforward way to do that by using a hash and changing the calculated value in your Select-Object cmdlets:

    $States = @{
        down                = 0
        up                  = 1
        'out of service'    = 2
    }
    $x = [PSCustomObject]@{name = 'ralph';state = 'down'},
         [PSCustomObject]@{name = 'george';state = 'up'},
         [PSCustomObject]@{name = 'frank';state = 'out of service'}
    
    $x |
        ForEach-Object{
            $_ | Select-Object name, @{n='Value';e={$States.($_.state)}}
        }
    

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.