我想将我创建的现有 powershell 脚本包装到 GUI 中,但没有花几天时间试图弄清楚我想我在这里发布。我已经为脚本的外观创建了一个原始表单,但需要帮助来隐藏它。
我运行的脚本将计算机帐户添加到给定 3 个选项的安全组。针对 localhost、计算机列表或手动输入主机名运行脚本。此外,如果可能,我想选择一个组并根据选择进行操作,而不是手动输入组名。
我的主要脚本
Import-Module ActiveDirectory
cls
$OU = "OU=TESTScripts,OU=Groups,OU=Company,DC=Company,DC=local"
$AppGroupList = Get-ADGroup -Filter {GroupCategory -eq "security"} -SearchBase "$OU"
$AppGroupList | Select-Object @{Name="AD Security Group";Expression={$_.Name}}
$title = "Add manually entered computer, localhost or list of computers to SCCM Application Install Group"
$message = "Please select an option from below:-"
$1 = New-Object System.Management.Automation.Host.ChoiceDescription "Enter &Hostname"
$2 = New-Object System.Management.Automation.Host.ChoiceDescription "Add &List of Computers"
$3 = New-Object System.Management.Automation.Host.ChoiceDescription "&Add localhost"
$options = [System.Management.Automation.Host.ChoiceDescription[]]($1, $2, $3)
$result = $host.ui.PromptForChoice($title, $message, $options, 0)
switch ($result)
{
0 {$Computers = Read-Host "Enter Hostname>"}
1 {$Computers = get-content "c:\scripts\Computers.csv"}
2 {$Computers = $env:computername}
}
$AppGroup = Read-Host "Application Security Group Name>"
$Group = Get-ADGroup -Identity $AppGroup
foreach ($Hostname in $Computers){
$member = Get-ADComputer -Identity "$Hostname" -Properties MemberOf | Select-Object MemberOf
$pc = Get-ADComputer "$Hostname"
if ($member.Memberof -like "$Group"){
Write-Host ""
Write-Host $Hostname is already a member of $AppGroup -ForegroundColor "Yellow"
}
else {
Add-ADGroupMember $AppGroup $pc
Write-Host ""
Write-Host $Hostname has been added to $AppGroup -ForegroundColor "Green"
}
}
`
我的表单代码如下所示:-
#Generated Form Function
function GenerateForm {
########################################################################
# Code Generated By: SAPIEN Technologies PrimalForms (Community Edition) v1.0.10.0
# Generated On: 22/03/2013 11:17 AM
########################################################################
#region Import the Assemblies
[reflection.assembly]::loadwithpartialname("System.Drawing") | Out-Null
[reflection.assembly]::loadwithpartialname("System.Windows.Forms") | Out-Null
#endregion
#region Generated Form Objects
$form1 = New-Object System.Windows.Forms.Form
$label3 = New-Object System.Windows.Forms.Label
$Hostname = New-Object System.Windows.Forms.Label
$Hostname = New-Object System.Windows.Forms.TextBox
$Install = New-Object System.Windows.Forms.Button
$label1 = New-Object System.Windows.Forms.Label
$Browse = New-Object System.Windows.Forms.Button
$checkBox1 = New-Object System.Windows.Forms.CheckBox
$ListApps = New-Object System.Windows.Forms.ComboBox
$InitialFormWindowState = New-Object System.Windows.Forms.FormWindowState
#endregion Generated Form Objects
#----------------------------------------------
#Generated Event Script Blocks
#----------------------------------------------
#Provide Custom Code for events specified in PrimalForms.
$handler_textBox1_TextChanged=
{
#TODO: Place custom script here
}
$Browse_OnClick=
{
#TODO: Place custom script here
}
$handler_label2_Click=
{
#TODO: Place custom script here
}
$handler_form1_Load=
{
#TODO: Place custom script here
Import-Module ActiveDirectory
$OU = "OU=TESTScripts,OU=Groups,OU=Company,DC=Company,DC=local"
$AppGroupList = Get-ADGroup -Filter {GroupCategory -eq "security"} -SearchBase "$OU"
$AppGroupList | Select-Object @{Name="AD Security Group";Expression={$_.Name}}
}
$Install_OnClick=
{
#TODO: Place custom script here
}
$handler_checkBox1_CheckedChanged=
{
#TODO: Place custom script here
}
$OnLoadForm_StateCorrection=
{#Correct the initial state of the form to prevent the .Net maximized form issue
$form1.WindowState = $InitialFormWindowState
}
#----------------------------------------------
#region Generated Form Code
$System_Drawing_Size = New-Object System.Drawing.Size
$System_Drawing_Size.Height = 423
$System_Drawing_Size.Width = 284
$form1.ClientSize = $System_Drawing_Size
$form1.DataBindings.DefaultDataSourceUpdateMode = 0
$form1.Name = "form1"
$form1.Text = "Install Software"
$form1.add_Load($handler_form1_Load)
$label3.DataBindings.DefaultDataSourceUpdateMode = 0
$System_Drawing_Point = New-Object System.Drawing.Point
$System_Drawing_Point.X = 12
$System_Drawing_Point.Y = 76
$label3.Location = $System_Drawing_Point
$label3.Name = "label3"
$System_Drawing_Size = New-Object System.Drawing.Size
$System_Drawing_Size.Height = 20
$System_Drawing_Size.Width = 185
$label3.Size = $System_Drawing_Size
$label3.TabIndex = 7
$label3.Text = "Application Install Groups:-"
$form1.Controls.Add($label3)
$Hostname.DataBindings.DefaultDataSourceUpdateMode = 0
$System_Drawing_Point = New-Object System.Drawing.Point
$System_Drawing_Point.X = 206
$System_Drawing_Point.Y = 146
$Hostname.Location = $System_Drawing_Point
$Hostname.Name = "Hostname"
$System_Drawing_Size = New-Object System.Drawing.Size
$System_Drawing_Size.Height = 29
$System_Drawing_Size.Width = 66
$Hostname.Size = $System_Drawing_Size
$Hostname.TabIndex = 6
$Hostname.Text = "Enter Hostname"
$Hostname.add_Click($handler_label2_Click)
$form1.Controls.Add($Hostname)
$Hostname.DataBindings.DefaultDataSourceUpdateMode = 0
$System_Drawing_Point = New-Object System.Drawing.Point
$System_Drawing_Point.X = 91
$System_Drawing_Point.Y = 155
$Hostname.Location = $System_Drawing_Point
$Hostname.Name = "Hostname"
$System_Drawing_Size = New-Object System.Drawing.Size
$System_Drawing_Size.Height = 20
$System_Drawing_Size.Width = 102
$Hostname.Size = $System_Drawing_Size
$Hostname.TabIndex = 5
$Hostname.add_TextChanged($handler_textBox1_TextChanged)
$form1.Controls.Add($Hostname)
$Install.DataBindings.DefaultDataSourceUpdateMode = 0
$System_Drawing_Point = New-Object System.Drawing.Point
$System_Drawing_Point.X = 10
$System_Drawing_Point.Y = 246
$Install.Location = $System_Drawing_Point
$Install.Name = "Install"
$System_Drawing_Size = New-Object System.Drawing.Size
$System_Drawing_Size.Height = 38
$System_Drawing_Size.Width = 259
$Install.Size = $System_Drawing_Size
$Install.TabIndex = 4
$Install.Text = "Install"
$Install.UseVisualStyleBackColor = $True
$Install.add_Click($Install_OnClick)
$form1.Controls.Add($Install)
$label1.DataBindings.DefaultDataSourceUpdateMode = 0
$System_Drawing_Point = New-Object System.Drawing.Point
$System_Drawing_Point.X = 91
$System_Drawing_Point.Y = 206
$label1.Location = $System_Drawing_Point
$label1.Name = "label1"
$System_Drawing_Size = New-Object System.Drawing.Size
$System_Drawing_Size.Height = 18
$System_Drawing_Size.Width = 164
$label1.Size = $System_Drawing_Size
$label1.TabIndex = 3
$label1.Text = "Browse to add list of computers"
$form1.Controls.Add($label1)
$Browse.DataBindings.DefaultDataSourceUpdateMode = 0
$System_Drawing_Point = New-Object System.Drawing.Point
$System_Drawing_Point.X = 10
$System_Drawing_Point.Y = 201
$Browse.Location = $System_Drawing_Point
$Browse.Name = "Browse"
$System_Drawing_Size = New-Object System.Drawing.Size
$System_Drawing_Size.Height = 23
$System_Drawing_Size.Width = 75
$Browse.Size = $System_Drawing_Size
$Browse.TabIndex = 2
$Browse.Text = "Browse"
$Browse.UseVisualStyleBackColor = $True
$Browse.add_Click($Browse_OnClick)
$form1.Controls.Add($Browse)
$checkBox1.DataBindings.DefaultDataSourceUpdateMode = 0
$System_Drawing_Point = New-Object System.Drawing.Point
$System_Drawing_Point.X = 12
$System_Drawing_Point.Y = 135
$checkBox1.Location = $System_Drawing_Point
$checkBox1.Name = "checkBox1"
$System_Drawing_Size = New-Object System.Drawing.Size
$System_Drawing_Size.Height = 60
$System_Drawing_Size.Width = 75
$checkBox1.Size = $System_Drawing_Size
$checkBox1.TabIndex = 1
$checkBox1.Text = "Tick LocalHost"
$checkBox1.UseVisualStyleBackColor = $True
$checkBox1.add_CheckedChanged($handler_checkBox1_CheckedChanged)
$form1.Controls.Add($checkBox1)
$ListApps.DataBindings.DefaultDataSourceUpdateMode = 0
$ListApps.FormattingEnabled = $True
$System_Drawing_Point = New-Object System.Drawing.Point
$System_Drawing_Point.X = 12
$System_Drawing_Point.Y = 108
$ListApps.Location = $System_Drawing_Point
$ListApps.Name = "ListApps"
$System_Drawing_Size = New-Object System.Drawing.Size
$System_Drawing_Size.Height = 21
$System_Drawing_Size.Width = 260
$ListApps.Size = $System_Drawing_Size
$ListApps.TabIndex = 0
$form1.Controls.Add($ListApps)
#endregion Generated Form Code
#Save the initial state of the form
$InitialFormWindowState = $form1.WindowState
#Init the OnLoad event to correct the initial state of the form
$form1.add_Load($OnLoadForm_StateCorrection)
#Show the Form
$form1.ShowDialog()| Out-Null
} #End Function
#Call the Function
GenerateForm