简体   繁体   English

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

[英]Powershell interactive Menu, Menu options from text file

I'm trying to create an interactive menu in Powershell.我正在尝试在 Powershell 中创建一个交互式菜单。 Now, I want to know if its possible to get the menu options to come out of a text file:现在,我想知道是否可以从文本文件中获取菜单选项:

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}
}
    
}

The Text file looks like this:文本文件如下所示:

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

(from textfile) should now be: (来自文本文件)现在应该是:

1)Salse
2)IT
3)CEO

As menu options作为菜单选项

Sorry english isn't my first language if something is unclear please ask.抱歉,英语不是我的母语,如果有不清楚的地方请询问。

It seems to be a csv file, but considering that it's a text file (as you said), you can read the contents of your .txt file using Get-Content cmdlet.它似乎是一个 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
}

You can then iterate through $orgValues to use them in your switch cases.然后,您可以遍历$orgValues以在您的 switch 案例中使用它们。 Maybe something like this:也许是这样的:

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
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM