Powershell试试Catch并重试?

时间:2017-06-11 22:42:23

标签: loops powershell try-catch finally

我有这个脚本

#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

当计算机名称获取空格时,它会失败导致主机名无法包含空格。 我可以阻止表单有任何空格,或者有人知道如何在为重试创建错误时返回到输入框

2 个答案:

答案 0 :(得分:7)

<html>
	<head>
		<title>Text editor</title>
		<meta charset="utf-8" />
		<link href="https://fonts.googleapis.com/css?family=Roboto|Roboto+Slab" rel="stylesheet">
		<style>
			.title {
				font-family: roboto;
				text-align: center;
			}
			.text {
				font-family: roboto slab;
				text-align: justify;
				margin-bottom: 40px;
			}
			.container {
				width: 80%;
			}
			.textarea {
				width: 100%;
				height: 30em;
				text-indent: 0.5em;
			}
			.title_box {
				margin: 10px;
				width: 20em;
				padding: 10px;
				font-size: 1em;
			}
		</style>
	</head>
	<body>
		<center>
		<div class="container">
			<h1 class="title" id="title_space">Title of the page</h1>
			<p class="text" id="text_space">Content of the page.</p>
		</div>
		</center>
		<hr />
		<center><input type="text" class="title_box" placeholder="Title" id="title_box" /></center>
		<textarea class="textarea" id="text_box"></textarea>
		<input type="submit" value="Save" onclick="saveText()" />
		<input type="submit" value="br" onclick="br()" />
		
		<script>
			function saveText(){
				var title = document.getElementById("title_box").value;
				var text = document.getElementById("text_box").value;
				
				document.getElementById("title_space").innerHTML = title;
				document.getElementById("text_space").innerHTML = text;
				
			}
			function br(){
				var actualtext = document.getElementById("text_box").value;
				var processedtext = actualtext + "<br />";
				document.getElementById("text_box").innerHTML = processedtext;
			}
		</script>
		
	</body>
</html>

使用do { $ComputerName = [Microsoft.VisualBasic.Interaction]::InputBox('Insert the desired computername:','Change hostname') } while ($ComputerName -match "\s") 循环并检查输入没有任何空格可以解决您的问题,这将重新提示,直到输入有效的主机名,如果您想要检查任何错误:

do{}while()

答案 1 :(得分:1)

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

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