我正在创建一个脚本来获取用户输入共享路径或路径,该脚本将提供所提供路径的权限列表。我要求使用“读取主机”命令来提供用户所需路径数量的输入,因此,他/她需要逐一放置路径,以提供ACL列表。 在执行此操作时,我遇到了一个问题,即用户输入了错误的输入,例如用户提供的输入不是整数,双精度值还是它的字符串,因此应该转到我的if条件,然后他/她重新输入键入错误的数字,它将结束脚本并要求重新运行脚本。
我已经尝试过以下脚本,但是在第4行,我完全被卡住了。这是我编写的完整代码。在第3行,当我输入一个值说1.2或任何字符串值说Hello或任何负整数或0值时,它应该转到第4行,否则应该转到elseif(即第36行)。有人可以帮我解决这个问题吗。我知道我可以用更短的方式来做,但是这里有些违反。
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"
答案 0 :(得分:1)
我认为可以使用一个小的辅助函数。也许是这样的:
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
}
}
您可以这样使用它:
# 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
}