Managing Systems with PowerShell Jeffrey P. Snover Partner Architect.

39
Managing Systems with PowerShell Jeffrey P. Snover Partner Architect

Transcript of Managing Systems with PowerShell Jeffrey P. Snover Partner Architect.

Page 1: Managing Systems with PowerShell Jeffrey P. Snover Partner Architect.

Managing Systems with PowerShellJeffrey P. SnoverPartner Architect

Page 2: Managing Systems with PowerShell Jeffrey P. Snover Partner Architect.

AgendaWhat is PowerShell?V2 Community Technology Preview (CTP)Learning PowerShellAutomating with PowerShell

Page 3: Managing Systems with PowerShell Jeffrey P. Snover Partner Architect.

Windows PowerShellNew command-line shell and scripting language

Windows PowerShellNew command-line shell and scripting language

As interactive and composable as BASH/KSHAs programmatic as Perl/Python/RubyAs production oriented as AS400 CL/VMS DCLAllows access to data stores as easy to access as filesystem

Page 4: Managing Systems with PowerShell Jeffrey P. Snover Partner Architect.

The Difference is OBJECTS!

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

Get-P

rocess

Cm

dle

t

Common Windows PowerShell Parser

Windows PowerShell Pipeline ProcessorW

here

Cm

dle

t

Sort

Cm

dle

t

Form

at

Cm

dle

t

Page 5: Managing Systems with PowerShell Jeffrey P. Snover Partner Architect.

Demo: Short Introduction to PowerShellJeffrey P SnoverPartner Architect

Page 6: Managing Systems with PowerShell Jeffrey P. Snover Partner Architect.

State of the SoftwarePhenomenal rate of adoption

> 1.55 Million downloads in < 1 yearExchange 2007, MOM 2007Virtual Machine Manager 2007Lotus Domino Transporter SuiteData Protection ManagerCompute Cluster Tool PackWindows ServerBackupGood ISV pickup> 20 new internal partners

CEC 2009 requirementWon both Best of TechEd and Teched Attendees Pick

Page 7: Managing Systems with PowerShell Jeffrey P. Snover Partner Architect.

State of the Software - CommunityActive and useful Newsgroups and Forums

25 Codeplex projects (2 in the top 25 most active)Strong group of MVPsStrong community tools

Page 8: Managing Systems with PowerShell Jeffrey P. Snover Partner Architect.

Tower of Power

Page 9: Managing Systems with PowerShell Jeffrey P. Snover Partner Architect.

ProductivityExchange 2003 (VBScript) E12 (Monad Script)

Mailbox Statistics

Set listExchange_Mailboxs = GetObject("winmgmts:{impersonationLevel=impersonate}!\\COMPUTERNAME\ROOT\MicrosoftExchangeV2").InstancesOf("Exchange_Mailbox")

For Each objExchange_Mailbox in listExchange_MailboxsWScript.echo "AssocContentCount =” + objExchange_Mailbox.AssocContentCountWScript.echo " DateDiscoveredAbsentInDS =” + objExchange_Mailbox.DateDiscoveredAbsentInDSWScript.echo " DeletedMessageSizeExtended =” + objExchange_Mailbox. DeletedMessageSizeExtendedWScript.echo " LastLoggedOnUserAccount =” + objExchange_Mailbox. LastLoggedOnUserAccountWScript.echo " LastLogoffTime =” + objExchange_Mailbox. LastLogoffTimeWScript.echo " LastLogonTime =” + objExchange_Mailbox. LastLogonTime WScript.echo " LegacyDN =” + objExchange_Mailbox. LegacyDNWScript.echo " MailboxDisplayName =” + objExchange_Mailbox. MailboxDisplayNameWScript.echo " MailboxGUID =” + objExchange_Mailbox. MailboxGUID WScript.echo " ServerName =” + objExchange_Mailbox. ServerName WScript.echo " Size =” + objExchange_Mailbox. SizeWScript.echo " StorageGroupName =” + objExchange_Mailbox. StorageGroupName WScript.echo " StorageLimitInfo =” + objExchange_Mailbox. StorageLimitInfo WScript.echo " StoreName =” + objExchange_Mailbox. StoreName WScript.echo " TotalItems =” + objExchange_Mailbox. TotalItems Next

get-mailboxstatistics –server $servername

Database Mgmt

Dim StorGroup as New CDOEXM.StorageGroup

StorGroup.DataSource.Open "LDAP://" + DCServer + "/ CN=First Storage Group,CN=InformationStore,CN=" + Server + ",CN=Servers,CN=First Administrative Group, CN=Administrative Groups,CN=First Organization, CN=Microsoft Exchange,CN=Services, CN=Configuration," + DomainName

StorGroup.MoveLogFiles("C:\newlogPath", 0)

move-storagegrouppath -identity “First Storage Group“ –log "C:\newlogPath”

Recipient Mgmt

Dim objMailbox As CDOEXM.IMailboxStore

