【发布时间】:2019-10-05 23:30:11
【问题描述】:
我正在创建一个脚本来获取用户输入的共享路径或路径,它将提供所提供路径的权限列表。使用 Read-Host command-let 我要求用户提供他需要的路径数量的输入,因此他/她需要一一放置路径,这将提供 ACL 列表。 在执行此操作时,我遇到了用户输入错误输入的问题,例如,如果用户提供的输入不是整数、双精度值或字符串,那么它应该转到我的 if 条件,然后他/她再次重新输入错误的数字会结束脚本并要求重新运行脚本。
我已经尝试了下面的脚本,但是在第 4 行我完全卡住了。这是我编写的完整代码。在第 3 行,当我输入一个值说 1.2 或任何字符串值说 Hello 或任何负整数或 0 值时,它应该转到第 4 行,否则它应该转到第 36 行的 elseif。有人可以帮我解决这个问题.我知道我可以用更短的方式做到这一点,但这里有些违规。
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,或字符串或双精度值。如果包含其中任何一个,则应该转到第 4 行,即 if 条件。
标签: powershell