Sune
Sune

Reputation: 3270

Returning value from function (setting variable from existing variable name)

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?

Upvotes: 2

Views: 2185

Answers (1)

Andrey Marchuk
Andrey Marchuk

Reputation: 13483

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

Upvotes: 4

Related Questions