Last active
May 22, 2016 19:15
-
-
Save midnightfreddie/238297db30e40ba18b676ff83a8e1465 to your computer and use it in GitHub Desktop.
In reply to https://www.reddit.com/r/PowerShell/comments/4kjhvo/need_some_help_with_restricting_a_prompt_for/
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add-Type -TypeDefinition @" | |
public enum PcType { | |
DESKTOP, | |
LAPTOP | |
} | |
"@ | |
$PcChoices = [System.Enum]::GetValues([type]"PcType") | |
# '^(DESKTOP|LAPTOP)\d{6}$' , but this lets us add/modify enum choices later with no code changes | |
$RegExMatch = "^({0})\d{{6}}$" -f ( ( $PcChoices | ForEach-Object { $_.ToString() } ) -join "|") | |
do { | |
if ( $i ) { Write-Host "`nYou entered invalid data. Please try again.`n" } | |
Write-Host "Valid choices:" | |
for ($i = 1; $i -le $PcChoices.Count; $i++) { | |
Write-Host ("{0}.`t{1}" -f $i, $PcChoices[$i-1]) | |
} | |
$Type = Read-Host "Enter PC Type: " | |
$Code = Read-Host "Enter 6 digit code" | |
$ComputerName = "{0}{1}" -f $PcChoices[$Type - 1], ([int]$Code).ToString("000000") | |
} until ($ComputerName -cmatch $RegExMatch) | |
$ComputerName |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment