2017-04-18 5 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 관리자에서 실행하면 'Do-While'루프가 아무 문제없이 실행됩니다. 그러나 PowerShell non-ISE에서 Do-While.ps1 스크립트를 마우스 오른쪽 단추로 클릭하면 'Do-While'루프가 반복되어 중단되지 않습니다. 뭐라 구요?

코드를 향상시키는 방법이 있습니까? 나는 또한 $ Answer 변수에 문자열 길이 검사를 추가하는 것에 대해 궁금해하며, 나는 친구와 가진 대화 이외에 이것을 연구하지 않았 음을 충분히 인정한다.

+0

'$ EMVType'은 어디에 정의되어 있습니까? 그것이 스크립트에서 null로 보이기 때문에 큰 문제가 될 것입니다. ISE 세션 범위가 정의되어있을 수도 있지만 스크립트는 그렇지 않습니다. – Matt

+0

@Matt는 오타였습니다. – Underdog

+0

이것은 misprint입니다 : "-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') 

} 

내 단지 내하지만 내 모든 코드 '- 동안 루프를 수행'첫 번째 문은 내가 지금 PowerShell을 필요로하지 않고 관리자 PowerShell 창에서 내 모든 코드를 실행할 수있는 문제를 해결했다 'IF' ISE.

나는이 게시물에 나오는 모든 사람들에게 도움이되기를 바랍니다.

도움 주셔서 감사합니다.