26
MGT305 Windows PowerShell Next Generation Command Line Scripting Jeffrey Snover & Leonard Chung Windows Enterprise Management Division

MGT305 Windows PowerShell Next Generation Command Line Scripting Jeffrey Snover & Leonard Chung Windows Enterprise Management Division

  • View
    239

  • Download
    2

Embed Size (px)

Citation preview

Page 1: MGT305 Windows PowerShell Next Generation Command Line Scripting Jeffrey Snover & Leonard Chung Windows Enterprise Management Division

MGT305Windows PowerShell Next Generation Command Line Scripting

Jeffrey Snover & Leonard Chung

Windows Enterprise Management Division

Page 2: MGT305 Windows PowerShell Next Generation Command Line Scripting Jeffrey Snover & Leonard Chung Windows Enterprise Management Division

Keynote Reprise:Windows PowerShell has Released!• Download LinkDownload Link

• www.microsoft.com/powershell

• Supported platformsSupported platforms

• Windows XP & Windows Server 2003Windows XP & Windows Server 2003

• Windows Vista version out shortlyWindows Vista version out shortly

• Support for x86, x64, and ia64Support for x86, x64, and ia64

• Lots of Partners!Lots of Partners!

Page 3: MGT305 Windows PowerShell Next Generation Command Line Scripting Jeffrey Snover & Leonard Chung Windows Enterprise Management Division

Microsoft Products Providing Microsoft Products Providing Windows PowerShell CmdletsWindows PowerShell CmdletsMicrosoft Products Providing Microsoft Products Providing Windows PowerShell CmdletsWindows PowerShell Cmdlets

Windows Vista

Exchange Server 2007

Operations Manager 2007

Windows Server “Longhorn”

Virtual Machine Manager

Data Protection Manager “V2”

Page 4: MGT305 Windows PowerShell Next Generation Command Line Scripting Jeffrey Snover & Leonard Chung Windows Enterprise Management Division

Microsoft MVPs

PowerShell Team Blog

Active Newsgroup

Channel 9: DFO Show

IIS.net

Visit www.microsoft.com/powershell for more info

TechNet ScriptCenterExchange Server 2007

Terminal Server

WMI, Registry, Hardware, etc.

Community Submitted Scripts

Windows PowerShell EcosystemWindows PowerShell EcosystemWindows PowerShell EcosystemWindows PowerShell Ecosystem

Partners

100s of Scripts

MyITForum.com

Manning Publications

O’Reilly Publications

Sapien Press

and Other Publishers

Community Support

Books and Training Materials

Page 5: MGT305 Windows PowerShell Next Generation Command Line Scripting Jeffrey Snover & Leonard Chung Windows Enterprise Management Division

AgendaAgenda

• IntroductionIntroduction

• What is Windows PowerShell?What is Windows PowerShell?

• ShellShell

• Why is Windows PowerShell a great interactive shell?Why is Windows PowerShell a great interactive shell?

• ScriptingScripting

• How can I script with Windows PowerShell?How can I script with Windows PowerShell?

Page 6: MGT305 Windows PowerShell Next Generation Command Line Scripting Jeffrey Snover & Leonard Chung Windows Enterprise Management Division

Windows PowerShellWindows PowerShell

• Next generation command line shell that is:Next generation command line shell that is:

• As As interactiveinteractive and and composablecomposable as BASH/KSH as BASH/KSH

• As As programmaticprogrammatic as Perl/Ruby as Perl/Ruby

• As As production orientedproduction oriented as AS400 CL/VMS DCL as AS400 CL/VMS DCL

• Allows access to data stores as Allows access to data stores as easyeasy to access as filesystems to access as filesystems

Page 7: MGT305 Windows PowerShell Next Generation Command Line Scripting Jeffrey Snover & Leonard Chung Windows Enterprise Management Division

Admin Tools RoadmapAdmin Tools Roadmap

ScenarioScenario TodayToday TomorrowTomorrow PowerShell BenefitsPowerShell BenefitsGUI ShellGUI Shell MMCMMC MMC with snap-MMC with snap-

ins layered onins layered on PowerShellPowerShell

GUI-Cmd parityGUI-Cmd parity

Auto script creation (V2)Auto script creation (V2)

Command Command ShellShell

CMDCMD CMDCMD

PowerShellPowerShell CMD and UNIX shell parityCMD and UNIX shell parity

