Powershell交互式菜单,文本文件中的菜单选项

Powershell interactive Menu, Menu options from text file

我正在尝试在 Powershell 中创建交互式菜单。现在,我想知道是否可以从文本文件中获取菜单选项:

function GetMenu {    
Clear-Host
Write-Ascii -fore green "Berechtigungen"
""
"1)Root Folder"
"2)Sub Folder"
"3)Delete Folder"
"4)Add User"
""
"x) Exit"
""
$MenuSelection = Read-Host "Choose Option"
GetSubMenu
} 
GetMenu



###########################################
function GetSubMenu {
write-host ""
switch -wildcard ($MenuSelection) {
    "1" 
    {
    function GetSubMenuRoot {
write-host ""
switch -wildcard ($MenuSelectionRoot) {
    "1" 
    {
    
    }
    "2" 
    {
    
    }
    "3" 
    {
    
    }
    "x" 
    {
        Clear-Host; 
        exit}
    default{Clear-Host;GetMenuRoot}
    }

}




function GetMenuRoot {    
Clear-Host
Write-Ascii -fore green "RootFolder"
""
"1)(From TextFile)"
"2)(From TextFile)"
"3)(From TextFile)"
""
"x) Exit"
""
$MenuSelectionRoot = Read-Host "Wähle Option"
GetSubMenuRoot
} 
GetMenuRoot

    
    }
    "2"
    {
    
    }
    "3"
    {
    
    }
    "4"
    {
    
    }       
    "x" {
        Clear-Host; 
        exit}
    default{Clear-Host;GetMenu}
}
    
}

文本文件如下所示:

Org;Share;
Sales;\srvSales\Sales$\
IT;\srvIT\IT$\
CEO;\srvCEO\CEO$\

(来自文本文件)现在应该是:

1)Salse
2)IT
3)CEO

作为菜单选项

抱歉,英语不是我的第一语言,如果有什么不清楚的地方,请询问。

它似乎是一个 csv 文件,但考虑到它是一个文本文件(如您所说),您可以使用 Get-Content cmdlet 读取 .txt 文件的内容。

$data = Get-Content '\YourTextFile.txt'
$orgValues = @()

foreach($line in $data[1 .. $data.Count]) #since you won't need the column name from first row
{
    $info = $line -split ';';
    $orgValues += $info[0]; #this contains your required values
}

然后您可以遍历 $orgValues 以在您的开关盒中使用它们。也许是这样的:

function GetMenuRoot 
{    
    Clear-Host
    $count = $orgValues.Count
    Write-Ascii -ForegroundColor green "RootFolder"
    ""
    for($i = 1; $i -le $orgValues.Length; $i++)
    {
        "$i) $($orgValues[$i - 1])"
        
    }
    $count++;
    "$count) Exit"
    ""
    
    $MenuSelectionRoot = Read-Host "Wähle Option"
    GetSubMenuRoot
}