Search code examples
powershellvalidateset

Powershell Script prompt user for input on validate set Function Parameter


I'm guessing this is impossible, but I figured I'd ask because it would be cool if I could.

I have a function, that has a validate set (so people don't screw up the input - obviously) However, prompts in a script don't seem to allow for this. I've looked all over, I can't find anyone asking this question, or providing any details that would help.

function ShowStackOverFlowCommunity-ExampleOfFunction {
    PARAM (
        
        [parameter()]
        [validateset(
            "Don't Input Incorrect Things",
            "Stop it",
            "I Swear to God..."
        )]
        [string]
        $pplbedumb

    )

    write-host "Hi I'm a script. TeeHee"
    write-host $pplbedumb;

}

So an example script would be

$path = read-host "input path to file"
$pathdata = gc $path;

foreach ($item in $pathdata) {
    get-service $item | select name, balls, etc. 
    if ($item.balls) {
        ShowStackOverFlowCommunity-ExampleOfFunction 
    }
}

I'm well aware that I can do a read-host - but that allows for the possibility of input mistakes. The only thing i can think of is to just say at the beginning of the script "HEY, RUN THIS FUNCTION FIRST OUTSIDE OF THE SCRIPT. THEN COME BACK"

But that's just...lame.


Solution

  • You can run your validation after the user put his input and then return a "Please try again" or "Please follow the restrictions for the input"

    When I was writing a script to check the users without opening the AD console I did the below:

    Import-Module ActiveDirectory
    [string[]]$GetADProps=echo Created, Name, EmailAddress, Enabled, LockedOut, LastBadPasswordAttempt, PasswordExpired, AccountExpires, PasswordLastSet, LastLogonDate, Modified, LogonCount, Office, TelephoneNumber
    [string[]]$FlProps=echo Created, Modified, LogonCount, Name, EmailAddress, Enabled, LockedOut, PasswordExpired, PasswordLastSet, LastLogonDate, LastBadPasswordAttempt, Office, TelephoneNumber
    do{
        $Username = (Read-Host -Prompt "Please Enter Username to Lookup")
        Get-ADDomainController -Filter * | Select Name, IPv4Address, Site | Sort-Object Name | Out-String
        $DC = (Read-Host  -Prompt "Please Enter the Domain Controler name from the list")
        $ADUser= Get-ADUser -Server $DC $Username -Properties $GetADProps  
        if ($adUser.'LockedOut' -or $ADUser.'PasswordExpired'){
        $ADUser | Format-List $FlProps  | Out-String | Write-Host -ForegroundColor Red
    do {
        do {
            write-host ""
            write-host "[U] - Unlock User " -NoNewline; write-host "$Username" -ForegroundColor Red
            write-host "[R] - Reset Users " -NoNewline; write-host "$Username" -ForegroundColor Red -NoNewline; write-host " Password"
            write-host "[C] - Check Users " -NoNewline; write-host "$Username" -ForegroundColor Red -NoNewline; write-host " Account Info"
            write-host ""
            write-host "[X] - Exit"
            write-host ""
            write-host -NoNewline "Type your choice and press Enter: "
            
            $Choice = read-host
            
            write-host ""
            
            $ok = $Choice -match '^[urcx]+$'
            
    if ( -not $ok) { write-host "Invalid selection" }
    } until ( $ok )
    

    So the user using it had to put the right input. If you need to search a specific path you can run a test-path and if it is not right you can prompt the user again.

    Also I read about the validate set on https://adamtheautomator.com/powershell-validateset/ Hope this helps you.