Share via

Powershell parameters

Sara 441 Reputation points
2023-06-21T11:55:51.97+00:00

I have the following function and I am trying to define some parameters, particularly $vipstatus and $netscalername to see outputs only based out of it when needed, but when I run

Get-NSVIPStatus $vipstatus

it displays the entire result instead of the output that is stored in that variable,what am I missing?

function Get-NSVIPStatus
{
param(
    [parameter(Mandatory=$false)]
    [string]$vipstatus
)


$Components = @("virtualserver")

$VIPstatus = New-Object System.Collections.ArrayList($null)

foreach ($component in $Components) 
{
    $Metrics = Invoke-RestMethod "https://graphvip100.com/metrics/find?query=metrics.winops.netscalers.components.nsexsre*.$component.*.2m.status" -ErrorAction Stop
    $Raw = foreach ($metric in $Metrics) 
    {
        Invoke-RestMethod "https://graphvip100.com/render?target=$($metric.id)&from=-10min&format=csv" -ErrorAction Stop
    }

    $Data = $Raw | ConvertFrom-Csv -Header "Metric","Time","Value" | Where-Object { -not [string]::IsNullOrEmpty($_.Value) } | Group-Object -Property Metric

    foreach ($group in $Data) 
    {
        $NetscalerName = $group.Name.Split(".")[4]
        $Componentname = $group.name.split(".")[5]
        $Name          = $group.Name.Split(".")[6]
        
        If (($group.Group[-1].Value -eq "0.0") 
        {
            $vipdown = [PSCustomObject]@{
                NetscalerName = $NetscalerName
                Componentname = $Componentname
                Name = $Name
        }
    
    foreach ($vip in $vipdown)
    {
        $null = $VIPstatus.Add($vipdown)
        write-warning "$vipdown is down"
    }      
            
     $failures = $true
        }
        ElseIf (($group.Group[-1].Value -eq "1.0") 
        {
            Write-Verbose "$NetscalerName $Componentname $Name is UP" -Verbose
        }
        ElseIf (($group.Group[-1].Value -eq "2.0")
        {
            Write-Verbose "$NetscalerName $Componentname $Name is OUT OF SERVICE" -Verbose
        }
    }
}
Windows for business | Windows Server | User experience | PowerShell

1 answer

Sort by: Most helpful
  1. Limitless Technology 45,241 Reputation points
    2023-06-22T12:49:01.1733333+00:00

    Hello Sara,

    Thank you for your question and for reaching out with your question today.

    In your code, it seems that you are passing the variable $vipstatus directly to the Get-NSVIPStatus function. However, you need to pass the value of the variable instead of the variable itself.

    To achieve this, you can modify your code as follows:

    
    Get-NSVIPStatus -vipstatus $vipstatus
    
    

    By using the -vipstatus parameter and passing the value stored in the $vipstatus variable, you can ensure that only the specific output based on that parameter is displayed.

    Also, please note that in your function, you are using the $vipstatus parameter in the script block. If you want to filter the results based on the value of the $vipstatus parameter, you need to modify your code accordingly within the foreach loop or wherever you intend to apply the filter.

    I used AI provided by ChatGPT to formulate part of this response. I have verified that the information is accurate before sharing it with you.

    If the reply was helpful, please don’t forget to upvote or accept as answer.

    Was this answer helpful?


Your answer

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