2016-04-14 126 views
0

我遇到了一个小问题,我必须要求用户不要在文件夹名称中放置反斜杠“\”,因为当我将$ Server和$组合在一起时,它将删除服务器名称家长...我如何防止这种情况发生?我宁愿不限制我的用户添加反斜杠...如何防止文字被删除

另外,我一直试图阻止c:,d:,e:等驱动器在$ Parent中使用,但即使我使用-in-contains它仍然允许输入c:\ xxxx或d:\ xxxx。我如何防止这种情况发生?

# File share server name 
$Server = Read-Host -prompt "Verify Server Server Name (ie ECCOFS01)" 
If ([string]::IsNullOrWhiteSpace($Server)) { 
    Write-Host "You entered $Server which is an incorrect value: This Script is now Exiting." -Foreground "White" -Background "Red" 
    Exit 
    } 
else { 
    Write-Host "You Entered $Server" -Foreground "Black" -Background "Yellow" 
    } 

    # Parent folder setup 
$Parent = Read-Host -prompt "Enter full parent path that will contain the new folder(ie. Groups\ECCO IT) - Do NOT start with \. Please use correct spelling and capitalization (ie. Parent Folder Name). " 
If ([string]::IsNullOrWhiteSpace($Parent) -or ($Parent -eq "c:") -or ($Parent -eq "d:")) { 
    Write-Host "You entered $Parent which is an incorrect value: This Script is now Exiting." -Foreground "White" -Background "Red" 
    Exit 
    } 
else { 
    Write-Host "You Entered $Parent" -Foreground "Black" -Background "Yellow" 
    } 

$ServerParentShare = "\\"+[IO.Path]::Combine($Server,$Parent) 

    # New Folder Name 
$Name = Read-Host -prompt "Enter New Folder Name. Please use correct spelling and capitalization (ie. New Test Folder)" 
If ([string]::IsNullOrWhiteSpace($Name)) { 
    Write-Host "You entered $Name which is an incorrect value: This Script is now Exiting." -Foreground "White" -Background "Red" 
    Exit 
    } 
else { 
    Write-Host "You Entered $Name." -Foreground "Black" -Background "Yellow" 
    } 

$Path = [IO.Path]::Combine($ServerParentShare,$Name) 
    Write-Host = "New Folder Path = $Path" -Foreground "Black" -Background "Yellow" 

    # Choose parent OU 
$Country = Read-Host -prompt "Enter the Country OU that the Security Group will reside in (i.e. Global, Americas, Europe, Asia Pacific)" 
If ([string]::IsNullOrWhiteSpace($Country)) { 
    Write-Host "You entered $Country which is an incorrect value: This Script is now Exiting." -Foreground "White" -Background "Red" 
    Exit 
    } 
else { 
    Write-Host "---------------------VERIFY ENTRY---------------------" -Foreground "Black" -Background "Yellow" 
    Write-Host "OU = $Country, New share location = $Path" -Foreground "Black" -Background "Yellow" 
    } 


    # Option to continue or cancel the script 
$Continue = Read-Host -prompt "Does this look correct? Y or N?" 
If (($Continue -eq "N") -or ($Continue -eq "No")) { 
    Write-Host "Please Start over. This Script is now Exiting." -Foreground "White" -Background "Red" 
    Exit 
    } 
else { 
    Write-Host "Make sure to verify all folders and and AD Groups once complete." -Foreground "Yellow" -Background "Black" 
    } 
+0

我其实问了上面的两个问题。你给我的链接只有一个答案,而不是两个答案。我应该删除第一个问题并留下第二个问题吗? –

回答

1

我必须要求用户不要把一个反斜杠 “\” 文件夹名称

插入此行一行12后:

$parent = $parent -replace '^\\','' 

如果$父在位置0包含一个反斜杠,它将用空字符串替换它。如果没有,它没有效果。

PS C:\> '\a\b' -replace '^\\','' 
a\b 
PS C:\> 'a\b' -replace '^\\','' 
a\b 
PS C:\> 

技术上这并不阻止用户把一个反斜杠的文件夹名称,但如果他/她呢,它会删除它,这有类似的效果。

我一直在试图阻止C:d:,E:等从 $家长正在使用的驱动器,但即使我使用-in或 - 包含它仍然允许

-in和-contains对集合进行操作,而不是单个对象(如$ parent)。对于$ parent,你可能想使用-like或者-match。您可以检查这样的驱动器盘符格式的路径:

($parent -like '?:*') 

或者你可以找一个冒号路径

($parent -like '*:*') 

您可以使用这些条件语句中while循环,迫使用户继续输入,直到他/她输入你想要的格式。或者如果输入无效则可以退出。把它放在一起,例如:

do{ 
    $parent = read-host -prompt 'Enter full parent path' 
    $parent = $parent -replace '^\\','' 
}while($parent -like '*:*') 
+0

太棒了,谢谢。我一直在努力弄清楚如何做循环,但一直没有能够深入研究它。 –

+0

如何做一个多'while'语句?我必须添加额外的循环吗?我尝试使用'([string] :: IsNullOrWhiteSpace($ Server))'连接'while($ parent -like'*:*')',但似乎无法使其工作...我需要合并“if”语句中的两个内部? –

+0

上面的while循环可以修改为继续在null或空白条目上循环: 'while($ parent -like'*:*'-or(!$ parent))''。 备注:'[system.string] :: isnullorwhitespace()'对于空字符串或空字符串返回true。在if和while这样的条件中做一个简短的方法:'if(!$ parent)' – cwr