This will be my first post in a long series of
posts, screencasts and other things related to
PowerShell and .net forms with one of my favorite
tools, Primal Forms (Community Version).
I love this tool because of that It’s so easy to take
a old PowerShell script or a function and make a forms
for It in 5 minutes.
Windows forms tools will draw a bigger audiences to It
then ordinary PowerShell scripts will.
In this first example I will only do a simple form with
a close button and a label that will show the Internet
IP Address.
And then we export the form to PowerShell and get this code:
#Generated Form Function function GenerateForm { ######################################################################## # Code Generated By: SAPIEN Technologies PrimalForms (Community Edition) v1.0.3.0 # Generated On: 2009-07-26 19:37 # Generated By: Fredrik ######################################################################## #region Import the Assemblies [reflection.assembly]::loadwithpartialname("System.Windows.Forms") | Out-Null [reflection.assembly]::loadwithpartialname("System.Drawing") | Out-Null #endregion #region Generated Form Objects $form1 = New-Object System.Windows.Forms.Form $groupBox1 = New-Object System.Windows.Forms.GroupBox $lblIPAddress = New-Object System.Windows.Forms.Label $btnClose = New-Object System.Windows.Forms.Button $InitialFormWindowState = New-Object System.Windows.Forms.FormWindowState #endregion Generated Form Objects #---------------------------------------------- #Generated Event Script Blocks #---------------------------------------------- #Provide Custom Code for events specified in PrimalForms. $btnClose_OnClick= { #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 $form1.Text = 'Primal Form' $form1.Name = 'form1' $form1.DataBindings.DefaultDataSourceUpdateMode = 0 $System_Drawing_Size = New-Object System.Drawing.Size $System_Drawing_Size.Width = 294 $System_Drawing_Size.Height = 266 $form1.ClientSize = $System_Drawing_Size $groupBox1.Name = 'groupBox1' $groupBox1.Text = 'Internet Information' $System_Drawing_Size = New-Object System.Drawing.Size $System_Drawing_Size.Width = 269 $System_Drawing_Size.Height = 198 $groupBox1.Size = $System_Drawing_Size $System_Drawing_Point = New-Object System.Drawing.Point $System_Drawing_Point.X = 13 $System_Drawing_Point.Y = 13 $groupBox1.Location = $System_Drawing_Point $groupBox1.TabStop = $False $groupBox1.TabIndex = 1 $groupBox1.DataBindings.DefaultDataSourceUpdateMode = 0 $form1.Controls.Add($groupBox1) $lblIPAddress.TabIndex = 0 $System_Drawing_Size = New-Object System.Drawing.Size $System_Drawing_Size.Width = 185 $System_Drawing_Size.Height = 23 $lblIPAddress.Size = $System_Drawing_Size $System_Drawing_Point = New-Object System.Drawing.Point $System_Drawing_Point.X = 43 $System_Drawing_Point.Y = 79 $lblIPAddress.Location = $System_Drawing_Point $lblIPAddress.DataBindings.DefaultDataSourceUpdateMode = 0 $lblIPAddress.Name = 'lblIPAddress' $groupBox1.Controls.Add($lblIPAddress) $btnClose.TabIndex = 0 $btnClose.Name = 'btnClose' $System_Drawing_Size = New-Object System.Drawing.Size $System_Drawing_Size.Width = 75 $System_Drawing_Size.Height = 23 $btnClose.Size = $System_Drawing_Size $btnClose.UseVisualStyleBackColor = $True $btnClose.Text = 'Close' $System_Drawing_Point = New-Object System.Drawing.Point $System_Drawing_Point.X = 207 $System_Drawing_Point.Y = 231 $btnClose.Location = $System_Drawing_Point $btnClose.DataBindings.DefaultDataSourceUpdateMode = 0 $btnClose.add_Click($btnClose_OnClick) $form1.Controls.Add($btnClose) #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
If we run this code we will get this dumb form.
No information and the close button will not work.
To incorporate my Get-MyInternetIP function
we just add the code for the Get-MyInternetIP code
above the form code.
Function Get-MyInternetIP {
if ($WebClient -eq $null) {$Global:WebClient=new-object System.Net.WebClient }
$url="http://ip-address.domaintools.com/myip.xml"
$myInternetIP=([xml]($WebClient.DownloadString($url))).dnstools.ip_address
$myInternetIP
}
And then add
$form1.close()
to the
$btnClose_OnClick=
section
And at the en add
$lblIPAddress.Text = Get-MyInternetIP
Thats It!
All the code:
Function Get-MyInternetIP { if ($WebClient -eq $null) {$Global:WebClient=new-object System.Net.WebClient } $url="http://ip-address.domaintools.com/myip.xml" $myInternetIP=([xml]($WebClient.DownloadString($url))).dnstools.ip_address $myInternetIP } #Generated Form Function function GenerateForm { ######################################################################## # Code Generated By: SAPIEN Technologies PrimalForms (Community Edition) v1.0.3.0 # Generated On: 2009-07-26 19:37 # Generated By: Fredrik ######################################################################## #region Import the Assemblies [reflection.assembly]::loadwithpartialname("System.Windows.Forms") | Out-Null [reflection.assembly]::loadwithpartialname("System.Drawing") | Out-Null #endregion #region Generated Form Objects $form1 = New-Object System.Windows.Forms.Form $groupBox1 = New-Object System.Windows.Forms.GroupBox $lblIPAddress = New-Object System.Windows.Forms.Label $btnClose = New-Object System.Windows.Forms.Button $InitialFormWindowState = New-Object System.Windows.Forms.FormWindowState #endregion Generated Form Objects #---------------------------------------------- #Generated Event Script Blocks #---------------------------------------------- #Provide Custom Code for events specified in PrimalForms. $btnClose_OnClick= { #TODO: Place custom script here $form1.close() } $OnLoadForm_StateCorrection= {#Correct the initial state of the form to prevent the .Net maximized form issue $form1.WindowState = $InitialFormWindowState } #---------------------------------------------- #region Generated Form Code $form1.Text = 'Primal Form' $form1.Name = 'form1' $form1.DataBindings.DefaultDataSourceUpdateMode = 0 $System_Drawing_Size = New-Object System.Drawing.Size $System_Drawing_Size.Width = 294 $System_Drawing_Size.Height = 266 $form1.ClientSize = $System_Drawing_Size $groupBox1.Name = 'groupBox1' $groupBox1.Text = 'Internet Information' $System_Drawing_Size = New-Object System.Drawing.Size $System_Drawing_Size.Width = 269 $System_Drawing_Size.Height = 198 $groupBox1.Size = $System_Drawing_Size $System_Drawing_Point = New-Object System.Drawing.Point $System_Drawing_Point.X = 13 $System_Drawing_Point.Y = 13 $groupBox1.Location = $System_Drawing_Point $groupBox1.TabStop = $False $groupBox1.TabIndex = 1 $groupBox1.DataBindings.DefaultDataSourceUpdateMode = 0 $form1.Controls.Add($groupBox1) $lblIPAddress.TabIndex = 0 $System_Drawing_Size = New-Object System.Drawing.Size $System_Drawing_Size.Width = 185 $System_Drawing_Size.Height = 23 $lblIPAddress.Size = $System_Drawing_Size $System_Drawing_Point = New-Object System.Drawing.Point $System_Drawing_Point.X = 43 $System_Drawing_Point.Y = 79 $lblIPAddress.Location = $System_Drawing_Point $lblIPAddress.DataBindings.DefaultDataSourceUpdateMode = 0 $lblIPAddress.Name = 'lblIPAddress' $lblIPAddress.Text = Get-MyInternetIP $groupBox1.Controls.Add($lblIPAddress) $btnClose.TabIndex = 0 $btnClose.Name = 'btnClose' $System_Drawing_Size = New-Object System.Drawing.Size $System_Drawing_Size.Width = 75 $System_Drawing_Size.Height = 23 $btnClose.Size = $System_Drawing_Size $btnClose.UseVisualStyleBackColor = $True $btnClose.Text = 'Close' $System_Drawing_Point = New-Object System.Drawing.Point $System_Drawing_Point.X = 207 $System_Drawing_Point.Y = 231 $btnClose.Location = $System_Drawing_Point $btnClose.DataBindings.DefaultDataSourceUpdateMode = 0 $btnClose.add_Click($btnClose_OnClick) $form1.Controls.Add($btnClose) #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
No related posts.
Nice. Thanx.
I’ve done a similar demo recently, although using dyndns.com. I prefer your technique since domaintools gives such a nice collection of data.
There’s one problem that can occur with using their XML. They assume that all strings contain xml-safe characters, and that’s not always the case; for example, if you’re using an AT&T Internet Services address, the isp element contains a literal “&” that causes the XML cast to puke.
It’s still relatively easy to parse – or, assuming there are no other problematic characters, a simple substitution will fix the problem:
(($WebClient.DownloadString($url) -replace '&','&')).dnstools.ipaddressObviously not very pretty, but I haven't seen this blind substitution cause a problem yet.