如何让Do-While Loop在基于网络的PowerShell脚本中工作?
问题描述:
谢谢所有帮我弄清楚发生了什么的人。如何让Do-While Loop在基于网络的PowerShell脚本中工作?
我正在使用的代码搜索猪品种的响应并验证响应不是垃圾。如果它是垃圾,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 Administrator的 'DO-虽然' 循环,没有任何问题执行。但是,如果我只需右键单击在PowerShell非ISE中打开的Do-While.ps1脚本,“Do-While”循环将重复并且永不中断。是什么赋予了?
您是否看到过改进代码的方法?我也想知道如何在$ Answer变量中添加一个字符串长度检查,我完全承认除了与朋友的对话外,我还没有研究过这个问题。
答
我想通了这个问题,现在我有我的代码工作,并希望给你所有的问题的解决方案。
#Run as Adminstrator
If (-NOT ([Security.Principal.WindowsPrincipal][Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole] "Administrator"))
{
$arguments = "& '" + $myinvocation.mycommand.definition + "'"
Start-Process powershell -Verb runAs -ArgumentList $arguments
#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')
}
结束语我不只是我的“DO-while循环”,但我的所有代码的第一个“IF”语句解决了,我现在可以运行我的管理员PowerShell窗口中的所有代码,而无需PowerShell中的问题ISE。
我确实希望这可以帮助所有发现此贴子的人。
谢谢大家的帮助。
定义了$ EMVType吗?这将是一个大问题,因为它在你的脚本中看起来是空的。您的ISE会话范围可能会定义它,但您的脚本当然不会。 – Matt
@Matt,这是一个错字。 – Underdog
这是一个错字:“-cnotcontains”? – rrirower