如何在基于网络的PowerShell脚本中进行操作循环工作



谢谢所有帮助我弄清楚发生了什么的人。

我正在使用搜索养猪响应的代码,并验证响应不是垃圾。如果是垃圾,则do循环重复或根据响应退出。

#Get the location of this script
$ScriptPath = (Split-Path $Myinvocation.MyCommand.path -Parent)
    $TemPath = "$ScriptPath_Template"
    $NewTemPath = "$ScriptPath_New_TEMPLATE"
    $EarNotchPath = "$ScriptPathEarNocthes"
    $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-whilile"循环执行毫无问题。但是,如果我右键单击do-while.ps1 powershell non-ises中打开的``do-whil''循环重复,永不断开。什么给?

您看到了改进代码的任何方法吗?我也想知道将字符串长度检查添加到$答案变量中,我完全承认,除了与朋友进行对话外,我还没有进行过研究。

我弄清楚了问题,现在我的代码正常工作,想为您提供所有解决方案。

    #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 = "$ScriptPathEarNocthes"
        $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-whil lile-loop",而且第一个"如果"语句中的所有代码都解决了问题,我现在可以在管理员powershell窗口中运行所有代码,而无需powershell ise。p>我希望这对遇到此帖子的每个人有帮助。

谢谢大家的帮助。

相关内容

  • 没有找到相关文章

最新更新