2016-04-14 102 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