2017-04-18 110 views
0

謝謝所有幫我弄清楚發生了什麼的人。如何讓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變量中添加一個字符串長度檢查,我完全承認除了與朋友的對話外,我還沒有研究過這個問題。

+0

定義了$ EMVType嗎?這將是一個大問題,因爲它在你的腳本中看起來是空的。您的ISE會話範圍可能會定義它,但您的腳本當然不會。 – Matt

+0

@Matt,這是一個錯字。 – Underdog

+0

這是一個錯字:「-cnotcontains」? – rrirower

回答

0

我想通了這個問題,現在我有我的代碼工作,並希望給你所有的問題的解決方案。

#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。

我確實希望這可以幫助所有發現此貼子的人。

謝謝大家的幫助。

相關問題