【问题标题】:How to get Do-While Loop to work in network based PowerShell script?如何让 Do-While 循环在基于网络的 PowerShell 脚本中工作?
【发布时间】: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


【解决方案1】:

我发现了问题,现在我的代码可以正常工作,并希望为您提供问题的所有解决方案。

    #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-Loop”包装,而且将我的所有代码包装在第一个“IF”语句中,解决了我现在可以在管理员 PowerShell 窗口中运行所有代码而不需要 PowerShell ISE 的问题。

我希望这对看到这篇文章的每个人都有帮助。

感谢大家的帮助。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-01-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多