Powershell脚本 - 要求用户输入,重新运行该部分直到输入正确

时间:2018-03-29 18:03:24

标签: powershell input dialog

我在PowerShell中编写了一个非常广泛的300多行用户添加脚本,专为非管理员设计,无需管理员权限(自行提升)即可将用户添加到Windows域,而无需进入ADUC。

有一个部分(下面)要求提供电话号码,清理输入并正确格式化以便同步到Azure / O365。它还检查以确保输入为办公室和手机的10位数,如果他们没有抛出错误对话框。我想找到一种方法,不仅可以抛出错误框,还可以让脚本返回电话输入代码的顶部,让他们在按下OK按钮后重新输入数字。这就是我不知所措 - 让它回到该部分的顶部。

提前致谢! =)

    # Phone code begins here
$phoneform = New-Object System.Windows.Forms.Form
$phoneform.Text = 'Phone numbers'
$phoneform.Size = '300, 200'
$phoneform.StartPosition = "CenterScreen"
$OKButton = New-Object System.Windows.Forms.Button
$OKButton.Location = New-Object System.Drawing.Size(75, 120)
$OKButton.Size = '75, 23'
$OKButton.Text = 'OK'
$OKButton.DialogResult = 'Ok'
$phoneform.Controls.Add($OKButton)
$CancelButton = New-Object System.Windows.Forms.Button
$CancelButton.Location = '160, 120'
$CancelButton.Size = '75, 23'
$CancelButton.Text = 'Cancel'
$CancelButton.DialogResult = 'Cancel'
$phoneform.Controls.Add($CancelButton)
$phoneform.Topmost = $True
$phoneform.Add_Shown({ $phoneform.Activate() })

$objLabel = New-Object System.Windows.Forms.Label
$objLabel.Location = New-Object System.Drawing.Size(10,20) 
$objLabel.Size = New-Object System.Drawing.Size(280,20) 
$objLabel.Text = "Work and mobile numbers for $IHIfirstname $IHIlastname"
$phoneform.Controls.Add($objLabel) 

$objLabel = New-Object System.Windows.Forms.Label
$objLabel.Location = New-Object System.Drawing.Size(40,42) 
$objLabel.Size = New-Object System.Drawing.Size(50,20) 
$objLabel.Text = "Work"
$phoneform.Controls.Add($objLabel) 

$objLabel = New-Object System.Windows.Forms.Label
$objLabel.Location = New-Object System.Drawing.Size(40,72) 
$objLabel.Size = New-Object System.Drawing.Size(60,20) 
$objLabel.Text = "Cellphone"
$phoneform.Controls.Add($objLabel) 

$objTextBox = New-Object System.Windows.Forms.TextBox 
$objTextBox.Location = New-Object System.Drawing.Size(100,40) 
$objTextBox.Size = New-Object System.Drawing.Size(160,20) 
$phoneform.Controls.Add($objTextBox)

$objTextBox2 = New-Object System.Windows.Forms.TextBox 
$objTextBox2.Location = New-Object System.Drawing.Size(100,70) 
$objTextBox2.Size = New-Object System.Drawing.Size(160,20) 
$phoneform.Controls.Add($objTextBox2) 

if('Ok' -eq $phoneform.ShowDialog()){
    #write-host "form OK button clicked"
}else{
    #Write-Host 'Form cancelled'
exit
}

$worknumber = $objTextBox.text
$cellnumber = $objTextBox2.text
$worknumber = $worknumber -replace '[()-. ]',''
$cellnumber = $cellnumber -replace '[()-. ]',''
#write-host $worknumber
#write-host $cellnumber
$worknumber.Length
$cellnumber.Length
if ($worknumber.Length -eq 0) {$worknumber = "0000000000"}
if ($cellnumber.Length -eq 0) {$cellnumber = "0000000000"}

if (($worknumber.Length -eq 10) -and ($cellnumber.Length -eq 10)) {
#write-host "both match"
$worknumber = $worknumber.Insert(0,"(")
$worknumber = $worknumber.Insert(4,")")
$worknumber = $worknumber.Insert(5," ")
$worknumber = $worknumber.Insert(9,"-")

$cellnumber = $cellnumber.Insert(0,"(")
$cellnumber = $cellnumber.Insert(4,")")
$cellnumber = $cellnumber.Insert(5," ")
$cellnumber = $cellnumber.Insert(9,"-") }
Else {
write-host "One of these is not 10 digits"
$a= New-Object -ComObject wscript.shell
$numanswer = $a.popup("
Not enough digits entered
Work Phone - $worknumber
Cell Phone -  $cellnumber", `
0,"Error",0)
If ($numanswer -eq 6) {
exit }

}
# Phone code ends here

