Introduction to PowerShell

advertisement

10/29/07

© 2007 Asynchrony Solutions, Inc.

Introduction to PowerShell

Brian Button

VP Engineering

Asynchrony Solutions, Inc brian.button@asolutions.com

http://oneagilecoder.asolutions.com

1

Visual Studio 2005

VS.Net SP1

IIS

TFS Explorer

SQL Server 2005

GAX

GAT

Just the other day…

Enterprise Library

Nunit

Xunit.net

Resharper

Test Driven.net

© 2007 Asynchrony Solutions, Inc.

10/29/07

2

Failure!!!

• SQL Server failed to install and uninstall

• What to do???

• MSDN says:

• Regedit HKLM:\Software\Microsoft\Windows\CurrentVersion\Uninstall

• For each GUID found, Examine DisplayName searching for match of ‘SQL

Server’

• Manually uninstall it

• Maybe there was a better way!

10/29/07

© 2007 Asynchrony Solutions, Inc.

3

What is PowerShell

• New Scripting language

• Same, but different!

• In Vista, then unbundled

• Separate download

• General purpose programming language

© 2007 Asynchrony Solutions, Inc.

10/29/07

4

• Shell prompt

• Cmdlets

• Pipes

• .Net objects

• PSDrives

• Help system

Basic Tour

© 2007 Asynchrony Solutions, Inc.

10/29/07

5

• Get-Help

• Get-Command

• Get-Member

• Get-Psdrive

• Format-List

5 key commands

© 2007 Asynchrony Solutions, Inc.

10/29/07

6

Let’s build something

• Removing all bin and obj folders from project

• Find all IDisposable classes in assembly

10/29/07

© 2007 Asynchrony Solutions, Inc.

7

Powershell as a programming language

• General purpose programming language

• File extension PS1

• Example: Reformat.PS1

10/29/07

© 2007 Asynchrony Solutions, Inc.

8

Extending Powershell

• Custom Cmdlets and PSSnapins

• Written in C# or VB.Net

• Functions, Filters, Aliases

• Written in powershell

• Adding methods, properties to existing types

• Written in XML, powershell

• Add-member

© 2007 Asynchrony Solutions, Inc.

10/29/07

9

Extending Powershell in C#/VB.Net

• Create Custom cmdlet

[Cmdlet(VerbsCommon.Get, "Proc")]

}

{ public class GetProcCommand : Cmdlet

• Create PSSnapin to install

• Great tutorial at http://blogs.msdn.com/daiken/archive/2007/02/07/creating-a-windowspowershell-cmdlet-using-the-visual-studio-windows-powershelltemplates.aspx

10/29/07

© 2007 Asynchrony Solutions, Inc.

10

10/29/07

Extending Powershell natively

• Functions function to_uppercase

{ param([string]$first)

$first.ToUpper()

}

• At command line or in user profile

© 2007 Asynchrony Solutions, Inc.

11

Extending Powershell natively

• Aliases – make commands easier to use

• set-alias, new-alias, get-alias

Alias gal Get-Alias

Alias gc Get-Content

Alias gci Get-ChildItem

Alias gcm Get-Command

Alias gdr Get-PSDrive

10/29/07

© 2007 Asynchrony Solutions, Inc.

12

10/29/07

Customizing Powershell

• User profile

• $profile

• Get-Executionpolicy

• Like .profile or .login in Linux

• Invoked for every new shell

• Sets up functions, aliases, etc

© 2007 Asynchrony Solutions, Inc.

13

Why learn Powershell

• Automate repetitive tasks

• Share knowledge with others

• It’s the Microsoft Way…

10/29/07

© 2007 Asynchrony Solutions, Inc.

14

10/29/07

Controlling Windows through Scripts

• Complaints for years about GUI-only config

• In Unix/Linux forever

• MS making admin tasks powershell-scriptable

• Exchange

• SQL Server

• IIS 7

• More

© 2007 Asynchrony Solutions, Inc.

15

10/29/07

Resources

• Download powershell through Microsoft.com

• Videos

• http://channel9.msdn.com/Media/?TagID=163

• Blogs

• http://blogs.msdn.com/powershell

• http://thepowershellguy.com

• http://keithhill.spaces.live.com

• http://www.leeholmes.com/blog

© 2007 Asynchrony Solutions, Inc.

16

10/29/07

More resources

• Books

• Powershell in Action, Bruce Payette

• Professional Windows Powershell, Andrew Watt

• Windows Powershell Cookbook, Lee Holmes

• Community projects

• 21 projects on http://www.codeplex.com

© 2007 Asynchrony Solutions, Inc.

17

Q & A

• Slides posted at http://oneagilecoder.asolutions.com

• Email questions to brian.button@asolutions.com

10/29/07

© 2007 Asynchrony Solutions, Inc.

18

Download