Help with function to check multiple inputs and return multiple results

This topic contains 4 replies, has 2 voices, and was last updated by Profile photo of Gareth Edwards Gareth Edwards 2 years, 1 month ago.

  • Author
    Posts
  • #20265
    Profile photo of Gareth Edwards
    Gareth Edwards
    Participant

    Hi,

    First post and hope I could get some help.
    I am very new to PS and I have been doing my best to search for these answers before coming here.
    After watching MVA jump start videos, I believe this may be the best place for some help.

    So the problem >

    I am wanting to query several Stratus servers for the OperationalState of the cpu's and ilo cards.
    Get the returned state and "convert" this into a "understandable output" and then write this to the host.
    Right now I would love some help with the correct function to check my 4 returned values from my initial get-wmiobject and any inspiration on getting this to html

    #import
    import-csv C:\test.csv | ForEach-object {  
    
    #set server name
    $Name = $_.Name
    
    #set output for error check
    $cpu0output = "0"
    
    #get OperationalState
    $cpu0 = get-wmiobject SRA_CPUBoard -ComputerName $Name -filter "InstanceName=0" | select-object -expandproperty OperationalState
    $cpu1 = get-wmiobject SRA_CPUBoard -ComputerName $Name -filter "InstanceName=1" | select-object -expandproperty OperationalState
    $ilo0 = get-wmiobject SRA_IOBoard -ComputerName $Name -filter "InstanceName=10" | select-object -expandproperty OperationalState
    $ilo1 = get-wmiobject SRA_IOBoard -ComputerName $Name -filter "InstanceName=11" | select-object -expandproperty OperationalState
    
    #use OperationalState.state code for converting to human speak
    #this is where a need a function to check cpu0,cpu1,ilo0,ilo1 and have it return the corresponding output
      
    if ($cpu0.state -eq "65536") {$cpu0output = "Empty"}
    Elseif ($cpu0.state -eq "131073") {$cpu0output = "Removed from Svc"}
    Elseif ($cpu0.state -eq "131074") {$cpu0output = "Shot"}
    Elseif ($cpu0.state -eq "131075") {$cpu0output = "Broken"}
    Elseif ($cpu0.state -eq "131076") {$cpu0output = "Dumping"}
    Elseif ($cpu0.state -eq "262149") {$cpu0output = "Diagnostics"}
    Elseif ($cpu0.state -eq "131078") {$cpu0output = "Diags Passed"}
    Elseif ($cpu0.state -eq "131079") {$cpu0output = "Initializing"}
    Elseif ($cpu0.state -eq "131080") {$cpu0output = "Syncing"}
    Elseif ($cpu0.state -eq "131081") {$cpu0output = "F/W Updating"}
    Elseif ($cpu0.state -eq "131088") {$cpu0output = "F/W Update Done"}
    Elseif ($cpu0.state -eq "131082") {$cpu0output = "Offline"}
    Elseif ($cpu0.state -eq "131083") {$cpu0output = "Device Ready"}
    Elseif ($cpu0.state -eq "131084") {$cpu0output = "Stopped"}
    Elseif ($cpu0.state -eq "524308") {$cpu0output = "Online"}
    Elseif ($cpu0.state -eq "524309") {$cpu0output = "Simplex"}
    Elseif ($cpu0.state -eq "1572886") {$cpu0output = "Duplex"}
    Elseif ($cpu0.state -eq "1572887") {$cpu0output = "Triplex"}
    Elseif ($cpu0.state -eq "65560") {$cpu0output = "Not Present"}
    Elseif ($cpu0output -eq "0") {$cpu0output = "No Data"} #please note this is the error check value so $cpu0output is correct
    
    #currently this works correctly for $cpu0, but I have not been able to get it to return a value for the other objects without writing the massive if statement over and over 
    write-host $Name,$cpu0output
    
    }
    

    Thank you for your time even if you aren't able to help.

  • #20267
    Profile photo of Dave Wyatt
    Dave Wyatt
    Moderator

    You're on the right track. You already said the answer, in fact: you want to put your "number to readable string" logic into a function, and call that function multiple times for different input. Something along these lines:

    function ConvertTo-ReadableState
    {
        param (
            [Parameter(Mandatory = $true)]
            [string] $State
        )
    
        switch ($State)
        {
            65536   { return 'Empty' }
            131073  { return 'Remoted from Svc' }
            131074  { return 'Shot' }
            131075  { return 'Broken' }
            131076  { return 'Dumping' }
            262149  { return 'Diagnostics' }
            131078  { return 'Diags Passed' }
            131079  { return 'Initializing' }
            131080  { return 'Syncing' }
            131081  { return 'F/W Updating' }
            131088  { return 'F/W Updating Done' }
            131082  { return 'Offline' }
            131083  { return 'Device Ready' }
            131084  { return 'Stopped' }
            524308  { return 'Online' }
            524309  { return 'Simplex' }
            1572886 { return 'Duplex' }
            1572887 { return 'Triplex' }
            65560   { return 'Not Present' }
            default { return 'No Data' }
        }
    }
    
    #import
    Import-csv C:\test.csv | ForEach-object { 
    
        #set server name
        $Name = $_.Name
    
        #set output for error check
        $cpu0output = '0'
    
        #get OperationalState
        $cpu0 = get-wmiobject SRA_CPUBoard -ComputerName $Name -filter 'InstanceName=0' | select-object -expandproperty OperationalState
        $cpu1 = get-wmiobject SRA_CPUBoard -ComputerName $Name -filter 'InstanceName=1' | select-object -expandproperty OperationalState
        $ilo0 = get-wmiobject SRA_IOBoard -ComputerName $Name -filter 'InstanceName=10' | select-object -expandproperty OperationalState
        $ilo1 = get-wmiobject SRA_IOBoard -ComputerName $Name -filter 'InstanceName=11' | select-object -expandproperty OperationalState
    
        $cpu0output = ConvertTo-ReadableState -State $cpu0.State
        $cpu1output = ConvertTo-ReadableState -State $cpu1.State
        $ilo0output = ConvertTo-ReadableState -State $ilo0.State
        $ilo1output = ConvertTo-ReadableState -State $ilo1.State
    
        write-host $Name,$cpu0output,$cpu1output,$ilo0output,$ilo1output
    }
    
  • #20285
    Profile photo of Gareth Edwards
    Gareth Edwards
    Participant

    Hi Dave, thank you so much for the response.

    When I run the your lovely work unfortunately the default { return 'No Data' } switch doesn't work as intended.

    ConvertTo-ReadableState : Cannot bind argument to parameter 'State' because it is an empty string.
    At line:50 char:50
    +     $cpu0output = ConvertTo-ReadableState -State $cpu0.State
    +                                                  ~~~~~~~~~~~
        + CategoryInfo          : InvalidData: (:) [ConvertTo-ReadableState], ParameterBindingValidationException
        + FullyQualifiedErrorId : ParameterArgumentValidationErrorEmptyStringNotAllowed,ConvertTo-ReadableState 
    

    and the result is returned

    AAAAAAAA Duplex Duplex Duplex Duplex
    BBBBBBBB 0 Duplex Duplex Duplex
    CCCCCCC Duplex Duplex Duplex Duplex

    Where "BBBBBBB" should be returning the below

    AAAAAAAA Duplex Duplex Duplex Duplex
    BBBBBBBB No Data No Data No Data No Data
    CCCCCCC Duplex Duplex Duplex Duplex

    Now this is certainly because the "get-wmiobject" is not retrieving any data. In fact this is part of the testing and a fault that should be reported.
    Rather then the object not being present error i.e a "get-wmiobject : Invalid class "SRA_IOBoard""

    It would seem the "default { return 'No Data' }" is not working in the "ConvertTo-ReadableState -State $cpu0.State" command.

    I have tried to change the "default { return 'No Data' }" settings to "0 { return 'No Data' }" and "Null {return 'No Data'}

    My thoughts were turning to a if statement in the switch but since we are doing a "-State $cpu0.State" I thought the result would end up being the same.
    Or possibly a an "ErrorAction" on the outputs.

    Blocking out #[Parameter(Mandatory = $true)] results in it simply returning

    AAAAAAAA Duplex Duplex Duplex Duplex
    BBBBBBBB
    CCCCCCC Duplex Duplex Duplex Duplex

    Which is a better result for me, since at least we can see there is an issue.
    But, I wanted to just swing this by you again and see if you had any ideas?
    I realize these things can be challenging and I hope I am providing a enough information in a legible format.

  • #20286
    Profile photo of Dave Wyatt
    Dave Wyatt
    Moderator

    Ah, no problem. You can just remove the [Parameter(Mandatory = $true)] line. By default, mandatory parameters cannot be $null, empty strings or empty collections (unless you also use the [AllowNull()] / etc attributes.)

  • #20287
    Profile photo of Gareth Edwards
    Gareth Edwards
    Participant

    You are a star, thank you.

    Removing "[Parameter(Mandatory = $true)]" and setting the switch back to "default { return 'No Data' }" solved the problem.

    AAAAAAAA Duplex Duplex Duplex Duplex
    BBBBBBBB No Data No Data No Data No Data
    CCCCCCC Duplex Duplex Duplex Duplex

You must be logged in to reply to this topic.