2 个答案:

答案 0 :(得分:0)

如上面的评论所述,如果您将代码包装在do /中,直到您可以循环允许用户重试输入数据。请参阅下面的更新脚本。 (另外,由于其他一切都是Winforms,我修改了脚本以使用Winforms MessageBox类而不是调用wscript.shell。)

do {

# Phone code begins here
$phoneform = New-Object System.Windows.Forms.Form
$phoneform.Text = 'Phone numbers'
$phoneform.Size = '300, 200'
$phoneform.StartPosition = "CenterScreen"
$OKButton = New-Object System.Windows.Forms.Button
$OKButton.Location = New-Object System.Drawing.Size(75, 120)
$OKButton.Size = '75, 23'
$OKButton.Text = 'OK'
$OKButton.DialogResult = 'Ok'
$phoneform.Controls.Add($OKButton)
$CancelButton = New-Object System.Windows.Forms.Button
$CancelButton.Location = '160, 120'
$CancelButton.Size = '75, 23'
$CancelButton.Text = 'Cancel'
$CancelButton.DialogResult = 'Cancel'
$phoneform.Controls.Add($CancelButton)
$phoneform.Topmost = $True
$phoneform.Add_Shown({ $phoneform.Activate() })

$objLabel = New-Object System.Windows.Forms.Label
$objLabel.Location = New-Object System.Drawing.Size(10,20) 
$objLabel.Size = New-Object System.Drawing.Size(280,20) 
$objLabel.Text = "Work and mobile numbers for $IHIfirstname $IHIlastname"
$phoneform.Controls.Add($objLabel) 

$objLabel = New-Object System.Windows.Forms.Label
$objLabel.Location = New-Object System.Drawing.Size(40,42) 
$objLabel.Size = New-Object System.Drawing.Size(50,20) 
$objLabel.Text = "Work"
$phoneform.Controls.Add($objLabel) 

$objLabel = New-Object System.Windows.Forms.Label
$objLabel.Location = New-Object System.Drawing.Size(40,72) 
$objLabel.Size = New-Object System.Drawing.Size(60,20) 
$objLabel.Text = "Cellphone"
$phoneform.Controls.Add($objLabel) 

$objTextBox = New-Object System.Windows.Forms.TextBox 
$objTextBox.Location = New-Object System.Drawing.Size(100,40) 
$objTextBox.Size = New-Object System.Drawing.Size(160,20) 
$phoneform.Controls.Add($objTextBox)

$objTextBox2 = New-Object System.Windows.Forms.TextBox 
$objTextBox2.Location = New-Object System.Drawing.Size(100,70) 
$objTextBox2.Size = New-Object System.Drawing.Size(160,20) 
$phoneform.Controls.Add($objTextBox2) 

if('Ok' -eq $phoneform.ShowDialog()){
    #write-host "form OK button clicked"
}else{
    #Write-Host 'Form cancelled'
    exit
}

$worknumber = $objTextBox.text
$cellnumber = $objTextBox2.text
$worknumber = $worknumber -replace '[()-. ]',''
$cellnumber = $cellnumber -replace '[()-. ]',''
#write-host $worknumber
#write-host $cellnumber
$worknumber.Length
$cellnumber.Length
if ($worknumber.Length -eq 0) {$worknumber = "0000000000"}
if ($cellnumber.Length -eq 0) {$cellnumber = "0000000000"}

if (($worknumber.Length -eq 10) -and ($cellnumber.Length -eq 10)) {
    #write-host "both match"
    $worknumber = $worknumber.Insert(0,"(")
    $worknumber = $worknumber.Insert(4,")")
    $worknumber = $worknumber.Insert(5," ")
    $worknumber = $worknumber.Insert(9,"-")

    $cellnumber = $cellnumber.Insert(0,"(")
    $cellnumber = $cellnumber.Insert(4,")")
    $cellnumber = $cellnumber.Insert(5," ")
    $cellnumber = $cellnumber.Insert(9,"-") 
}
Else 
{
    # Use Winforms MessageBox class instead of VBScript Dialog()
    $result = [System.Windows.Forms.MessageBox]::Show(
        "Not enough digits entered`nWork Phone - $worknumber`nCell Phone -  $cellnumber", 
        "Invalid Phone Number", 
        [System.Windows.Forms.MessageBoxButtons]::RetryCancel)
    if ($result -eq "Retry")
    {
        continue
    }
    else
    {
        exit
    }
}

} until ($false)

答案 1 :(得分:0)

