Ниже переписать ваш код.Большую часть я оставил нетронутым, за исключением:
- Я удалил точки с запятой, потому что они не нужны в PowerShell
- Я заменил все фигурные кавычки на прямые
- Обе кнопки теперь имеют определенное значение
DialogResult
- Я изменил устаревшие
LoadWithPartialName
строки - Добавлен
$form.Dispose()
- Проверен ли выход из диалога с
OK
Я также использую командлет Start-Process
, чтобы наконец выполнить исполняемый файл, чтобы сделать код более читабельным и чтобы вы могли изучить код выхода из него, и .. 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.."
}
Надеюсь, это поможет