Example Shell
Powershell for Developers
Presentation to the Dot.Net Usergroup Bremen
9 July 2008
Peter Hahndorf
Solution Architect
Hahndorf Consulting
http://peter.hahndorf.eu

Agenda

  • Requirements
  • The Basics
  • Functions, Scripts and Profiles
  • COM, WMI and Dot.Net
  • Examples
  • Building Cmdlets
  • Extensions
  • Preview of Version 2.0
  • Resources

Requirements

  • Requires .Net 2.0 but doesn't run on Windows 2000
  • 32 and 64 bit Versions, not all extensions run under 64 Bit
  • Different downloads for XP, Windows 2003 and Vista.
    Included in Server 2008

The Basics

Keyboard

TAB, F7

Commands

Command -parameter1 -parameter2 argument1 argument2 Arguments are parameters to real parameters

Always verb-noun combinations, so no dir or copy but get-childitems and copy-item.

Aliases

dir, ls are aliases for get-childitems. get-alias | sort name

Help

get-help Get-History [-detailed] [-full]
get-command
Get-Process | get-member

Providers and drives

Filesystem, Registry are builtin, as extensions: Exchange mailboxes, IIS sites etc. get-psdrive
set-location HKLM:
sl hkcu: ls, cd into stuff
sl Function:
sl C:

You can add your own drives as subset of existing ones, or even write your own providers for your application.

Piping

Passing output as input to other commands. Everything is an object get-process
get-process | where {$_.ProcessName -eq 'Powershell'}
get-process | where {$_.ProcessName -eq 'Powershell'} | select ProcessName, CPU
get-childitem | where {$_.Name -like '*.exe'}
get-childitem | where {$_.Name -like 'zip.exe'}
get-childitem | where {$_.Name -like 'zip.exe'} | select *
get-childitem | where {$_.Name -like 'zip.exe'} | get-member
get-childitem | where {$_.Name -like 'zip.exe'} | get-member | where {$_.Name -eq 'FullName' } | select * | fl

Variables

$variableName, $_
$myName = "Peter"
Write-Host $myName
Write-Host $myName.Replace("P","X")


Functions, Scripts and Profiles

Functions

function demo {Write-Host "Hello $Args"}
demo "World"

Scripts

Set-ExecutionPolicy RemoteSigned
Textfiles with *.ps1 extension

Profiles

Start when Powershell starts:
Per user: %UserProfile%\(My )Documents\WindowsPowerShell\Microsoft.PowerShell_profile.ps1
Per machine: %windir%\system32\WindowsPowerShell\v1.0\ Microsoft.PowerShell_profile.ps1

COM, WMI and Dot.Net

COM

Access all the COM goodness on your machine; Windows, Office, your own stuff $ie = new-object -com "InternetExplorer.Application"
$ie.visible = $true
$ie.Navigate2("http://ix.de")

WMI

Some things are only available through Windows Management Instrumentation Get-WmiObject -class Win32_LogicalDisk | sort -desc Size | select -first 5 DeviceID, VolumeName, Size | format-table -autosize

Dot.Net

All the things we like.
Access static methods with ::
$web = [System.Reflection.Assembly]::LoadWithPartialName("System.Web")
$web.GetExportedTypes()
$web.GetExportedTypes() | get-member
$web.GetExportedTypes() | WHERE {$_.Name -like '*Mail*'}
$web.GetExportedTypes() | WHERE {$_.Name -like 'MailMessage'} | get-member
[System.Math]::POW(2,3)
Create an instance of a class
$wc = New-object net.webclient
$page = $wc.DownloadString("http://www.ix.de")
$page.length
$page
$xml = [xml]$wc.DownloadString("http://www.twee.net/news/rss.xml")
$xml.rss.channel.item | select title

Examples

  • Remove all Subversion files from a folder
  • cd into location
    get-childitem . -recurse -filter *.svn -force
    ls . -recurse -filter *.svn -force | remove-item -recurse -whatif
  • Set file dates based on original file dates
  • Use \Data\Presentations\Powershell\TouchDemo\TouchDemo.ps1
  • Prepare asp.net deployment
  • Creates Deployment Zip File to be copied to server
    - Compiles Web-Sites into multiple assemblies
    - Removes Theme Attributes from Page Directives
    - Merges all web assemblies into a single one
    - Compares new version with last version and
    finds all the different files
    - Zips the new files into a special zip file.
    Build an Assembly
    wds
    .\bPersonal.ps1
    Change aspx.cs
    \Hosts\Topas\TweeNet.Topas.UI.Web\search.aspx.cs
    Line 46
    Update SolutionInfo.cs
    \scripts\packagetopas.ps1


    Show 'Include Files' in packagetopas.ps1
    Show 'Compare' in common.ps1 Line: 65

Building CmdLets

  • Download and install VS Templates
  • Write 'Hello World' Cmdlet
  • Create a new Powershell Project
    Add item PSCmdlet
    Implement ProcessRecord
    WriteWarning("Hello Bremen .Net User Group");
    Set name 'GroupGreeting' in attribute
    Set SnapIn Name 'BremenUG' in PSSnapin.cs
    Compile Project
    Copy dll path
    (as admin) cd $tnDiexit rDotNet2
    .\InstallUtil.exe "full assemblyName"
    get-pssnapin -registered
    add-pssnapin BremenUG
    GroupGreeting

Extensions

PowerShell Community Extensions

Many extensions, I use it for email and XML handling.

PowerTab

Tab expansion and 'intellisense' on the command line.

PowerShell Commands for Active Directory

Free Active Directory Management extensions

Exchange 2007

Builtin to the products, does anything the GUI does and more.

PowerShell Provider for IIS 7.0

Full management of IIS plus real time request monitoring.

SQL Server 2008

Builtin, special version

Other Microsoft Products

All future Microsoft server products will have builtin Powershell support.


Preview of Version 2.0

  • Graphical Shell, editor with integrated shell
  • Transactions
  • Many new commandlets
  • Remote script execution, but only on Windows 6 with WinRM

Resources

Windows Powershell in Action

Best Powershell book I've read, by Bruce Payette on Manning

PowerShell Anwendergruppe

Microsoft Powershell Home

Powershell @ Microsoft Script Center

My blog