I am trying to add a PowerShell script into a PrimalForm GUI and seem to be having difficulty getting it work. The script allows you to update Active Directory description on multiple machines listed on a .CSV file. Below is the script I am trying to add:
<#Update computer description
Open notepad and create a .CSV file. Add ComputerName,Description as the headers.
Below the headers type each computer name and description separated by a comma.
EXAMPLE:
ComputerName,Description
HORMEDTKWK40797,Test_Description
HORMEDTKTB47144,Test_Description
#>
Import-Csv "C:\scripts\computerdescription.csv" | % {
$computer = $_.computername
$Desc = $_.Description
set-ADComputer $computer -Description $Desc
}
Below is the Primalform GUI format. The GUI gives you three options: Update a localhost AD Description, type a computer name on the form or browse to a .CSV file. Thanks!
#Generated Form Function
function GenerateForm {
########################################################################
# Code Generated By: SAPIEN Technologies PrimalForms (Community Edition) v1.0.10.0
# Generated On: 4/6/2014 4:01 PM
# Generated By:
########################################################################
#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
$Install = New-Object System.Windows.Forms.Button
$label3 = New-Object System.Windows.Forms.Label
$Browse = New-Object System.Windows.Forms.Button
$label2 = New-Object System.Windows.Forms.Label
$txtComputername = New-Object System.Windows.Forms.TextBox
$radioButton3 = New-Object System.Windows.Forms.RadioButton
$radioButton2 = New-Object System.Windows.Forms.RadioButton
$radioButton1 = New-Object System.Windows.Forms.RadioButton
$txtDescription = New-Object System.Windows.Forms.TextBox
$label1 = New-Object System.Windows.Forms.Label
$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
$Computer = Get-Content $FileName
$Install.enabled = $true
}
$Install_OnClick=
{
#Update AD Description of computer
Foreach ($pc in $Computer) {
$Desc = $txtDescription
set-ADComputer $pc -Description $Desc
}
}
$handler_radioButton1_CheckedChanged=
{
#Localhost radio button
$txtComputername.enabled = $false
$Browse.enabled = $false
$Computer = $env:computername
$Install.enabled = $true
}
$handler_radioButton2_CheckedChanged=
{
#Enter Hostname radiobutton
$txtComputername.enabled = $true
$Browse.enabled = $false
}
$handler_radioButton3_CheckedChanged=
{
#Load list of computers radiobutton
$Browse.enabled = $true
$txtComputername.enabled = $false
}
$handler_textBox1_TextChanged=
{
#Hostname textbox
$Computer = $txtComputername.Text.ToString()
$Install.enabled = $true
#---------------------------------------------- $System_Drawing_Point = New-Object System.Drawing.Point $form1.Controls.Add($Install) $label3.DataBindings.DefaultDataSourceUpdateMode = 0 $System_Drawing_Point = New-Object System.Drawing.Point $form1.Controls.Add($label3) $System_Drawing_Point = New-Object System.Drawing.Point $form1.Controls.Add($Browse) $label2.DataBindings.DefaultDataSourceUpdateMode = 0 $System_Drawing_Point = New-Object System.Drawing.Point $form1.Controls.Add($label2) $txtComputername.DataBindings.DefaultDataSourceUpdateMode = 0 $form1.Controls.Add($txtComputername) $System_Drawing_Point = New-Object System.Drawing.Point $form1.Controls.Add($radioButton3) $System_Drawing_Point = New-Object System.Drawing.Point $form1.Controls.Add($radioButton2) $System_Drawing_Point = New-Object System.Drawing.Point $form1.Controls.Add($radioButton1) $txtDescription.DataBindings.DefaultDataSourceUpdateMode = 0 $form1.Controls.Add($txtDescription) $label1.DataBindings.DefaultDataSourceUpdateMode = 0 $System_Drawing_Point = New-Object System.Drawing.Point $form1.Controls.Add($label1) #endregion Generated Form Code #Save the initial state of the form } #End Function #Select File Explorer Function [void] [System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms") } #Call the Function
}
$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 = 288
$System_Drawing_Size.Width = 517
$form1.ClientSize = $System_Drawing_Size
$form1.DataBindings.DefaultDataSourceUpdateMode = 0
$form1.Name = "form1"
$form1.Text = "Computer Description"
$Install.DataBindings.DefaultDataSourceUpdateMode = 0
$System_Drawing_Point.X = 333
$System_Drawing_Point.Y = 207
$Install.Location = $System_Drawing_Point
$Install.Name = "Install"
$System_Drawing_Size = New-Object System.Drawing.Size
$System_Drawing_Size.Height = 23
$System_Drawing_Size.Width = 93
$Install.Size = $System_Drawing_Size
$Install.TabIndex = 9
$Install.Text = "Install"
$Install.UseVisualStyleBackColor = $True
$Install.add_Click($Install_OnClick)
$System_Drawing_Point.X = 116
$System_Drawing_Point.Y = 208
$label3.Location = $System_Drawing_Point
$label3.Name = "label3"
$System_Drawing_Size = New-Object System.Drawing.Size
$System_Drawing_Size.Height = 23
$System_Drawing_Size.Width = 100
$label3.Size = $System_Drawing_Size
$label3.TabIndex = 8
$label3.Text = "Load Computer List"
$label3.TextAlign = 16
$Browse.DataBindings.DefaultDataSourceUpdateMode = 0
$System_Drawing_Point.X = 13
$System_Drawing_Point.Y = 208
$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 = 93
$Browse.Size = $System_Drawing_Size
$Browse.TabIndex = 2
$Browse.Text = "Browse"
$Browse.UseVisualStyleBackColor = $True
$Browse.add_Click($Browse_OnClick)
$System_Drawing_Point.X = 230
$System_Drawing_Point.Y = 156
$label2.Location = $System_Drawing_Point
$label2.Name = "label2"
$System_Drawing_Size = New-Object System.Drawing.Size
$System_Drawing_Size.Height = 23
$System_Drawing_Size.Width = 100
$label2.Size = $System_Drawing_Size
$label2.TabIndex = 6
$label2.Text = "Enter Hostname"
$label2.TextAlign = 16
$label2.add_Click($handler_label2_Click)
$System_Drawing_Point = New-Object System.Drawing.Point
$System_Drawing_Point.X = 13
$System_Drawing_Point.Y = 159
$txtComputername.Location = $System_Drawing_Point
$txtComputername.Name = "txtComputername"
$System_Drawing_Size = New-Object System.Drawing.Size
$System_Drawing_Size.Height = 20
$System_Drawing_Size.Width = 188
$txtComputername.Size = $System_Drawing_Size
$txtComputername.TabIndex = 5
$txtComputername.add_Leave($handler_txtComputername_TextChanged)
$radioButton3.DataBindings.DefaultDataSourceUpdateMode = 0
$System_Drawing_Point.X = 271
$System_Drawing_Point.Y = 97
$radioButton3.Location = $System_Drawing_Point
$radioButton3.Name = "radioButton3"
$System_Drawing_Size = New-Object System.Drawing.Size
$System_Drawing_Size.Height = 41
$System_Drawing_Size.Width = 104
$radioButton3.Size = $System_Drawing_Size
$radioButton3.TabIndex = 4
$radioButton3.TabStop = $True
$radioButton3.Text = "Load List of Computers"
$radioButton3.UseVisualStyleBackColor = $True
$radioButton3.add_CheckedChanged($handler_radioButton3_CheckedChanged)
$radioButton2.DataBindings.DefaultDataSourceUpdateMode = 0
$System_Drawing_Point.X = 167
$System_Drawing_Point.Y = 98
$radioButton2.Location = $System_Drawing_Point
$radioButton2.Name = "radioButton2"
$System_Drawing_Size = New-Object System.Drawing.Size
$System_Drawing_Size.Height = 40
$System_Drawing_Size.Width = 75
$radioButton2.Size = $System_Drawing_Size
$radioButton2.TabIndex = 3
$radioButton2.TabStop = $True
$radioButton2.Text = "Enter Hostname"
$radioButton2.UseVisualStyleBackColor = $True
$radioButton2.add_CheckedChanged($handler_radioButton2_CheckedChanged)
$radioButton1.DataBindings.DefaultDataSourceUpdateMode = 0
$System_Drawing_Point.X = 13
$System_Drawing_Point.Y = 106
$radioButton1.Location = $System_Drawing_Point
$radioButton1.Name = "radioButton1"
$System_Drawing_Size = New-Object System.Drawing.Size
$System_Drawing_Size.Height = 24
$System_Drawing_Size.Width = 104
$radioButton1.Size = $System_Drawing_Size
$radioButton1.TabIndex = 2
$radioButton1.TabStop = $True
$radioButton1.Text = "LocalHost"
$radioButton1.UseVisualStyleBackColor = $True
$radioButton1.add_CheckedChanged($handler_radioButton1_CheckedChanged)
$System_Drawing_Point = New-Object System.Drawing.Point
$System_Drawing_Point.X = 13
$System_Drawing_Point.Y = 51
$txtDescription.Location = $System_Drawing_Point
$txtDescription.Name = "txtDescription"
$System_Drawing_Size = New-Object System.Drawing.Size
$System_Drawing_Size.Height = 20
$System_Drawing_Size.Width = 473
$txtDescription.Size = $System_Drawing_Size
$txtDescription.TabIndex = 1
$System_Drawing_Point.X = 13
$System_Drawing_Point.Y = 24
$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 = 470
$label1.Size = $System_Drawing_Size
$label1.TabIndex = 0
$label1.Text = "Enter Description"
$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
function Select-FileDialog {
$ofd = New-Object System.Windows.Forms.OpenFileDialog
$ofd.InitialDirectory = "c:\Description"
$ofd.ShowHelp=$true
if($ofd.ShowDialog() -eq "OK") { $ofd.FileName }
GenerateForm