Windows PowerShell - Billings .NET User Group - August 2009

Post on 21-May-2015

1,875 views 4 download

Tags:

transcript

Windows PowerShellJohn Clayton

http://codemonkeylabs.com/

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?

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

Using Windows PowerShell

Prepare to be amazed

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

Get-Command

Get-Command

Get-Command

Get-Help

Get-PSDrive

Get-Member

Command Aliases

Native Applications

Object pipeline

Formatting

Navigation

Navigation

Navigation

Filtering, Looping, & Selecting

Scripting

Scripting

Scripting

Scripting

Extending Windows PowerShell

If built in it is not, make it you shall

Creating Cmdlets

In a class library reference System.Management.Automation

Hint: It’s not in the GAC!

Creating Cmdlets

In a class library reference System.Configuration.Install

Creating Cmdlets

Add a class and build your cmdlet

Creating Cmdlets

Add a class and build your snap-in

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

Creating Cmdlets

Launch a new instance of PowerShell

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

Register the snap-in:Add-PSSnapIn

Creating Cmdlets

Export the current console:Export-Console

Include the exported file in your project

Set it to always copy to the output directory

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

Creating Cmdlets

Hosting PowerShell

Create a new windows forms application

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

Hosting PowerShell

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

Hosting PowerShell

Using the runspace create and invoke a pipeline

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

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?

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

Resources