Set objMailbox = GetObject("LDAP://" + DCServer + "CN=FOO,CN=users," + DomainName)

objMailbox.CreateMailbox "LDAP://" + DCServer + "/CN=Private MDB,CN=First Storage Group,CN=InformationStore,CN=" + Server + ",CN=Servers,CN=First Administrative Group, CN=Administrative Groups,CN=First Organization, CN=Microsoft Exchange,CN=Services, CN=Configuration," + DomainName

enable-mailbox -identity domain\FOO –database “First Storage Group\Private MDB”

Page 10: Managing Systems with PowerShell Jeffrey P. Snover Partner Architect.

PartnerDmitry SotnikovNew Product Research ManagerQuest Software

Page 11: Managing Systems with PowerShell Jeffrey P. Snover Partner Architect.

Announcing : PowerShell V2 Community Technology Preview (CTP)

Page 12: Managing Systems with PowerShell Jeffrey P. Snover Partner Architect.

Availability /Applicability

The PowerShell V2 CTP is not for everyoneRead this PowerShell Team blog entry:

http://blogs.msdn.com/powershell/archive/2007/11/02/ctp-watch-this-space.aspx 

Page 13: Managing Systems with PowerShell Jeffrey P. Snover Partner Architect.

PowerShell V2 Themes

Universal Code Execution Model

GUI over PowerShell

Production Scripting

Community Feedback

Page 14: Managing Systems with PowerShell Jeffrey P. Snover Partner Architect.

PowerShell V2 Themes

Universal Code Execution Model

• Commands, expressions and ScriptBlocks can be run• in the foreground or background • on one or more machines• over a LAN or a WAN• in unrestricted or restricted environments• using short or long connections• using impersonation or supplied credentials• initiated by user input or by events

Page 15: Managing Systems with PowerShell Jeffrey P. Snover Partner Architect.

Features

Universal Code Execution Model

• Remoting• Mobile Object Model• Background Jobs• Restricted Runspaces• Eventing (coming soon)

Page 16: Managing Systems with PowerShell Jeffrey P. Snover Partner Architect.

Features

GUI over PowerShell

• New hosting APIs• Runspace pooling

Page 17: Managing Systems with PowerShell Jeffrey P. Snover Partner Architect.

Features

Production Scripting

• Graphical PowerShell• Script Cmdlets• Script Internationalization/Data Language• Debugging• Packages and modules (coming soon)

Page 18: Managing Systems with PowerShell Jeffrey P. Snover Partner Architect.

Features

Community Feedback

• Language enhancements• Improved adapters• New/improved cmdlets

Page 19: Managing Systems with PowerShell Jeffrey P. Snover Partner Architect.

Call To Action

Download PowerShell CTPOn non-production systems

Kick the tires and let us know what we got right/wrongLet us know about any incompatibilities with V1

Page 20: Managing Systems with PowerShell Jeffrey P. Snover Partner Architect.

Learning PowerShell

Invest and learn how to:Learn and leverage others workDiscover the systemLeverage utilitiesFormalize your work

Methods apply to V1 and V2

Page 21: Managing Systems with PowerShell Jeffrey P. Snover Partner Architect.

Learning and LeveragingIn the box documents

Release NotesGetting Started, Users Guide, Quick Reference GuideHelp

Discovery utilitiesBooksCommunityPractice ad hoc development

Page 22: Managing Systems with PowerShell Jeffrey P. Snover Partner Architect.

Utilities - Discovery

Command Function

Get-Help Help about PowerShell commands and topics

Get-Command

Get information about anything that can be invoked

Get-Member Show what can be done with an object

Get-PSDrive Shows what object stores are available

Page 23: Managing Systems with PowerShell Jeffrey P. Snover Partner Architect.

Demo: Discovery UtilitiesJeffrey P SnoverPartner Architect

Page 24: Managing Systems with PowerShell Jeffrey P. Snover Partner Architect.

Utilities – Object Manipulation

Command

Action

Compare Compare 2 sets of objects

Group Split a set of objects into groups

Measure Measure some property of a set of objects

Select Select a set of properties from a set of objects

Sort Sort objects

Tee Make a copy of a set of objects

Where Select a subset of objects

Page 25: Managing Systems with PowerShell Jeffrey P. Snover Partner Architect.

Demo: Object Manipulation UtilitiesJeffrey P SnoverPartner Architect

Page 26: Managing Systems with PowerShell Jeffrey P. Snover Partner Architect.

Transformation and Output

Commands Functions

Format -Custom -List -Table -Wide

Convert objects into formatting records

Out -File -Host -Printer -String

Convert formatting records into output-specific directives.

Export/Import -CliXML -CSV

Converts objects into and out of file formats

ConvertTo -HTML

Converts object into other objects

Page 27: Managing Systems with PowerShell Jeffrey P. Snover Partner Architect.

Demo: Transformation and Output UtilitiesJeffrey P SnoverPartner Architect

Page 28: Managing Systems with PowerShell Jeffrey P. Snover Partner Architect.

