Windows PowerShell - Billings .NET User Group - August 2009

38
Windows PowerShell John Clayton http://codemonkeylabs.com/

Transcript of Windows PowerShell - Billings .NET User Group - August 2009

Page 1: Windows PowerShell - Billings .NET User Group - August 2009

Windows PowerShellJohn Clayton

http://codemonkeylabs.com/

Page 2: Windows PowerShell - Billings .NET User Group - August 2009

Next generation command line shell◦ Processes objects instead of plain text◦ Large set of built-in commands◦ Consistent command parser

Scripting language◦ Designed for managing .NET objects◦ Provides a consistent environment for using

cmdlets◦ Supports complex tasks without making simple

tasks more complex

What is it?

Page 3: Windows PowerShell - Billings .NET User Group - August 2009

Built on .NET◦ Underlying objects are .NET◦ Extensible via cmdlets, providers, & host

applications Targeted at IT professionals

◦ Supported by the latest server products◦ An astonishing amount can be accomplished

without any programming◦ TechNet Script Center has hundreds of resources

What is it? (cont.)

Page 4: Windows PowerShell - Billings .NET User Group - August 2009

Using Windows PowerShell

Prepare to be amazed

Page 5: Windows PowerShell - Billings .NET User Group - August 2009

All commands use a Verb-Noun syntax Self-describing, discoverable environment Tab expansion Command aliases Brings in native applications & utilities Object pipeline Formatting Navigation Filtering Scripting

The Shell

Page 6: Windows PowerShell - Billings .NET User Group - August 2009

Get-Command

Page 7: Windows PowerShell - Billings .NET User Group - August 2009

Get-Command

Page 8: Windows PowerShell - Billings .NET User Group - August 2009

Get-Command

Page 9: Windows PowerShell - Billings .NET User Group - August 2009

Get-Help

Page 10: Windows PowerShell - Billings .NET User Group - August 2009

Get-PSDrive

Page 11: Windows PowerShell - Billings .NET User Group - August 2009

Get-Member

Page 12: Windows PowerShell - Billings .NET User Group - August 2009

Command Aliases

Page 13: Windows PowerShell - Billings .NET User Group - August 2009

Native Applications

Page 14: Windows PowerShell - Billings .NET User Group - August 2009

Object pipeline

Page 15: Windows PowerShell - Billings .NET User Group - August 2009

Formatting

Page 16: Windows PowerShell - Billings .NET User Group - August 2009

Navigation

Page 17: Windows PowerShell - Billings .NET User Group - August 2009

Navigation

Page 18: Windows PowerShell - Billings .NET User Group - August 2009

Navigation

Page 19: Windows PowerShell - Billings .NET User Group - August 2009

Filtering, Looping, & Selecting

Page 20: Windows PowerShell - Billings .NET User Group - August 2009

Scripting

Page 21: Windows PowerShell - Billings .NET User Group - August 2009

Scripting

Page 22: Windows PowerShell - Billings .NET User Group - August 2009

Scripting

Page 23: Windows PowerShell - Billings .NET User Group - August 2009

Scripting

Page 24: Windows PowerShell - Billings .NET User Group - August 2009

Extending Windows PowerShell

If built in it is not, make it you shall

Page 25: Windows PowerShell - Billings .NET User Group - August 2009

Creating Cmdlets

In a class library reference System.Management.Automation

Hint: It’s not in the GAC!

Page 26: Windows PowerShell - Billings .NET User Group - August 2009

Creating Cmdlets

In a class library reference System.Configuration.Install

Page 27: Windows PowerShell - Billings .NET User Group - August 2009

Creating Cmdlets

Add a class and build your cmdlet

Page 28: Windows PowerShell - Billings .NET User Group - August 2009

Creating Cmdlets

Add a class and build your snap-in

Page 29: Windows PowerShell - Billings .NET User Group - August 2009

Creating Cmdlets

From an administrative Visual Studio command prompt navigate to your output directory and install your assembly

Gotcha: On a 64-bit system you’ll need to use the 64-bit version of InstallUtil.exe

Page 30: Windows PowerShell - Billings .NET User Group - August 2009

Creating Cmdlets

Launch a new instance of PowerShell

View the registered snap-ins:Get-PSSnapIn –Registered

Register the snap-in:Add-PSSnapIn

Page 31: Windows PowerShell - Billings .NET User Group - August 2009

Creating Cmdlets

Export the current console:Export-Console

Include the exported file in your project

Set it to always copy to the output directory

Page 32: Windows PowerShell - Billings .NET User Group - August 2009

Creating CmdletsIn the project properties set the debug action to start powershell.exe

In the command line arguments specify your console export file using the –PSConsoleFile flag

Page 33: Windows PowerShell - Billings .NET User Group - August 2009

Creating Cmdlets

Page 34: Windows PowerShell - Billings .NET User Group - August 2009

Hosting PowerShell

Create a new windows forms application

Add a reference to System.Management.Automation and your snap-in project

Page 35: Windows PowerShell - Billings .NET User Group - August 2009

Hosting PowerShell

In the form use a RunspaceConfiguration instance to add your snap-in, then create and open the runspace

Page 36: Windows PowerShell - Billings .NET User Group - August 2009

Hosting PowerShell

Using the runspace create and invoke a pipeline

Remember that Get-Proc is returning a collection and not individual items!

Page 37: Windows PowerShell - Billings .NET User Group - August 2009

PowerShell 1.0◦ Included in Windows Server 2008◦ Separate download for Windows XP SP2, Windows

Server 2003, & Windows Vista PowerShell 2.0

◦ Included in Windows 7 & Windows Server 2008 R2◦ RC available for Windows XP SP3, Windows Server

2003 SP2, Vista SP1, & Windows Server 2008

Where do I get it?

Page 38: Windows PowerShell - Billings .NET User Group - August 2009

http://blogs.msdn.com/PowerShell/ http://bit.ly/powershell-ebook http://bit.ly/scriptcenter http://bit.ly/poshtweet http://bit.ly/easy-powershell

Resources