Powershell Try Catch 并重试?
Powershell Try Catch and retry?
我有这个脚本
#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
当计算机名有空格时,它会失败,因为主机名不能有空格。
我可以阻止表单以包含任何空格吗?或者有人知道如何在重试时创建错误时返回输入框
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)
对最终结果非常满意,非常感谢
#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)
我有这个脚本
#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
当计算机名有空格时,它会失败,因为主机名不能有空格。 我可以阻止表单以包含任何空格吗?或者有人知道如何在重试时创建错误时返回输入框
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)
对最终结果非常满意,非常感谢
#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)