function - 显示菜单提示并将用户选择描述保存为 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
但是,它现在似乎直接跳转到脚本后面的“输入项目名称:”提示并且没有显示该函数?
解决方案
更新
我现在看到了您的代码,我已经以您可以应用的方式编辑了我的代码示例。
您可以使用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
包含选择的名称。
推荐阅读
- python - 使用 argparse.ArgumentParser() 构造解析器时出错
- php - 使用 PHP (Laravel) 将动态嵌套 JSON 数据插入 MySQL
- vba - Word中的复制范围避免剪贴板
- python - 从 Django 请求 URL 获取锚标记
- python - 如何获取当前行的行索引
- angular - 无法禁用 VSCode 以在保存时删除结尾空格?
- arrays - switch 语句中的重复案例
- dagster - 当至少一个所需的输入被给出时执行一个实体
- php - 我该如何运行作曲家更新命令
- session - Ext JS 7.3.1 经典错误 - 记录保存时 C 的状态无效