Command consistencyCommand consistency

Namespaces as Drives Namespaces as Drives (e.g. FS, Reg, WMI, AD, (e.g. FS, Reg, WMI, AD, Certs, etc) Certs, etc)

Command Command ScriptingScripting

CMD scriptingCMD scripting CMD scriptingCMD scripting

PowerShellPowerShell Richer languageRicher language

Better text utilitiesBetter text utilities

COM COM ScriptingScripting

WSH WSH VBScriptVBScript

JscriptJscript

WSHWSH VBScriptVBScript

JscriptJscript

Jscript (.net)Jscript (.net)PowerShellPowerShell

Richer languageRicher language

Object utilitiesObject utilities

Common access to COM, Common access to COM, XML, .NET, ADO, WMI, XML, .NET, ADO, WMI, ADSIADSI

Page 8: MGT305 Windows PowerShell Next Generation Command Line Scripting Jeffrey Snover & Leonard Chung Windows Enterprise Management Division

• Try things out in an interactive shellTry things out in an interactive shell

• Stitch things together with utilitiesStitch things together with utilities

• Put the results in a script file Put the results in a script file

• Realize that the tools are unsuitable and restart with a new set of Realize that the tools are unsuitable and restart with a new set of toolstools

• Generalize (e.g., parameterize)Generalize (e.g., parameterize)

• Clean it up Clean it up production-quality production-quality

• Integrate into your environmentIntegrate into your environment

• Share with the communityShare with the community

Admin Development ModelAdmin Development Model

Page 9: MGT305 Windows PowerShell Next Generation Command Line Scripting Jeffrey Snover & Leonard Chung Windows Enterprise Management Division

The 4 Pillars of DiscoverabilityThe 4 Pillars of Discoverability

1.1. Get-CommandGet-Command

2.2. Get-Help and -?Get-Help and -?

3.3. Get-PSDriveGet-PSDrive

4.4. Get-MembersGet-Members

Page 10: MGT305 Windows PowerShell Next Generation Command Line Scripting Jeffrey Snover & Leonard Chung Windows Enterprise Management Division

Once around the track

Leonard Chung

PowerShell Program Manager

Windows Enterprise Management Division

Page 11: MGT305 Windows PowerShell Next Generation Command Line Scripting Jeffrey Snover & Leonard Chung Windows Enterprise Management Division

Clear, Concise, Consistent, LearnableClear, Concise, Consistent, Learnable

Get-Process | Where { $_.handles –gt 500 } | Sort handles | Format-TableGet-Process | Where { $_.handles –gt 500 } | Sort handles | Format-Table

Get-P

rocess

Get-P

rocess

Cm

dlet

Cm

dlet

Common Windows PowerShell ParserCommon Windows PowerShell Parser

Windows PowerShell Pipeline ProcessorWindows PowerShell Pipeline ProcessorW

here C

md

letW

here C

md

let

So

rt S

ort

Cm

dlet

Cm

dlet

Fo

rmat

Fo

rmat

Cm

dlet

Cm

dlet

Page 12: MGT305 Windows PowerShell Next Generation Command Line Scripting Jeffrey Snover & Leonard Chung Windows Enterprise Management Division

Extending PowerShell with PowerGadgets

Rene Garcia

President

PowerGadgets

Page 13: MGT305 Windows PowerShell Next Generation Command Line Scripting Jeffrey Snover & Leonard Chung Windows Enterprise Management Division

How can I script with Windows How can I script with Windows PowerShell?PowerShell?• Script against various:Script against various:

• Instrumentation: Text, COM, .NET, CmdletsInstrumentation: Text, COM, .NET, Cmdlets

• Object types: WMI, XML, ADO, ADSIObject types: WMI, XML, ADO, ADSI

• Datastores: Filesystem, Registry, Certs, etcDatastores: Filesystem, Registry, Certs, etc

• Ad-hoc scripts need to be quick and easyAd-hoc scripts need to be quick and easy

• Arguments don’t need to be named or typedArguments don’t need to be named or typed

• Scripts don’t need to be signedScripts don’t need to be signed

• Production scripts need be to production qualityProduction scripts need be to production quality

• Arguments can be typed, named and have initializersArguments can be typed, named and have initializers

• Rich error handlingRich error handling

• Protection from uninitialized variablesProtection from uninitialized variables

