簡體   English   中英

在PowerShell腳本中取消按鈕

[英]Cancel Button in a PowerShell Script

我有下面的PowerShell腳本,它接受用戶輸入並將其注入自動SSRS安裝。 除取消按鈕外,一切都按預期工作。 我希望用戶能夠單擊取消並停止腳本繼續。

我是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])"

下面重寫你的代碼。 我保留了大部分原樣,除了:

  • 我刪除了分號,因為PowerShell中不需要它們
  • 我用直的引號替換了所有的引號
  • 這兩個按鈕現在都有一個已定義的DialogResult
  • 我更改了已棄用的LoadWithPartialName
  • 添加了$form.Dispose()
  • 檢查對話框是否退出OK

我還使用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.."
}

希望有所幫助

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM