首页 > 解决方案 > 显示菜单提示并将用户选择描述保存为 powershell 脚本中的变量

问题描述

不幸的是,没有多少 Powershell 家伙。希望完成以下工作:

Prompt the user with a list of choices: 
1 - gaming
2 - Home Ent
3 - Theatrical
4 - TV and Streaming
5 - VR

然后,给定用户响应 (1-5),它将类型保存为变量(即,如果他们选择 3,则变量 ($projectType) 将保存为“Theatrical”的 ChoiceDescription。

脚本的其余部分创建 AD 组、目录结构并设置权限。那部分我已经弄清楚了,但是菜单提示是在暗示我。

一直在使用 PromptForChoice,但失败得很惨。

提前致谢

编辑:根据 Vasil 的建议,我知道有这个:

Function Get-ProjectType {
    $type=Read-Host "
    1 - gaming
    2 - Home Ent
    3 - Theatrical
    4 - TV and Streaming
    5 - VR
    Please choose Project Type"
    Switch ($type){
        1 {$projectType="Gaming"}
        2 {$projectType="Home Entertainment"}
        3 {$projectType="Theatrical"}
        4 {$projectType="TV & Streaming"}
        5 {$projectType="VR"}
    }
    return $projectType
}

#import the ActiveDirectory Module and define the parent folder path

Import-Module ActiveDirectory
$path = "\\calamedia\edit\$ProjectType"
 $newProjectName = Read-Host -Prompt "Enter Name of Project"
 $newFolderFull = $path + $newProjectName
 Write-Output "New Folder will be: $NewProjectName"
 $confirm = Read-Host "Confirm? Y/N"
 If(($confirm) -ne "y")
 {
     # End
 }
 Else
 {}

 Write-Output "Create AD Groups"
$groupnameRW = "Shared.$newProjectName.RW"
$groupnameR = "Shared.$newProjectName.R"
New-AdGroup $groupNameRW -samAccountName $groupNameRW -GroupScope DomainLocal -path "OU=Projects,OU=Managed Groups,DC=createadvertising,DC=com"
New-AdGroup $groupNameR -samAccountName $groupNameR -GroupScope DomainLocal -path "OU=Projects,OU=Managed Groups,DC=createadvertising,DC=com"

# add the folder itself and remove inherited permissions

Write-Output "Add Folder.."
New-Item $newProjectName -ItemType Directory
Write-Output "Remove Inheritance.."
icacls $newFolderFull /inheritance:d

但是,它现在似乎直接跳转到脚本后面的“输入项目名称:”提示并且没有显示该函数?

标签: functionpowershellvariables

解决方案


更新

我现在看到了您的代码,我已经以您可以应用的方式编辑了我的代码示例。

您可以使用Read-Host来放置一些文本并Switch获取选择变量。

Function Get-ProjectType {
    $type=Read-Host "
    1 - gaming
    2 - Home Ent
    3 - Theatrical
    4 - TV and Streaming
    5 - VR
    Please choose"
    Switch ($type){
        1 {$choice="Gaming"}
        2 {$choice="Home Entertainment"}
        3 {$choice="Theatrical"}
        4 {$choice="TV & Streaming"}
        5 {$choice="VR"}
    }
    return $choice
}

$projectType=Get-ProjectType

现在$projectType包含选择的名称。


推荐阅读