• Data can be written to multiple streamsData can be written to multiple streams• Output, Error, Debug, Verbose, Warning, ProgressOutput, Error, Debug, Verbose, Warning, Progress

• Scripts can be digitally signedScripts can be digitally signed

Page 14: MGT305 Windows PowerShell Next Generation Command Line Scripting Jeffrey Snover & Leonard Chung Windows Enterprise Management Division

Simple ScriptingSimple Scripting

• Arguments not declared or typedArguments not declared or typed

function MyEcho function MyEcho {{ for ($i = 0; $i –lt for ($i = 0; $i –lt $args.count$args.count; $i++); $i++) {{ “ “arg{0} = {1}” –f $i, arg{0} = {1}” –f $i, $args[$i]$args[$i] }}}}

Page 15: MGT305 Windows PowerShell Next Generation Command Line Scripting Jeffrey Snover & Leonard Chung Windows Enterprise Management Division

Advanced ScriptingAdvanced Scriptingfunction Get-Total (function Get-Total (

[String]$property = $(throw "Property Required"),[String]$property = $(throw "Property Required"),

[string]$formatString = "Total {1} = {0}"[string]$formatString = "Total {1} = {0}"

) )

{ { BeginBegin

{ $total = 0{ $total = 0

}}

ProcessProcess

{ $total += $_.$property{ $total += $_.$property

}}

EndEnd

{ $formatString -f $total,$property{ $formatString -f $total,$property

}}

}}

Arguments declared, typed, initializedArguments declared, typed, initialized

Page 16: MGT305 Windows PowerShell Next Generation Command Line Scripting Jeffrey Snover & Leonard Chung Windows Enterprise Management Division

Sophisticated ScriptingSophisticated Scripting

function Get-Types (function Get-Types (

[ScriptBlock]$AssemblyFilter = {1},[ScriptBlock]$AssemblyFilter = {1},

[ScriptBlock]$TypeFilter = [ScriptBlock]$TypeFilter = {$_.IsPublic –eq “TRUE”}{$_.IsPublic –eq “TRUE”}

) )

{{

TRAP [System.Exception] {Write-Error “ERROR: $_.Exception”}TRAP [System.Exception] {Write-Error “ERROR: $_.Exception”}

foreach ($a in foreach ($a in [AppDomain]::CurrentDomain.GetAssemblies()[AppDomain]::CurrentDomain.GetAssemblies() | |

where $AssemblyFilter)where $AssemblyFilter)

{ {

$a.GetTypes() | $a.GetTypes() | where $TypeFilterwhere $TypeFilter

}}

}}

•Uses .NET APIs, traps, ScriptBlocksUses .NET APIs, traps, ScriptBlocks

Page 17: MGT305 Windows PowerShell Next Generation Command Line Scripting Jeffrey Snover & Leonard Chung Windows Enterprise Management Division

IDE Development ModelPrimalScript

Don Jones

Sapien Technologies Inc

www.Sapien.com

Page 18: MGT305 Windows PowerShell Next Generation Command Line Scripting Jeffrey Snover & Leonard Chung Windows Enterprise Management Division

MySpace

Dan Farino

Chief Systems Architect

MySpace.com

Page 19: MGT305 Windows PowerShell Next Generation Command Line Scripting Jeffrey Snover & Leonard Chung Windows Enterprise Management Division

More Windows PowerShell SessionsMore Windows PowerShell Sessions

• PowerShell Scripting in Exchange Server 2007 (UCMCT05)PowerShell Scripting in Exchange Server 2007 (UCMCT05)

• Solving Management Problems with Windows PowerShell (MGT314)Solving Management Problems with Windows PowerShell (MGT314)

• Exploring Windows PowerShell Utilities (MGTCT08)Exploring Windows PowerShell Utilities (MGTCT08)

• Windows PowerShell Lightning Tour (MGTLD03)Windows PowerShell Lightning Tour (MGTLD03)

Page 20: MGT305 Windows PowerShell Next Generation Command Line Scripting Jeffrey Snover & Leonard Chung Windows Enterprise Management Division

Ask The ExpertsGet Your Questions Answered

You can find us at the Microsoft Ask the Experts area, located in the Exhibition Hall:

Wednesday 15 November 10.15 – 10.45

Thursday 16 November 10.15 – 10.45

Thursday 16 November 14.45 – 15.45

