posted 28/01/10

PowerShell + Login Script

By Fredrik Wall

Working with a login script for my lab active directory.
100% PowerShell using .NET classes and WMI.

This is VBScript login scripts with HTA on steroids :)

image 

image 

The GUI part was done in 10-15 min with Primal Forms.

7 Comments
read more
posted 19/12/09

Get-DotNetInfo function

By Fredrik Wall

I made this function back in 2007.
But I did only published It in Swedish then.
This function helps me a lot when I’m working
with PowerShell and Dot Net classes.

When I have the Dot Net Class that I want
to use but not much information. Then I just
call this function with the Dot Net Class
and IE opens with the information.

# Get-DotNetInfo Function
#
By Fredrik Wall, fredrik@poweradmin.se
#
http://www.poweradmin.se/blog
#
#
Created: 2007-09-13
#
Last modified: 2009-12-18
#
Tested on: Windows XP, Windows Vista,
#
Windows 7, Windows Server 2008 R2
#
Revision history:
#
2009-12-18 ADD: Function information
#
Example of calling the function:
#
Get-DotNetInfo System.Net.Webclient

Function Get-DotNetInfo {
 
param([string]$dotnet="")
 
$objIE = New-Object -Com Internetexplorer.Application
 
$url = "http://msdn2.microsoft.com/en-us/library/" + $dotnet
 
$objIE.Navigate($url)
 
$objIE.Visible=$true
}


2 Comments
read more
posted 26/07/09

My Internet IP information

By Fredrik Wall

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).

image

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.

image

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.

image 

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!

image

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
3 Comments
read more