显示菜单提示,并将用户选择说明另存为 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( 被保存为 "Ttheaterrical" 的选择描述。

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

一直在搞乱PromptForChoice,但失败得很惨。

提前致谢

编辑:根据瓦西尔的建议,我知道有这个:

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 = "\calamediaedit$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

但是,它现在似乎直接从脚本后面的"输入项目名称:"提示符跳转到"输入项目名称:"提示,并且不显示任何函数?

更新

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

您可以使用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包含选择的名称。

最新更新