这是我的代码。我的主要问题是我在函数location
中请求一个变量,但是当我尝试在函数Show-Menu
中写入变量时,它显然是空的。
我进行了一次搜索,发现该变量应该位于全局变量之前。像这样:
function Show-Menu {
Param(
[string]$Title = 'Menu'
)
cls
Write-Host "================ $Title ================"
Write-Host " "
Write-Host "The path is $global:path"
Write-Host "1: Press '1' for IDF config."
Write-Host "2: Press '2' for MDF config."
Write-Host "Q: Press 'Q' to quit."
}
function location {
cls
$global:path = Read-Host -Prompt "Set path to save your configurations (default C:\Network_configs)"
#Acuerdate que no funciona bien, por el hecho de tener variables en diferentes funciones.
if ([lenght]$global:path -le 1) {
$global:path = C:\Network_configs
}
}
在没有键盘入口的情况下,我希望输出的路径为C:\ Network_configs,但是我有以下输出:
================ Menu ================ The path is 1: Press '1' for IDF config. 2: Press '2' for MDF config. Q: Press 'Q' to quit. Please make a selection:
答案 0 :(得分:0)
不建议使用全局变量签出variable scoping。
但是,如果您非常接近,请尝试检查用户输入,如果为空,则使用默认值。
function Show-Menu
{
Param(
[string]$Title = 'Menu'
)
cls
Write-Host "================ $Title ================"
Write-Host " "
Write-Host "The path is $global:path"
Write-Host "1: Press '1' for IDF config."
Write-Host "2: Press '2' for MDF config."
Write-Host "Q: Press 'Q' to quit."
}
function location
{
cls
$userInput = Read-Host -Prompt "Set path to save your configurations (default C:\Network_configs)"
if ($null -eq $userInput)
{
$global:path = 'C:\Network_configs'
}
else
{
$global:path = $userInput
}
}