简体   繁体   English

在PowerShell脚本中取消按钮

[英]Cancel Button in a PowerShell Script

I have the below PowerShell Script that takes a users input and injects it into an automated SSRS install. 我有下面的PowerShell脚本,它接受用户输入并将其注入自动SSRS安装。 Everything is working as intended except for the Cancel button. 除取消按钮外,一切都按预期工作。 I would like the user to be able to click Cancel and stop the script from proceeding. 我希望用户能够单击取消并停止脚本继续。

I am very new to PowerShell and self teaching so looking for some help. 我是PowerShell和自学的新手,所以寻求一些帮助。

function button ($title,$instance,$acct,$pass) {

###################Load Assembly for creating form & button######

[void][System.Reflection.Assembly]::LoadWithPartialName( “System.Windows.Forms”)
[void][System.Reflection.Assembly]::LoadWithPartialName( “Microsoft.VisualBasic”)


#####Define the form size & placement

$form = New-Object “System.Windows.Forms.Form”;
$form.Width = 500;
$form.Height = 160;
$form.Text = $title;
$form.StartPosition = [System.Windows.Forms.FormStartPosition]::CenterScreen;

##############Define text label1
$textLabel1 = New-Object “System.Windows.Forms.Label”;
$textLabel1.Left = 25;
$textLabel1.Top = 15;

$textLabel1.Text = $instance;

##############Define text label2

$textLabel2 = New-Object “System.Windows.Forms.Label”;
$textLabel2.Left = 25;
$textLabel2.Top = 50;

$textLabel2.Text = $acct;

##############Define text label3

$textLabel3 = New-Object “System.Windows.Forms.Label”;
$textLabel3.Left = 25;
$textLabel3.Top = 85;

$textLabel3.Text = $pass;

############Define text box1 for input
$textBox1 = New-Object “System.Windows.Forms.TextBox”;
$textBox1.Left = 150;
$textBox1.Top = 10;
$textBox1.width = 200;

############Define text box2 for input

$textBox2 = New-Object “System.Windows.Forms.TextBox”;
$textBox2.Left = 150;
$textBox2.Top = 50;
$textBox2.width = 200;

############Define text box3 for input

$textBox3 = New-Object “System.Windows.Forms.TextBox”;
$TextBox3.Passwordchar = "*"
$textBox3.Left = 150;
$textBox3.Top = 90;
$textBox3.width = 200;

#############Define default values for the input boxes
$defaultValue = “”
$textBox1.Text = $defaultValue;
$textBox2.Text = $defaultValue;
$textBox3.Text = $defaultValue;

#############define OK button
$button = New-Object “System.Windows.Forms.Button”;
$button.Left = 360;
$button.Top = 85;
$button.Width = 100;
$button.Text = “Submit”;

#############define CANCEL button
$button2 = New-Object “System.Windows.Forms.Button”;
$button2.Left = 360;
$button2.Top = 45;
$button2.Width = 100;
$button2.Text = “Cancel”;

############# This is when you have to close the form after getting values
$eventHandler = [System.EventHandler]{
$textBox1.Text;
$textBox2.Text;
$textBox3.Text;
$form.Close();};

$button.Add_Click($eventHandler) ;

#############Add controls to all the above objects defined
$form.Controls.Add($button);
$form.Controls.Add($button2);
$form.Controls.Add($textLabel1);
$form.Controls.Add($textLabel2);
$form.Controls.Add($textLabel3);
$form.Controls.Add($textBox1);
$form.Controls.Add($textBox2);
$form.Controls.Add($textBox3);
$ret = $form.ShowDialog();

#################return values

return $textBox1.Text, $textBox2.Text, $textBox3.Text
}

$return= button “SSRS Configuration” “Instance Name”   “Domain\ServiceID” “Password”

#Below variables will get the values that had been entered by the user

$return[0]
$return[1]
$return[2]

D:\SQL2016\"SQL Server 2016 SP1"\Setup.exe /q /IACCEPTSQLSERVERLICENSETERMS /ACTION="install" /USEMICROSOFTUPDATE="False" /INDICATEPROGRESS /INSTANCENAME="$($return[0])" /FEATURES="RS" /RSINSTALLMODE="FilesOnlyMode" /INSTANCEDIR="D:\Program Files\Microsoft SQL Server" /RSSVCACCOUNT="$($return[1])" /RSSVCPASSWORD="$($return[2])"

Below a rewrite of your code. 下面重写你的代码。 I left most of it intact, except for: 我保留了大部分原样,除了:

  • I removed the semi-colons because they are not needed in PowerShell 我删除了分号,因为PowerShell中不需要它们
  • I replaced all curly quotes with straight ones 我用直的引号替换了所有的引号
  • Both buttons now have a defined DialogResult 这两个按钮现在都有一个已定义的DialogResult
  • I changed the deprecated LoadWithPartialName lines 我更改了已弃用的LoadWithPartialName
  • Added a $form.Dispose() 添加了$form.Dispose()
  • Checked if the dialog exited with OK 检查对话框是否退出OK