PowerShell ScriptingInstrumentation

TextCOM.NETCmdlets

Object typesWMIXMLADOADSI

Page 29: Managing Systems with PowerShell Jeffrey P. Snover Partner Architect.

Demo: PowerShell ScriptingJeffrey P SnoverPartner Architect

Page 30: Managing Systems with PowerShell Jeffrey P. Snover Partner Architect.

Summary/Call to ActionNow available at: www.microsoft.com/downloads

Search for PowerShell

Try it, Deploy it, Use it, Share

Automate and Prosper!

Page 31: Managing Systems with PowerShell Jeffrey P. Snover Partner Architect.

Community ResourcesNewsgroup: Microsoft.Public.Windows.PowerShellTeam blog:

http://blogs.msdn.com/PowerShell/PowerShellCommunity.Org:

http://www.PowershellCommunity.OrgChannel 9

http://channel9.msdn.com/tags/PowerShellWiki

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

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

CodePlex:http://codeplex.com/Project/ProjectDirectory.aspx?TagName=powershell

Many excellent booksManning Press book by PowerShell Dev Lead Bruce Payette: PowerShell in Action

http://manning.com/powershell/ O’Reilly book by PowerShell Dev Lee Holmes – Windows PowerShell Cookbook

http://www.oreilly.com/catalog/9780596528492/index.html

Page 32: Managing Systems with PowerShell Jeffrey P. Snover Partner Architect.

Questions?

Page 33: Managing Systems with PowerShell Jeffrey P. Snover Partner Architect.

Complete an evaluation on CommNet and enter to win!

Page 34: Managing Systems with PowerShell Jeffrey P. Snover Partner Architect.

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

Page 35: Managing Systems with PowerShell Jeffrey P. Snover Partner Architect.

Scripting with COMAccess existing instrumentation

Bind to COM objects$fso = New-Object -ComObject Scripting.FileSystemObject$m = [System.Runtime.InteropServices.Marshal]$word = $m::GetActiveObject("Word.Application")

Invoke methods/access properties$fso.GetDrive(“C:”)$fso.VolumeName = “System Drive”

Understand/extend instrumentationExtend and discover properties/methods

Update-TypeData Office.Word.Types.ps1xml$fso | Get-Member

Manipulate and format resultsDefine and import custom formating

Update-FormatData Office.Word.Format.ps1xml$word.RecentFiles | Sort name | Format-Table

Allows more simpler/more powerful COM scripts because of utilities and formatting

Page 36: Managing Systems with PowerShell Jeffrey P. Snover Partner Architect.

Scripting with WMIPowerShell provides native WMI supportGet-WmiObject

Allows for inspection of WMI namespaceGet-WmiObject –list [-Namespace xx]Get-WmiObject –Class xx –Namespace xx –Property xxx – Filter xxx –ComputerName xxx –Credential xxx

Native language support[WMI] “\\JPSDESK10\root\cimv2:Win32_Process.Handle="0“[WMICLASS] "ROOT\cimv2:WIN32_PROCESS"[WMISEARCHER]"select * from Win32_process WHERE Name = 'calc.exe'"

Page 37: Managing Systems with PowerShell Jeffrey P. Snover Partner Architect.

Scripting with .NETPowerShell provides native access to any .NET classCreate any object

[reflection.assembly]::LoadWithPartialName("System.Windows.Forms")$d = New-Object System.DateTime 2006,12,25

Access Properties/Invoke Methods$d.DayOfWeek$d.AddDays(-30)

Access Statics[DateTime]::Now[DateTime]::IsLeapYear(2006)

Allows admins to easily access and leverage a huge API set because of scriptability, utilities and formatting

Page 38: Managing Systems with PowerShell Jeffrey P. Snover Partner Architect.

Scripting with XMLPowerShell provides native XML supportNative datatype

$x=[xml]"<a><b><c>TEST</c></b></a>“$b =[xml](type c:\i386\mssecure.xml)

Native syntax to access “data” view of properties$b.BulletinDataStore.Bulletins.Bulletin[0]

Access to XML methods $b.BulletinDataStore.SelectNodes(“//Patch”)

XML properties available through PSBase property$b.BulletinDataStore.PSBase.innerXml

Page 39: Managing Systems with PowerShell Jeffrey P. Snover Partner Architect.

Scripting with TextInvoke existing tools

Existing command run directly after variables are expanded

Harvest data from existing toolsParse output into variables using text utilities.

Pipe data to SELECT and use –FIRST and –LAST Select-String <REGEX> <Path>Dir | Select-String <REGEX>[DateTime]”12/25/2006 7:00”([DateTime]”12/25/2006 7:00”).AddDays(-30)

Use functions/scripts to wrap the commands and convert output to objects or provide standard syntax

Safely process textUse CLR types via Windows PowerShell to safely parse text

[URI]” http://blogs.msdn.com/powershell/archive/2006/04/25/583234.aspx”

Allows admins to get 2-10x more power out of existing commands because of scriptability