Powershell下拉选择项,将特定文本文件加载到表单上的文本框中

问题描述 投票:0回答:1

在Powershell中,我想要一个包含以下项目的下拉列表:

Textfile1文本文件2Textfile3

如果选择了一个列表项,则它将文件内容读入表单上的文本框。

例如:

下拉列表“ Textfile1”被选中,它将c:\ Textfile1.txt加载到窗体的文本框中。

已经尝试过使用if/elseif的功能,但是很难将其捆绑在一起,不幸的是仍在学习Powershell。

powershell dropdown
1个回答
0
投票

我不建议创建一个带有下拉列表的表单并从中返回选择,因为Powershell几乎不是为该类构建的。

当然,我认为这是不可能的,但是在这种情况下,我将使用Out-GridView函数来完成与您的需求相似的工作。

$files_location = "C:\yourlocation\*"


$options = Get-ChildItem $files_location


$user_choice = $options | Out-GridView -Title 'Select the File you want to show'  -PassThru

Switch ($user_choice)  {

    #Condition to check:

    { $user_choice.Name -eq 'textfile1.txt' }


    #Do something:
    {
        Write-Host "Im going to open $($user_choice.Name)"
        #Open the file:
        start "$user_choice"
    }

    #Continue your switch/case conditions here...

}

我正在使用Get-ChildItem函数的输出对象,并从中输出gridview。

如果您更喜欢此功能,则可以将Switch大小写更改为if语句

$files_location = "C:\yourlocation\*"


$options = Get-ChildItem $files_location


$user_choice = $options | Out-GridView -Title 'Select the File you want to show'  -PassThru

if ($user_choice.Name -eq 'a.txt')
{
    start $user_choice
}
© www.soinside.com 2019 - 2024. All rights reserved.