Mathieu Consol Professional Blog

System and deployment are my playgrounds. I work especially with MS technology...

Powershell: let’s try an IPConfig in a frame

Day after day, I’m bored about running CMD, ipconfig, scroll to the top because I’ve got lots of Tunnuling connection area…

So, I decided to create my own powershell script. And because just an ipconfig in a powershell console is too easy, a frame is more sexy.

The code below:

#################################################################################################
# importing files
[void] [System.Reflection.Assembly]::LoadWithPartialName("System.Drawing")
[void] [System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")
##################################################################################################
##################################################################################################
# Forms declaration
$objForm = New-Object System.Windows.Forms.Form
$objForm.Text = "IPConfig with PowerShell"
$objForm.clientsize = new-object drawing.size @(350,250)
$objForm.maximumsize =new-object drawing.size @(350,250)
$objForm.minimumsize =new-object drawing.size @(350,250)
$objForm.maximizebox = $false
$objForm.helpbutton = $true
$objForm.StartPosition = "CenterScreen"

$objForm.KeyPreview = $True
$objForm.Add_KeyDown({if ($_.KeyCode -eq "Enter")
    {$x=$objTextBox.Text;$objForm.Close()}})
$objForm.Add_KeyDown({if ($_.KeyCode -eq "Escape")
    {$objForm.Close()}})

##################################################################################################
##################################################################################################
#label Adaptator name
function Add_Label([string] $labelName,[string] $labelValue,[int] $posX,[int] $posY){
    $objLabel = New-Object System.Windows.Forms.Label
    $objLabel.Location = New-Object System.Drawing.Size($posX,$posY)
    $objLabel.Size = New-Object System.Drawing.Size(390,20)
    $objLabel.forecolor = "blue"
    $objLabel.Text = "{0,-20} {1,-0}" -f $labelName , $labelValue
    $objForm.Controls.Add($objLabel)
}
##################################################################################################
##################################################################################################
# PowerShell cmdlet to interrogate the Network Adapter
$strComputer = "."
$colItems = get-wmiobject -class "Win32_NetworkAdapterConfiguration" `
-computername $strComputer | Where{$_.IpEnabled -Match "True"}
foreach ($objItem in $colItems) {
    Add_Label "Name:"$objItem.Description 10 10
    Add_Label "Enabled:" $objItem.IPEnabled 10 30
    Add_Label "Mac address:" $objItem.MACAddress 10 60
    Add_Label "IP Address:" $objItem.IPAddress 10 80
    Add_Label "Subnet:" $objItem.IPSubnet 10 100
    Add_Label "Gateway:" $objItem.DefaultIPGateway 10 120
    Add_Label "DNS:" $objItem.DNSServerSearchOrder 10 140
}

$objForm.controls.add($btnclose)

##################################################################################################
# button close
$btnclose = new-object System.Windows.Forms.Button
$btnclose.Size = "80,30"
$btnclose.text = "&Ok"
$btnclose.backcolor = "buttonface"
$btnclose.location = new-object Drawing.point(10,160)
$btnclose.add_click({
    $objForm.close()
})
##################################################################################################
#afficher l'interface
$objForm.Add_Shown({$objForm.Activate()})  
[void]$objForm.showdialog()

I’m very interested if you could comment me some trips/tips about this code.

Posted: Jan 28 2010, 01:11 PM by mathieucl | with no comments
Filed under:

Comments

No Comments