I also use the Start-Process cmdlet to finally execute the exe to make the code more readable and possible for you to examine the exit code from it, and.. INDENTED the code. 我还使用Start-Process cmdlet来最终执行exe以使代码更具可读性,并且可以从中检查退出代码,并且.. INDENTED代码。

function button ($title,$instance,$acct,$pass) {
    ###################Load Assembly for creating form & button######

    Add-Type -AssemblyName System.Windows.Forms
    Add-Type -AssemblyName Microsoft.VisualBasic


    #####Define the form size & placement

    $form = New-Object "System.Windows.Forms.Form"
    $form.Width = 500
    $form.Height = 160
    $form.Text = $title
    $form.StartPosition = [System.Windows.Forms.FormStartPosition]::CenterScreen

    ##############Define text label1
    $textLabel1 = New-Object "System.Windows.Forms.Label"
    $textLabel1.Left = 25
    $textLabel1.Top = 15
    $textLabel1.Text = $instance

    ##############Define text label2

    $textLabel2 = New-Object "System.Windows.Forms.Label"
    $textLabel2.Left = 25
    $textLabel2.Top = 50
    $textLabel2.Text = $acct

    ##############Define text label3

    $textLabel3 = New-Object "System.Windows.Forms.Label"
    $textLabel3.Left = 25
    $textLabel3.Top = 85
    $textLabel3.Text = $pass

    ############Define text box1 for input
    $textBox1 = New-Object "System.Windows.Forms.TextBox"
    $textBox1.Left = 150
    $textBox1.Top = 10
    $textBox1.width = 200

    ############Define text box2 for input

    $textBox2 = New-Object "System.Windows.Forms.TextBox"
    $textBox2.Left = 150
    $textBox2.Top = 50
    $textBox2.width = 200

    ############Define text box3 for input

    $textBox3 = New-Object "System.Windows.Forms.TextBox"
    $TextBox3.Passwordchar = "*"
    $textBox3.Left = 150
    $textBox3.Top = 90
    $textBox3.width = 200

    #############Define default values for the input boxes
    $defaultValue = ""
    $textBox1.Text = $defaultValue
    $textBox2.Text = $defaultValue
    $textBox3.Text = $defaultValue

    #############define OK button
    $button = New-Object "System.Windows.Forms.Button"
    $button.Left = 360
    $button.Top = 85
    $button.Width = 100
    $button.Text = "Submit"
    $button.DialogResult = [System.Windows.Forms.DialogResult]::OK

    #############define CANCEL button
    $button2 = New-Object "System.Windows.Forms.Button"
    $button2.Left = 360
    $button2.Top = 45
    $button2.Width = 100
    $button2.Text = "Cancel"
    $button2.DialogResult = [System.Windows.Forms.DialogResult]::Cancel

    ############# This is when you have to close the form after getting values
    $eventHandler = [System.EventHandler]{
        $textBox1.Text
        $textBox2.Text
        $textBox3.Text
        $form.Close()
    }

    $button.Add_Click($eventHandler) 

    #############Add controls to all the above objects defined
    $form.Controls.Add($button)
    $form.Controls.Add($button2)
    $form.Controls.Add($textLabel1)
    $form.Controls.Add($textLabel2)
    $form.Controls.Add($textLabel3)
    $form.Controls.Add($textBox1)
    $form.Controls.Add($textBox2)
    $form.Controls.Add($textBox3)
    $ret = $form.ShowDialog()

    $result = $null
    #################return values
    if ($ret -eq 'OK') {
        # if NOT cancelled, return whatever is in the 3 text boxes
        $result = $textBox1.Text, $textBox2.Text, $textBox3.Text
    }

    # Dispose of the form
    $form.Dispose()

    return $result
}

$return= button "SSRS Configuration" "Instance Name" "Domain\ServiceID" "Password"

# test if the function returned anything, otherwise it was cancelled
if ($return) {
    #Below variables will get the values that had been entered by the user

    $return[0]
    $return[1]
    $return[2]

    # for better readability, create the arguments for the Setup.exe as array

    $arguments = '/q', 
                 '/IACCEPTSQLSERVERLICENSETERMS', 
                 '/ACTION="install"', 
                 '/USEMICROSOFTUPDATE="False"', 
                 '/INDICATEPROGRESS', 
                 ('/INSTANCENAME="{0}"' -f $return[0]), 
                 '/FEATURES="RS"', 
                 '/RSINSTALLMODE="FilesOnlyMode"', 
                 '/INSTANCEDIR="D:\Program Files\Microsoft SQL Server"', 
                 ('/RSSVCACCOUNT="{0}"'  -f $return[1]), 
                 ('/RSSVCPASSWORD="{0}"' -f $return[2])

    $proc = Start-Process -FilePath "D:\SQL2016\SQL Server 2016 SP1\Setup.exe" -ArgumentList $arguments -PassThru -Wait

    # you can examine the processes ExitCode using:
    Write-Host "The process returned ExitCode: $($proc.ExitCode)"
}
else {
    Write-Host "User cancelled the dialog.."
}

Hope that helps 希望有所帮助

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM