【发布时间】:2017-04-18 18:43:03
【问题描述】:
感谢所有帮助我弄清楚发生了什么的人。
我使用的代码搜索猪品种的响应并验证响应不是垃圾。如果它是垃圾,do 循环会根据响应重复或退出。
#Get the location of this script
$ScriptPath = (Split-Path $Myinvocation.MyCommand.path -Parent)
$TemPath = "$ScriptPath\_Template"
$NewTemPath = "$ScriptPath\_New_TEMPLATE"
$EarNotchPath = "$ScriptPath\EarNocthes"
$BoarPath = "$ScriptPath\_Boar_Samples"
$SowPath = "$ScriptPath\_Sow_Samples"
$GiltPath = "$ScriptPath\_Gilt_Samples"
$BarrowPath = "$ScriptPath\_Barrow_Samples"
$LittersPath = "$ScriptPath\_Litters_Samples"
Do {
#Create variables and collect information from user of script.
$CUST= Read-Host 'Enter Customer Name '
$BoarPath= Read-Host 'Enter Selected Boar Name (example: WildHog)'
$SowPath= Read-Host 'Enter Selected Sow Name (example: TrueBlue)'
$HogBreeds= Read-Host 'Enter Hereford, Yorkshire, Hampshire, Duroc, China_Poland'
#@Error Check $HogBreeds
If ('Hereford', 'Yorkshire', 'Hampshire', 'Duroc', 'China_Poland' -cnotcontains $HogBreeds){
If ('Hereford', 'Yorkshire', 'Hampshire', 'Duroc', 'China_Poland' -contains $HogBreeds){
$TextInfo = (Get-Culture).TextInfo
Switch ($HogBreeds) {
{$_ -in 'Hereford','Yorkshire','Duroc'} { $HogBreeds = $HogBreeds.ToUpper() }
'CHINA_Poland' { $HogBreeds = $HogBreeds.Substring(0,7).ToUpper() + $HogBreeds.Substring(7,5).ToLower() }
}
$Restart = 'StopDoLoop'
} Else {
Write-Warning 'You didnt enter one of: Hereford, Yorkshire, Hampshire, Duroc, China_Poland or Your response was not recongized.'
$ANSWER = Read-Host 'Do you want to start over (Yes/No) - type Yes or No'
If ($ANSWER -eq 'Yes') { $Restart = 'StopDoLoop'}
If ($ANSWER -eq 'No') { Exit }
}
}
} Until ($Restart -eq 'StopDoLoop')
如果我使用 Windows PowerShell ISE 管理员运行此代码,“Do-While”循环将毫无问题地执行。但是,如果我只是右键单击在 PowerShell 非 ISE 中打开的 Do-While.ps1 脚本,则“Do-While”循环会重复并且永不中断。什么给了?
您有什么方法可以改进代码吗?我还想知道向 $Answer 变量添加字符串长度检查,我完全承认除了与朋友的对话外,我没有研究过这个。
【问题讨论】:
-
$EMVType在哪里定义?这将是一个大问题,因为它在您的脚本中看起来为空。您的 ISE 会话范围可能已定义,但您的脚本肯定没有。 -
@Matt 打错了。
-
这是不是印刷错误:“-cnotcontains”?
-
不一定。 c 使其区分大小写。
-
这不是印刷错误或错字,这是@MarkWragg 确定的原因。
标签: powershell do-while mapped-drive