调用函数

时间:2019-05-07 02:52:15

标签: powershell

我正在尝试运行一个函数,该函数会在Powershell脚本中设置然后测试变量。

该变量在函数内设置,但一旦返回脚本便不会携带其值。我不确定自己在做什么错。我认为这很简单,我很想念。

此功能是较大脚本的一部分。

我也不确定我对适当选择的测试是否正常工作。

####Function to set and test Year level#####
function Year_Level
{
    do{
    $Selection=Read-Host "Choose a Year Level"
    Switch ($Selection)
    {
        0 {$YearLevel1="Year 00"}
                01 {$YearLevel1="Year 01"}
        02 {$YearLevel1="Year 02"}
        03 {$YearLevel1="Year 03"}
        04 {$YearLevel1="Year 04"}
        05 {$YearLevel1="Year 05"}
        06 {$YearLevel1="Year 06"}
        07 {$YearLevel1="Year 07"}
        08 {$YearLevel1="Year 08"}
        09 {$YearLevel1="Year 09"}
        10 {$YearLevel1="Year 10"}
        11 {$YearLevel1="Year 11"}
        12 {$YearLevel1="Year 12"}
        13 {$YearLevel1="Staff"}
        }

if($selection -ne 01,02,03,04,05,06,07,08,09,10,11,12,13)
{Write-Warning "Incorrect year level selection, try again!" -Verbose
} else{
write-host "You have selected $YearLevel1"
}
    }until ($selection -lt 14)
}

Write-Host "Select Year Level"
Write-Host "0 = Reception"
Write-Host "01 = Year 1"
Write-Host "02 = Year 2"
Write-Host "03 = Year 3"
Write-Host "04 = Year 4"
Write-Host "05 = Year 5"
Write-Host "06 = Year 6"
Write-Host "07 = Year 7"
Write-Host "08 = Year 8"
Write-Host "09 = Year 9"
Write-Host "10 = Year 10"
Write-Host "11 = Year 11"
Write-Host "12 = Year 12"
Write-Host "13 = Staff"

Year_Level

我想在调用函数的脚本中设置$ yearlevel1变量以在该函数之后使用。

编辑

我现在在函数之外设置了变量。但是有时它不能正确设置/测试变量是什么。我缺少的代码中有显而易见的东西吗?

function Year_Level
{
    do{
    $Selection=Read-Host "Choose a Year Level"
    Switch ($Selection)
    {
        0 {$YearLevel1="Year 00"}
        01 {$YearLevel1="Year 01"}
        02 {$YearLevel1="Year 02"}
        03 {$YearLevel1="Year 03"}
        04 {$YearLevel1="Year 04"}
        05 {$YearLevel1="Year 05"}
        06 {$YearLevel1="Year 06"}
        07 {$YearLevel1="Year 07"}
        08 {$YearLevel1="Year 08"}
        09 {$YearLevel1="Year 09"}
        10 {$YearLevel1="Year 10"}
        11 {$YearLevel1="Year 11"}
        12 {$YearLevel1="Year 12"}
        13 {$YearLevel1="Staff"}
        }

if($selection -gt 13)
{Write-Warning "Incorrect year level selection, try again!" -Verbose
} else{
write-host "You have selected $YearLevel1"
}
    }until ($selection -lt 14)
    $YearLevel1
}

Write-Host "Select Year Level"
Write-Host "0 = Reception"
Write-Host "01 = Year 1"
Write-Host "02 = Year 2"
Write-Host "03 = Year 3"
Write-Host "04 = Year 4"
Write-Host "05 = Year 5"
Write-Host "06 = Year 6"
Write-Host "07 = Year 7"
Write-Host "08 = Year 8"
Write-Host "09 = Year 9"
Write-Host "10 = Year 10"
Write-Host "11 = Year 11"
Write-Host "12 = Year 12"
Write-Host "13 = Staff"

$yearlevel = Year_Level

1 个答案:

答案 0 :(得分:1)

$yearlevel1变量是在函数内部定义和设置的,因此在函数外部将不可用。我建议从函数返回值并将其存储在变量中。

function Year_Level {
    ....
    ....
    return $yearlevel1
}


$ReturnValue = year_level

但是似乎您在函数内使用变量,而不是在函数外使用变量。 我建议您尝试以下方法来满足您的要求。这很简单。

function Year_Level {
    $SupportedValues = 1..13
    do {
        $Selection = Read-Host "Choose a Year Level"
        if ($SupportedValues -notcontains $selection) {
            Write-Warning "Incorrect year level selection, try again!" -Verbose
        }
        else {
            $YearLevel1 = "Year $Selection"
            write-host "You have selected $YearLevel1"
        }

    }until ($selection -lt 14)
}