Reputation: 46
I want to wrap an existing powershell script I've created in to a GUI but without sending days on it trying to figure it out I thought I post here. I have created a from in Primal forms for what the script should look like but need assistance in coverting it.
The script I run adds computer accounts to a security group given 3 options. Run the script against the localhost, a list of computers or manually entering the hostname. Additionally I'd like to select a group and act on the selection rather than entering the group name manually if possible.
My main script
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"
}
}
`
My form code looks like this:-
#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
Upvotes: 0
Views: 3679
Reputation: 46
This is the complete code for converting the above script into a GUI.
#Generated Form Function
function GenerateForm {
########################################################################
# Code Generated By: SAPIEN Technologies PrimalForms (Community Edition) v1.0.10.0
# Generated On: 25/03/2013 4:56 PM
# Generated By: MrMeaner
########################################################################
Import-Module ActiveDirectory
#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
$radioButton3 = New-Object System.Windows.Forms.RadioButton
$radioButton2 = New-Object System.Windows.Forms.RadioButton
$radioButton1 = New-Object System.Windows.Forms.RadioButton
$label3 = New-Object System.Windows.Forms.Label
$textHostname = New-Object System.Windows.Forms.Label
$txtHostname = 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
$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.
$Browse_OnClick=
{
#When Browse button is clicked
$FileName = Select-FileDialog
$Computers = Get-Content $FileName
$Install.enabled = $true
}
$Install_OnClick=
{
#Add computer to security group script
if (!$ListApps.SelectedItem) {[System.Windows.Forms.MessageBox]::Show("Please select an SCCM application install group name from the drop down menu")
}
else {
$AppGroup = $ListApps.SelectedItem.ToString()
$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"){
[System.Windows.Forms.MessageBox]::Show("$Hostname is already a member of $AppGroup")
}
else {
Add-ADGroupMember $AppGroup $pc
[System.Windows.Forms.MessageBox]::Show("$Hostname has been added to $AppGroup")
}
}
}
}
$handler_radioButton1_CheckedChanged=
{
#Localhost radio button
$txtHostname.enabled = $false
$Browse.enabled = $false
$Computers = $env:computername
$Install.enabled = $true
}
$handler_radioButton2_CheckedChanged=
{
#Enter Hostname radiobutton
$txtHostname.enabled = $true
$Browse.enabled = $false
}
$handler_radioButton3_CheckedChanged=
{
#Load list of computers radiobutton
$Browse.enabled = $true
$txtHostname.enabled = $false
}
$handler_textBox1_TextChanged=
{
#Hostname textbox
$Computers = $txtHostname.Text.ToString()
$Install.enabled = $true
}
$handler_form1_Load=
{
#Runs when script is loaded
$Install.enabled = $false
$OU = "OU=TESTScripts,OU=Groups,OU=Company,DC=Company,DC=local"
$AppGroupList = Get-ADGroup -Filter {GroupCategory -eq "security"} -SearchBase "$OU" | Select -expand Name
#Fill the list with the groupnames
$AppGroupList | % { $ListApps.Items.Add($_) }
}
$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 = 271
$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)
$radioButton3.DataBindings.DefaultDataSourceUpdateMode = 0
$System_Drawing_Point = New-Object System.Drawing.Point
$System_Drawing_Point.X = 188
$System_Drawing_Point.Y = 58
$radioButton3.Location = $System_Drawing_Point
$radioButton3.Name = "radioButton3"
$System_Drawing_Size = New-Object System.Drawing.Size
$System_Drawing_Size.Height = 51
$System_Drawing_Size.Width = 88
$radioButton3.Size = $System_Drawing_Size
$radioButton3.TabIndex = 10
$radioButton3.TabStop = $True
$radioButton3.Text = "Load List of Computers"
$radioButton3.UseVisualStyleBackColor = $True
$radioButton3.add_CheckedChanged($handler_radioButton3_CheckedChanged)
$form1.Controls.Add($radioButton3)
$radioButton2.DataBindings.DefaultDataSourceUpdateMode = 0
$System_Drawing_Point = New-Object System.Drawing.Point
$System_Drawing_Point.X = 102
$System_Drawing_Point.Y = 54
$radioButton2.Location = $System_Drawing_Point
$radioButton2.Name = "radioButton2"
$System_Drawing_Size = New-Object System.Drawing.Size
$System_Drawing_Size.Height = 58
$System_Drawing_Size.Width = 80
$radioButton2.Size = $System_Drawing_Size
$radioButton2.TabIndex = 9
$radioButton2.TabStop = $True
$radioButton2.Text = "Enter Hostname"
$radioButton2.UseVisualStyleBackColor = $True
$radioButton2.add_CheckedChanged($handler_radioButton2_CheckedChanged)
$form1.Controls.Add($radioButton2)
$radioButton1.DataBindings.DefaultDataSourceUpdateMode = 0
$System_Drawing_Point = New-Object System.Drawing.Point
$System_Drawing_Point.X = 7
$System_Drawing_Point.Y = 59
$radioButton1.Location = $System_Drawing_Point
$radioButton1.Name = "radioButton1"
$System_Drawing_Size = New-Object System.Drawing.Size
$System_Drawing_Size.Height = 48
$System_Drawing_Size.Width = 76
$radioButton1.Size = $System_Drawing_Size
$radioButton1.TabIndex = 8
$radioButton1.TabStop = $True
$radioButton1.Text = "LocalHost"
$radioButton1.UseVisualStyleBackColor = $True
$radioButton1.add_CheckedChanged($handler_radioButton1_CheckedChanged)
$form1.Controls.Add($radioButton1)
$label3.DataBindings.DefaultDataSourceUpdateMode = 0
$System_Drawing_Point = New-Object System.Drawing.Point
$System_Drawing_Point.X = 7
$System_Drawing_Point.Y = 9
$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 = "SCCM Application Install Groups:-"
$form1.Controls.Add($label3)
$textHostname.DataBindings.DefaultDataSourceUpdateMode = 0
$System_Drawing_Point = New-Object System.Drawing.Point
$System_Drawing_Point.X = 140
$System_Drawing_Point.Y = 113
$textHostname.Location = $System_Drawing_Point
$textHostname.Name = "textHostname"
$System_Drawing_Size = New-Object System.Drawing.Size
$System_Drawing_Size.Height = 27
$System_Drawing_Size.Width = 105
$textHostname.Size = $System_Drawing_Size
$textHostname.TabIndex = 6
$textHostname.Text = "Enter Hostname"
$textHostname.add_Click($handler_label2_Click)
$form1.Controls.Add($textHostname)
$txtHostname.DataBindings.DefaultDataSourceUpdateMode = 0
$System_Drawing_Point = New-Object System.Drawing.Point
$System_Drawing_Point.X = 7
$System_Drawing_Point.Y = 113
$txtHostname.Location = $System_Drawing_Point
$txtHostname.Name = "txtHostname"
$System_Drawing_Size = New-Object System.Drawing.Size
$System_Drawing_Size.Height = 20
$System_Drawing_Size.Width = 124
$txtHostname.Size = $System_Drawing_Size
$txtHostname.TabIndex = 5
$txtHostname.add_Leave($handler_textBox1_TextChanged)
$form1.Controls.Add($txtHostname)
$Install.DataBindings.DefaultDataSourceUpdateMode = 0
$System_Drawing_Point = New-Object System.Drawing.Point
$System_Drawing_Point.X = 7
$System_Drawing_Point.Y = 181
$Install.Location = $System_Drawing_Point
$Install.Name = "Install"
$System_Drawing_Size = New-Object System.Drawing.Size
$System_Drawing_Size.Height = 82
$System_Drawing_Size.Width = 269
$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 = 140
$System_Drawing_Point.Y = 155
$label1.Location = $System_Drawing_Point
$label1.Name = "label1"
$System_Drawing_Size = New-Object System.Drawing.Size
$System_Drawing_Size.Height = 23
$System_Drawing_Size.Width = 115
$label1.Size = $System_Drawing_Size
$label1.TabIndex = 3
$label1.Text = "Load computer list"
$form1.Controls.Add($label1)
$Browse.DataBindings.DefaultDataSourceUpdateMode = 0
$System_Drawing_Point = New-Object System.Drawing.Point
$System_Drawing_Point.X = 7
$System_Drawing_Point.Y = 150
$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 = 124
$Browse.Size = $System_Drawing_Size
$Browse.TabIndex = 2
$Browse.Text = "Browse"
$Browse.UseVisualStyleBackColor = $True
$Browse.add_Click($Browse_OnClick)
$form1.Controls.Add($Browse)
$ListApps.DataBindings.DefaultDataSourceUpdateMode = 0
$ListApps.FormattingEnabled = $True
$System_Drawing_Point = New-Object System.Drawing.Point
$System_Drawing_Point.X = 7
$System_Drawing_Point.Y = 32
$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 = 269
$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
#Select File Explorer Function
function Select-FileDialog {
[void] [System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")
$ofd = New-Object System.Windows.Forms.OpenFileDialog
$ofd.InitialDirectory = "d:\scripts"
$ofd.ShowHelp=$true
if($ofd.ShowDialog() -eq "OK") { $ofd.FileName }
} #End Function
#Call the Function
GenerateForm
Upvotes: 0
Reputation: 54821
This question is too wide. Please ask for a specific problem and try yourself. If you know basic powerhsell and/or basic c# you will come further. To help you populate the app groups list as you specifically wanted, try:
$handler_form1_Load=
{
#TODO: Place custom script here
#I would move this import line out of the form and to the top of the script
Import-Module ActiveDirectory
$OU = "OU=TESTScripts,OU=Groups,OU=Metro Trains,DC=Metrotrains,DC=local"
$AppGroupList = Get-ADGroup -Filter {GroupCategory -eq "security"} -SearchBase "$OU" | Select -expand Name
#Fill the list with the groupnames
$AppGroupList | % { $ListApps.Items.Add($_) }
}
You should look into disabling some of the controls when one is used. Ex. if the localhost checkbox is checked, disable the textbox and the browse-button (if you don't need to ability to use all three). Add a click
eventhandler to the Install-button that takes the current values from the form and runs the "add computer to group"-code.
Upvotes: 1
Reputation: 2166
I'm no good when it comes to the Windows Presentation Foundation stuff or WinForms. I like it when PowerShell takes care of me. I hope this is something you can use:
$OU = Get-ADOrganizationalUnit -Filter * | Out-GridView -PassThru -Title 'Choose OU'
Upvotes: 0