Powershell Forms: DNS Query

Siguiendo con la serie de powershell forms, he hecho un sencillo formulario donde puedes escribir una IP o un nombre de maquina y la consulta en DNS para dar la maquina o IP resultante respectivamente.


Esta vez he embebido una imagen png en el codigo para que se muestre como icono del formulario.
He convertido la imagen a un string desde la web http://www.base64-image.de/step-1.php
Las funciones para consultar en DNS son nativas de .NET
[System.Net.Dns]::gethostentry($inputTextBox.text)
[System.Net.DNS]::GetHostByName($inputTextBox.text)

Aqui esta el script powershell:
#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()

Comentarios

  1. Buenos días , con respecto la entrada del blog http://sistemaswin.blogspot.com.es/2013/07/active-directory-avisar-por-mail-los.html , me puedes indicar los cambios para que al enviar los email, los envíe a los usuarios de un grupo en concreto.

    Un saludo y muchas gracias de antemano.

    ResponderEliminar
  2. ese grupo entiendo que es una lista de distribución (y sino debiera serlo).
    imagino que quieres añadirlo en copia oculta... antes del $smtp.Send($MailMessage) pon:

    $MailMessage.Bcc.Add("listadistribucion@dominio.com")

    Si no fuera una lista de distribucion y quisieras enviarlo a cada miembro del grupo:

    $objgroup = New-Object System.DirectoryServices.DirectoryEntry "LDAP://CN=NombreGrupo,OU=Groups,DC=dominio,DC=int"
    $miembros=$objgroup.properties.member
    foreach ($miembro in $miembros)
    {
    $objusuario=New-Object System.DirectoryServices.DirectoryEntry "LDAP://$miembro"
    $mail=$objusuario.properties.mail
    $MailMessage.Bcc.Add($mail)
    }

    ResponderEliminar

Publicar un comentario