您还可以将电话号码解析转换为函数,并在数据未验证您的方式时以递归方式调用。然后只需调用该函数并从返回的对象中提取worknumbercellnumber属性。如果他们点击取消,则返回$false(然后$return.worknumber将不会给出任何结果)因此可以验证它是否为假或仅以空白数据结束(取决于您需要的是什么)那个脚本要做。

function Get-PhoneNumbers {
    # Create form to obtain phone numbers
    $phoneform = New-Object System.Windows.Forms.Form
    $phoneform.Text = 'Phone numbers'
    $phoneform.Size = '300, 200'
    $phoneform.StartPosition = "CenterScreen"
    $OKButton = New-Object System.Windows.Forms.Button
    $OKButton.Location = New-Object System.Drawing.Size(75, 120)
    $OKButton.Size = '75, 23'
    $OKButton.Text = 'OK'
    $OKButton.DialogResult = 'Ok'
    $phoneform.Controls.Add($OKButton)
    $CancelButton = New-Object System.Windows.Forms.Button
    $CancelButton.Location = '160, 120'
    $CancelButton.Size = '75, 23'
    $CancelButton.Text = 'Cancel'
    $CancelButton.DialogResult = 'Cancel'
    $phoneform.Controls.Add($CancelButton)
    $phoneform.Topmost = $True
    $phoneform.Add_Shown( { $phoneform.Activate() })

    $objLabel = New-Object System.Windows.Forms.Label
    $objLabel.Location = New-Object System.Drawing.Size(10, 20) 
    $objLabel.Size = New-Object System.Drawing.Size(280, 20) 
    $objLabel.Text = "Work and mobile numbers for $IHIfirstname $IHIlastname"
    $phoneform.Controls.Add($objLabel) 

    $objLabel = New-Object System.Windows.Forms.Label
    $objLabel.Location = New-Object System.Drawing.Size(40, 42) 
    $objLabel.Size = New-Object System.Drawing.Size(50, 20) 
    $objLabel.Text = "Work"
    $phoneform.Controls.Add($objLabel) 

    $objLabel = New-Object System.Windows.Forms.Label
    $objLabel.Location = New-Object System.Drawing.Size(40, 72) 
    $objLabel.Size = New-Object System.Drawing.Size(60, 20) 
    $objLabel.Text = "Cellphone"
    $phoneform.Controls.Add($objLabel) 

    $objTextBox = New-Object System.Windows.Forms.TextBox 
    $objTextBox.Location = New-Object System.Drawing.Size(100, 40) 
    $objTextBox.Size = New-Object System.Drawing.Size(160, 20) 
    $phoneform.Controls.Add($objTextBox)

    $objTextBox2 = New-Object System.Windows.Forms.TextBox 
    $objTextBox2.Location = New-Object System.Drawing.Size(100, 70) 
    $objTextBox2.Size = New-Object System.Drawing.Size(160, 20) 
    $phoneform.Controls.Add($objTextBox2)

    # Exit on Cancel rather than proceed on Ok to remove need for else.    
    if ('Cancel' -eq $phoneform.ShowDialog()) {
        return $false
    }

    # Do validation here
    $worknumber = $objTextBox.text
    $cellnumber = $objTextBox2.text
    $worknumber = $worknumber -replace '[()-. ]', ''
    $cellnumber = $cellnumber -replace '[()-. ]', ''
    #write-host $worknumber
    #write-host $cellnumber
    if ($worknumber.Length -eq 0) {$worknumber = "0000000000"}
    if ($cellnumber.Length -eq 0) {$cellnumber = "0000000000"}

    if (($worknumber.Length -eq 10) -and ($cellnumber.Length -eq 10)) {
        #write-host "both match"
        $worknumber = $worknumber.Insert(0, "(")
        $worknumber = $worknumber.Insert(4, ")")
        $worknumber = $worknumber.Insert(5, " ")
        $worknumber = $worknumber.Insert(9, "-")

        $cellnumber = $cellnumber.Insert(0, "(")
        $cellnumber = $cellnumber.Insert(4, ")")
        $cellnumber = $cellnumber.Insert(5, " ")
        $cellnumber = $cellnumber.Insert(9, "-")
        # Return now so that only a valid result returns.
        # Previous attempts will be dropped.
        return [pscustomobject]@{
            WorkNumber = $worknumber
            CellNumber = $cellnumber
        }
    }
    Else {
        # Use Winforms MessageBox class instead of VBScript Dialog()
        $result = [System.Windows.Forms.MessageBox]::Show(
            "Not enough digits entered`nWork Phone - $worknumber`nCell Phone -  $cellnumber", 
            "Invalid Phone Number", 
            [System.Windows.Forms.MessageBoxButtons]::RetryCancel)
        if ($result -eq "Retry") {
            Get-PhoneNumbers
        }
        else {
            return $false
        }
    }
}