I'm running into a small problem where I have to require the user to NOT put a backslash "\" in the folder name because it removes the server name when I combine $Server and $Parent... How do I prevent that from happening? I'd rather not restrict my user from adding that backslash...
Also, I've been trying to prevent c:, d:, e:, etc. drives from being used in $Parent, but even if I use -in
or -contains
it still allows the c:\xxxx or d:\xxxx to be entered. How do I prevent that?
# 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"
}
I have to require the user to NOT put a backslash "\" in the folder name
insert this line after line 12:
$parent = $parent -replace '^\\',''
if $parent contains a backslash at position 0, it will replace it with an empty string. if not, it has no effect.
PS C:\> '\a\b' -replace '^\\',''
a\b
PS C:\> 'a\b' -replace '^\\',''
a\b
PS C:\>
technically this doesn't prevent the user from putting a backslash in the folder name, but if he/she does, it will remove it, which has a similar effect.
I've been trying to prevent c:, d:, e:, etc. drives from being used in $Parent, but even if I use -in or -contains it still allows
-in and -contains operate on collections, not a single object (like $parent). for $parent, you probably want to use -like or -match. you can check for a drive letter-formatted path like this:
($parent -like '?:*')
or you can just look for a colon in the path
($parent -like '*:*')
you can use either of those conditionals in a while loop, forcing the user to keep inputting until he/she inputs the format you want. or you can just exit if the input is invalid. put it all together, for example:
do{
$parent = read-host -prompt 'Enter full parent path'
$parent = $parent -replace '^\\',''
}while($parent -like '*:*')