【问题标题】:How to prevent words from being removed [duplicate]如何防止单词被删除[重复]
【发布时间】:2016-04-14 16:37:15
【问题描述】:

我遇到了一个小问题,我必须要求用户不要在文件夹名称中添加反斜杠“\”,因为当我组合 $Server 和 $Parent 时它会删除服务器名称......我该怎么做防止这种情况发生?我宁愿不限制我的用户添加反斜杠...

另外,我一直试图阻止 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"
   }

【问题讨论】:

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

标签: windows powershell syntax file-sharing


【解决方案1】:

我必须要求用户不要在文件夹名称中添加反斜杠“\”

在第 12 行之后插入这一行:

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

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

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

从技术上讲,这不会阻止用户在文件夹名称中添加反斜杠,但如果他/她这样做了,它将删除它,这具有类似的效果。

我一直试图阻止 c:、d:、e: 等驱动器在 $Parent,但即使我使用 -in 或 -contains 它仍然允许

-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 '*:*')

【讨论】:

  • 太棒了,谢谢。我一直在试图弄清楚如何进行循环,但一直无法深入研究。
  • 我如何做一个多重while声明?我必须添加一个额外的循环吗?我尝试将while($parent -like '*:*')([string]::IsNullOrWhiteSpace($Server)) 链接起来,但似乎无法使其工作......我需要将两者合并到if 语句中吗?
  • 上面的 while 循环可以修改为继续循环空或空白条目,如下所示:while($parent -like '*:*' -or (!$parent))。旁注:[system.string]::isnullorwhitespace() 为空字符串或空字符串返回 true。在 if 和 while 等条件句中的一种简写方式是:if(!$parent)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-02-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-05-06
  • 1970-01-01
相关资源
最近更新 更多