Page 21: MGT305 Windows PowerShell Next Generation Command Line Scripting Jeffrey Snover & Leonard Chung Windows Enterprise Management Division
Page 22: MGT305 Windows PowerShell Next Generation Command Line Scripting Jeffrey Snover & Leonard Chung Windows Enterprise Management Division
Page 23: MGT305 Windows PowerShell Next Generation Command Line Scripting Jeffrey Snover & Leonard Chung Windows Enterprise Management Division

Useful ResourcesNewsgroup: Microsoft.Public.Windows.PowerShellNewsgroup: Microsoft.Public.Windows.PowerShell

Channel 9 tag: Channel 9 tag: http://channel9.msdn.com/tags/Monad

Team blog: Team blog: http://blogs.msdn.com/PowerShell/

Script Center: Script Center: http://www.microsoft.com/technet/scriptcenter/hubs/msh.mspx

Books:Books:

O’Reilly: O’Reilly: http://www.oreilly.com/catalog/msh/

Manning Press: http://manning.com/powershell/ Manning Press: http://manning.com/powershell/

Sapien Press: http://www.sapienpress.com/Sapien Press: http://www.sapienpress.com/

MshAnalyzer toolMshAnalyzer tool

http://www.karlprosser.com/coder/?cat=8http://www.karlprosser.com/coder/?cat=8

Page 24: MGT305 Windows PowerShell Next Generation Command Line Scripting Jeffrey Snover & Leonard Chung Windows Enterprise Management Division

Community ResourcesCommunity Resources

Newsgroup: Microsoft.Public.Windows.PowerShellNewsgroup: Microsoft.Public.Windows.PowerShell

Channel 9 tag: http://channel9.msdn.com/tags/MonadChannel 9 tag: http://channel9.msdn.com/tags/Monad

Team blog: Team blog: http://blogs.msdn.com/PowerShell/http://blogs.msdn.com/PowerShell/

Marc van Orsouw’s blog: http://mow001.blogspot.comMarc van Orsouw’s blog: http://mow001.blogspot.com

Wiki:Wiki: http://channel9.msdn.com/wiki/default.aspx/Channel9.WindowsPowerShellWikihttp://channel9.msdn.com/wiki/default.aspx/Channel9.WindowsPowerShellWiki

Script Center:Script Center:

http://www.microsoft.com/technet/scriptcenter/hubs/msh.mspxhttp://www.microsoft.com/technet/scriptcenter/hubs/msh.mspx

O’Reilly book – Monad: Introducing the MSH Command Shell and Language O’Reilly book – Monad: Introducing the MSH Command Shell and Language

http://www.oreilly.com/catalog/msh/http://www.oreilly.com/catalog/msh/

Manning Press book by Bruce Payette: Manning Press book by Bruce Payette: PowerShell in Action PowerShell in Action

http://manning.com/powershell/http://manning.com/powershell/

Sapien Press book by Don Jones: PowerShellSapien Press book by Don Jones: PowerShellhttp://www.sapienpress.com/http://www.sapienpress.com/

MshAnalyzer toolMshAnalyzer tool

http://www.karlprosser.com/coder/?cat=8http://www.karlprosser.com/coder/?cat=8

Page 25: MGT305 Windows PowerShell Next Generation Command Line Scripting Jeffrey Snover & Leonard Chung Windows Enterprise Management Division

ResourcesResourcesTechnical Chats and Webcastshttp://www.microsoft.com/communities/chats/default.mspx http://www.microsoft.com/usa/webcasts/default.asp

Microsoft Learning and Certificationhttp://www.microsoft.com/learning/default.mspx

MSDN & TechNet http://microsoft.com/msdnhttp://microsoft.com/technet

Virtual Labshttp://www.microsoft.com/technet/traincert/virtuallab/rms.mspx

Newsgroupshttp://communities2.microsoft.com/communities/newsgroups/en-us/default.aspx

Technical Community Siteshttp://www.microsoft.com/communities/default.mspx

User Groupshttp://www.microsoft.com/communities/usergroups/default.mspx

Page 26: MGT305 Windows PowerShell Next Generation Command Line Scripting Jeffrey Snover & Leonard Chung Windows Enterprise Management Division

© 2006 Microsoft Corporation. All rights reserved.This presentation is for informational purposes only.MICROSOFT MAKES NO WARRANTIES, EXPRESS OR IMPLIED, IN THIS SUMMARY.