Function 从函数返回值(从现有变量名设置变量)

Function 从函数返回值(从现有变量名设置变量),function,powershell,Function,Powershell,我想将两个参数传递给我的函数,并根据结果设置一个变量(和变量名)。我一直在尝试以下方法: $service_to_check_for = "ping" function check-service-monitored ($check_command,$check_name) { if($service_to_check_for -eq $check_command) { $Value = "Yes" } else { $V

我想将两个参数传递给我的函数,并根据结果设置一个变量(和变量名)。我一直在尝试以下方法:

$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"
我想要

     $NagiosPing = "Yes"

但是如何实现呢?

只需确保函数输出的唯一值是结果:

$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 

函数现在提供的唯一输出是“是”或“否”,只需将其分配给变量即可

只需确保函数输出的唯一值是结果:

$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 

函数现在提供的唯一输出是“是”或“否”,只需将其分配给变量即可

非常感谢!!我没有想过将函数设置为变量!这是我完成的代码:$service_to_check_for=“ping”函数检查服务监控($check_命令){if($service_to_check_for-eq$check_命令){$Value=“Yes”}否则{$Value=“No”}返回$Value}$nagiosping=检查服务监控的“ping”echo“监控:$nagiosping”当然!由于某种原因,我无法(你可以暂时接受这个答案)。再次感谢你!!非常感谢你!!我没有想过将函数设置为变量!这是我完成的代码:$service_to_check_for=“ping”函数检查服务监控($check_命令){if($service_to_check_for-eq$check_命令){$Value=“Yes”}否则{$Value=“No”}返回$Value}$nagiosping=检查服务监控的“ping”echo“监控:$nagiosping”当然!由于某种原因,我无法(你可以暂时接受这个答案)。再次感谢你!!