【问题标题】:Powershell Try Catch and retry?Powershell Try Catch 并重试?
【发布时间】:2017-06-11 22:42:23
【问题描述】:

我有这个脚本

#Change hostname
[void][System.Reflection.Assembly]::LoadWithPartialName('Microsoft.VisualBasic') 
Write-Host "Change hostname " -NoNewLine
$ComputerName = [Microsoft.VisualBasic.Interaction]::InputBox('Insert the desired computername:', 'Change hostname') 
Write-Host "- DONE" -ForegroundColor DarkGreen -BackgroundColor green -NoNewline
Write-Host " hostname = $ComputerName "
Rename-Computer -NewName $ComputerName

当计算机名有空格时,它会失败导致主机名不能有空格。 我可以阻止表单有任何空格还是有人知道在创建错误以重试时如何返回输入框

【问题讨论】:

    标签: loops powershell try-catch finally


    【解决方案1】:
    do {
        $ComputerName = [Microsoft.VisualBasic.Interaction]::InputBox('Insert the desired computername:','Change hostname')
    } while ($ComputerName -match "\s")
    

    使用do{}while() 循环并检查输入没有任何空格应该可以解决您的问题,如果您想检查任何错误,这将重新提示,直到输入有效的主机名:

    do{
        $Failed = $false
        Try{
            $ComputerName = [Microsoft.VisualBasic.Interaction]::InputBox('Insert the desired computername:', 'Change hostname') 
            Write-Host "- DONE" -ForegroundColor DarkGreen -BackgroundColor green -NoNewline
            Write-Host " hostname = $ComputerName "
            Rename-Computer -NewName $ComputerName -ErrorAction Stop
        } catch { $Failed = $true }
    } while ($Failed)
    

    【讨论】:

    • 尝试了第一种方法,那就更好了。第二种方法我不能让代码正常运行,你能给我一个例子吗?我喜欢第二个工作,因为我有更多这样的脚本,比如
    • @ThomasvanDalen 在第二位使用您的代码进行了更新,您应该可以正常运行。
    • TY 但它不起作用,当有空格时会出现错误但输入框不会回来重试
    • @ThomasvanDalen 是 powershell 中那些奇怪的东西之一,该错误不足以触发 Try-Catch,我已经在那里更新了它。
    【解决方案2】:

    对最终结果非常满意,非常感谢

    #Change hostname
    Write-Host "Change hostname " -NoNewLine
    do{
        $Failed = $false
        Try{
            $ComputerName = [Microsoft.VisualBasic.Interaction]::InputBox('Insert the desired computername:', 'Change hostname') 
            Rename-Computer -NewName $ComputerName -ErrorAction Stop
            Write-Host "- DONE -" -ForegroundColor DarkGreen -BackgroundColor green -NoNewline
            Write-Host "Hostname = $ComputerName" -ForegroundColor DarkGreen -BackgroundColor yellow
        } catch { $Failed = $true }
    
    } while ($Failed)
    
    #Change workgroupname
    Write-Host "Change Workgroup " -NoNewLine
    do{
        $Failed = $false
        Try{   
            $WorkGroup = [Microsoft.VisualBasic.Interaction]::InputBox("Insert the Workgroupname:", 'Change WorkGroupName', 'werkgroep') 
            Add-Computer -WorkGroupName $WorkGroup -ErrorAction Stop
            Write-Host "- DONE -" -ForegroundColor DarkGreen -BackgroundColor green -NoNewline
            Write-Host "Workgroup = $WorkGroup" -ForegroundColor DarkGreen -BackgroundColor yellow
        } catch { $Failed = $true }
    } while ($Failed)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-11-25
      • 1970-01-01
      • 2012-11-16
      • 2017-11-23
      • 2018-01-27
      • 1970-01-01
      • 1970-01-01
      • 2015-12-07
      相关资源
      最近更新 更多