0

I am creating a script to grab a user input share path or path and that will provide permission list of the provided path. Using Read-Host command-let I am asking user to provide input for number of paths he requires and thereby he/she needs to put on the paths one by one which will provide the ACL list. While doing this I'm getting issue where user inputs a wrong input such as if user provide an input which is not an integer or is a double value or its a string so it should go to my if condition and again he/she re-types the wrong number it would end up the script and would ask for re-run of the script.

I have tried below script however at line 4 I am totally stuck. here is the full code I have written. At line 3 when I input a value say 1.2 or any string value say Hello or any negative integer or 0 value, it should go to Line 4, else it should go to elseif which is line 36. Can someone help me out to fix this. I know I can do it in a shorter manner but something here is violating.

Clear-Host
Get-Date
$num_path= Read-Host "`n `n Enter number of paths"
if (($num_path -as [int]) -le 0 -or ($num_path -as [double]) -is [double])
{
    Write-Host "Error: Value you have Entered is either less than or equal to 0 or not an interger or it is a String value.`nKindly enter correct value." -ForegroundColor Black -BackgroundColor Cyan
    $New_num_path= Read-Host "`n `n Re-Enter number of paths"
    if (($New_num_path -as [int]) -gt 0 -and !($New_num_path -as [double]) -is [double])
    {

        Write-Host "`n `n \\ServerName\FilePath `n `n Enter File Path in above format"
        For($i=1; $i -le $New_num_path; $i++)
        {
            $paths= Read-Host "`n `nEnter File path no. $i " #Enter path with $ sign after drive name e.g E$\Applications
            Write-Host "`n `n"
            if (Test-Path -Path $paths)
            {
                foreach($path in $paths)
                {
                    Get-Acl -Path $path | fl
                }
            }

            Else
            {
                Write-Host "Error: Path '$paths' does not exist" -ForegroundColor Black -BackgroundColor Cyan
            }
        }
    }
    Else
    {
        Write-Host "Error: You have input wrong value. Kindly Re-run the script again." -ForegroundColor Black -BackgroundColor Cyan
    }

}
Elseif(($num_path -as [int]) -gt 0)
{

    Write-Host "`n `n \\ServerName\FilePath `n `n Enter File Path in above format"
    For($i=1; $i -le $num_path; $i++)
    {
        $paths= Read-Host "`n `nEnter File path no. $i " #Enter path with $ sign after drive name e.g E$\Applications
        Write-Host "`n `n"
        if (Test-Path -Path $paths)
        {
            foreach($path in $paths)
            {
                Get-Acl -Path $path | fl
            }
        }

        Else
        {
            Write-Host "Error: Path '$paths' does not exist" -ForegroundColor Black -BackgroundColor Cyan
        }
    }
}
Else
{
    Write-Host "Error: You have input wrong value. Kindly Re-run the script again." -ForegroundColor Black -BackgroundColor Cyan
}
Write-Host "`n `n `n `t `t `t------------------------------------------------------THE END------------------------------------------------------`n"
3

1 Answer 1

1

I think a small helper function could come in handy. Something like this perhaps:

function Ask-Integer {
    [CmdletBinding()]
    param(
        [string]$Prompt = 'Please enter a value.',
        [string]$CancelOption = $null,
        [int]$MinValue = [int]::MinValue,
        [int]$MaxValue = [int]::MaxValue
    )

    # enter an endless loop
    while ($true) {
        Clear-Host

        [int]$value = 0
        if ($CancelOption) { 
            Write-Host "Type $CancelOption to cancel." -ForegroundColor Yellow
        }
        $result = Read-Host $Prompt

        # user cancelled, exit function and return nothing
        if ($result -eq $CancelOption) { return $null }

        if ([int]::TryParse($result, [ref]$value)) {
            if ($value -ge $MinValue -and $value -le $MaxValue) {
                return $value
            }
        }

        # if we got here, the user entered something other than the wanted integer, so try again
        Write-Warning "Invalid choice.. Please enter a whole number between $MinValue and $MaxValue."
        Sleep -Seconds 3
    }
}

You use it like this:

# use the function to return an integer value or $null if the user cancelled
$num_path = Ask-Integer -Prompt 'Enter the number of paths' -CancelOption 'Q' -MinValue 1 -MaxValue 10
# test if the user did not cancel
if ($null -ne $num_path) {

    # rest of your code

}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.