2

I would like to pass two arguments to my function and set a variable (and variable name) based on the results. I've been trying to do this the following way:

$service_to_check_for = "ping"

function check-service-monitored ($check_command,$check_name) {
    if($service_to_check_for -eq $check_command)
    {
        $Value = "Yes"
    }
    else
    {
        $Value = "No"
    }       

    $script:check_command = $check_command
    $script:check_name = $check_name
    $script:value = $Value
}

check-service-monitored "ping" "NagiosPing"

echo "$check_name : $value"

I would like

     $NagiosPing = "Yes"

but how?

1 Answer 1

4

Just make sure that the only value your function prints out is the result:

$service_to_check_for = "ping"

function check-service-monitored ($check_command,$check_name) {
if($service_to_check_for -eq $check_command)
{
    "Yes"
}
else
{
    "No"
}       

$script:check_command = $check_command
$script:check_name = $check_name
$script:value = $Value
}

$NagiosPing = check-service-monitored "ping" "NagiosPing"
$NagiosPing 

The only output your function provides now is Yes or No, just assign it to your variable

Sign up to request clarification or add additional context in comments.

2 Comments

Thank you so much!! I didn't think of setting the function as a variable! This is my finished code: $service_to_check_for = "ping" function check-service-monitored ($check_command) { if($service_to_check_for -eq $check_command) { $Value = "Yes" } else { $Value = "No" } return $value } $nagiosping = check-service-monitored "ping" echo "Monitor: $nagiosping"
Of course! For some reason I was unable to (you can accept this answer momentarily). Again, thank you!!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.