I am trying to create a form for emailing using powershell and I keep getting the ‘cannot call a method on a null value’ error when I try to add text boxes to the form.
The text boxes are to be filled in by the user so I am confused a little. Does powershell expect some value to be put into these text boxes before it will create them?
the code i am using is as follows
Add-Type -AssemblyName System.Windows.Forms
Add-Type -AssemblyName System.Drawing
$Form = New-Object System.Windows.Forms.Form
$Form.Size = New-Object System.Drawing.Size(300,300)
$From = New-Object System.Windows.Forms.TextBox
$From.Location = New-Object System.Drawing.Point(30,30)
$To = New-Object System.Windows.Forms.TextBox
$To.Location = New-Object System.Drawing.Point(30,60)
$Subject = New-Object System.Windows.Forms.TextBox
$Subject.Location = New-Object System.Drawing.Point(30,90)
$Body = New-Object System.Windows.Forms.RichTextBox
$Body.Location = New-Object System.Drawing.Point (30,120)
$SmtpServer ="Some SMTP server"
$Send = New-Object System.Windows.Forms.Button
$Send.Location = New-Object System.Drawing.Point(160,120)
$Send.Text = "Send"
$Form.Controls.Add($From)
$Form.Controls.Add($To)
$Form.Controls.Add($Subject)
$Form.Controls.Add($Body)
$Form.Controls.Add($Send)
$Send.Add_Click({Send-MailMessage -From $From.Text -To $To.Text -Subject $Subject.Text -Body $Body.Text -SmtpServer $SmtpServer})
$Form.ShowDialog()
Ideally I want the form to contain user information for new AD accounts in order to deliver the form via email to our security desk for review and final customization. The script for creating the accounts is written and functional. But this is giving me headaches as I cannot understand why it will not accept the creation of the text box.