Following with powershell forms series, I have written a simple form where you can type an IP address or a hostname and it queries DNS to get the hostname or IP address respectively.
This time I have embedded a png image into the code to show a fancy icon in the form.
I have converted the image into a string with the help of the website http://www.base64-image.de/step-1.php
Functions to query DNS are native from .NET
Here is the powershell script:
This time I have embedded a png image into the code to show a fancy icon in the form.
I have converted the image into a string with the help of the website http://www.base64-image.de/step-1.php
Functions to query DNS are native from .NET
[System.Net.Dns]::gethostentry($inputTextBox.text) [System.Net.DNS]::GetHostByName($inputTextBox.text)
Here is the powershell script:
#region Import the Assemblies [reflection.assembly]::loadwithpartialname("System.Windows.Forms") | Out-Null $MyForm = New-Object System.Windows.Forms.Form $base64ImageString ="iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAYAAAAf8/9hAAAABGdBTUEAAK/INwWK6QAAABl0RVh0U29mdHdhcmUAQWRvYmUgSW1hZ2VSZWFkeXHJZTwAAAMtSURBVDjLVZNLa1xlAIafc5szk8xkMkkm5MKY2EpT2qa2MTVCmoLS2gq6EKooimAW7iQb/0I2bgTRIog0oFW7KQpCS7VqrSmmJGlSQtswqWlLLmbGmcmcZM6cy/edz00r6bt8eXh4N6+mlGJnxiZHR4APgSNAFjCBKjClInXm05Gzl3by2mPB2OSoCUwAp1/LHbcziSyO24gbgJAegg2urF8UUsifhZBvfvXK99v/C8YmRy3gt8G2/cMv517E8Wx8ApYcjZiyKbkRSgQkcFn3rzG9Nn1LhOLYt2/8UNUfLZkYaN0zfLRrkLIMCHUNIXTqIoZLjLJvU/ASrFQtnko+z2BH38HAD78DMConHh4FPn5nz6vGgqyxTp16JNj2kpR9C8eD/OoW1VoNO1NCS+d5oW0vV27f2PX11MS8MTR6+JOTXUMHNCPBui5AtdMpk8xsGNQ9ndur20TxCnbPIn5TnmJUwaxIDrTm9Jn7d1tM4EiuqZs5d41iXGefsZsIwYNCgOfVSXconJbLLEWb4CuahU2+6HO8d4DQF/0m0NpgNvLAXaPgu6QadrEZpKhUItJZj/aMS1EewvHnsdUWW/+WKG82kEykCAPRbCqlNE1B4DsocpiW5OJfIVoiyfqSQFdNdGXrpLZGcFZDPKYJg2VQCiGEZkoRlZ3A6W41mknFn2WlaOKFFrG4Tbw9wb2/S3g3miHySLdbNDd2kzYKVGpVpIiqugjF7P3yQ55pyLFWmCSyVokZPqHnEoYmsWQGuyWOGdexNIkRFOnqbGN5bRngjh4G4rMLd6+KnmQW012lWrpOJuNjCh9LU9i6gRkEZHIrpNv/QK8vcijXz5lfLijgS+PmuYV75+fPDXr1Wt9znfsouy5x+2miuoltW1iawBJV0o0/wT8lBvbv5WZ+gaWNlasz43MfmQChH777e37uT78eHDx5+BiLBROjqhDaFmGkQ1KS6+mlr7+XX2evc+nWVB54+4kznfr8pZQIxXkRyhPvDb9vIjtQqgFN12hLO2yUZ/ni8o8SuAa8NTM+t/GE4HGGx4del0J+IGXUH8ko86iuAneAszPjc9/s5P8DuO6ZcsXuRqAAAAAASUVORK5CYII=" $iconimageBytes = [Convert]::FromBase64String($base64ImageString) $ims = New-Object IO.MemoryStream($iconimageBytes, 0, $iconimageBytes.Length) $ims.Write($iconimageBytes, 0, $iconimageBytes.Length); $MyForm.Icon = [System.Drawing.Icon]::FromHandle((new-object System.Drawing.Bitmap -argument $ims).GetHIcon()) $MyForm.Text = "DNS Query" $MyForm.Size = New-Object System.Drawing.Size(240,85) $MyForm.StartPosition = [System.Windows.Forms.FormStartPosition]::Manual ###start pos### $screen = [System.Windows.Forms.Screen]::PrimaryScreen $top = $screen.WorkingArea.height - $MyForm.height $left = $screen.WorkingArea.width - $MyForm.width $MyForm.Location = new-object System.Drawing.Point($left,$top) ###end pos### $MyForm.BackColor = [System.Drawing.Color]::SeaShell $Myform.FormBorderStyle = [System.Windows.Forms.FormBorderStyle]::FixedSingle $Myform.ShowInTaskbar = $false $Myform.maximizebox = $false $Myform.minimizebox = $false $Myform.topMost=$true #input $inputLabel = New-Object System.Windows.Forms.Label $inputLabel.Location = New-Object System.Drawing.Point(5,5) $inputLabel.Size = New-Object System.Drawing.Size(50,20) $inputLabel.Text = "ip/name:" $MyForm.Controls.Add($inputLabel) $inputTextBox = New-Object System.Windows.Forms.TextBox $inputTextBox.Location = New-Object System.Drawing.Point(60,5) $inputTextBox.Size = New-Object System.Drawing.Size(160,20) $MyForm.Controls.Add($inputTextBox) $inputTextBox.Add_KeyDown({ if ($_.KeyCode -eq "Enter") { $resultTextBox.text="searching..." [System.Windows.Forms.Application]::DoEvents() $isValidIP = [System.Net.IPAddress]::tryparse([string]$inputTextBox.text, [ref]$null) if ($isValidIP) { try {$resultdns = [System.Net.Dns]::gethostentry($inputTextBox.text) } catch { } if ($resultdns){$resultTextBox.text=[string]$resultdns.HostName} else{$resultTextBox.text="IP not found in DNS"} } else { try {$resultdns =[System.Net.DNS]::GetHostByName($inputTextBox.text) } catch { } if ($resultdns){$resultTextBox.text=[string]$resultdns.addresslist[0].IPaddresstostring} else{$resultTextBox.text="Name not found in DNS"} } } }) #result $resultLabel = New-Object System.Windows.Forms.Label $resultLabel.Location = New-Object System.Drawing.Point(5,25) $resultLabel.Size = New-Object System.Drawing.Size(50,20) $resultLabel.Text = "result:" $MyForm.Controls.Add($resultLabel) $resultTextBox = New-Object System.Windows.Forms.TextBox $resultTextBox.Location = New-Object System.Drawing.Point(60,25) $resultTextBox.Size = New-Object System.Drawing.Size(160,20) $MyForm.Controls.Add($resultTextBox) # Activates/draws the form. $myForm.Add_Shown({$myForm.Activate()}) [void] $MyForm.ShowDialog()
Comments
Post a Comment