managing systems with powershell - windows · pdf filemanaging systems with powershell jeffrey...

39
Managing Systems with PowerShell Jeffrey P. Snover Partner Architect

Upload: vominh

Post on 08-Mar-2018

234 views

Category:

Documents


2 download

TRANSCRIPT

Page 1: Managing Systems with PowerShell - Windows · PDF fileManaging Systems with PowerShell Jeffrey P. Snover ... As programmatic as Perl/Python/Ruby ... The PowerShell V2 CTP is not for

Managing Systems with

PowerShell

Jeffrey P. Snover

Partner Architect

Page 2: Managing Systems with PowerShell - Windows · PDF fileManaging Systems with PowerShell Jeffrey P. Snover ... As programmatic as Perl/Python/Ruby ... The PowerShell V2 CTP is not for

What is PowerShell?

V2 Community Technology Preview (CTP)

Learning PowerShell

Automating with PowerShell

Page 3: Managing Systems with PowerShell - Windows · PDF fileManaging Systems with PowerShell Jeffrey P. Snover ... As programmatic as Perl/Python/Ruby ... The PowerShell V2 CTP is not for

Windows PowerShellNew command-line shell and scripting language

As interactive and composable as BASH/KSH

As programmatic as Perl/Python/Ruby

As production oriented as AS400 CL/VMS DCL

Allows access to data stores as easy to access as

filesystem

Page 4: Managing Systems with PowerShell - Windows · PDF fileManaging Systems with PowerShell Jeffrey P. Snover ... As programmatic as Perl/Python/Ruby ... The PowerShell V2 CTP is not for

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 Processor

Wh

ere

Cm

dle

t

So

rt

Cm

dle

t

Fo

rmat

Cm

dle

t

Page 5: Managing Systems with PowerShell - Windows · PDF fileManaging Systems with PowerShell Jeffrey P. Snover ... As programmatic as Perl/Python/Ruby ... The PowerShell V2 CTP is not for

Demo: Short Introduction to

PowerShell

Jeffrey P Snover

Partner Architect

Page 6: Managing Systems with PowerShell - Windows · PDF fileManaging Systems with PowerShell Jeffrey P. Snover ... As programmatic as Perl/Python/Ruby ... The PowerShell V2 CTP is not for

State of the SoftwarePhenomenal rate of adoption

> 1.55 Million downloads in < 1 year

Exchange 2007, MOM 2007

Virtual Machine Manager 2007

Lotus Domino Transporter Suite

Data Protection Manager

Compute Cluster Tool Pack

Windows Server

Backup

Good ISV pickup

> 20 new internal partners

CEC 2009 requirement

Won both Best of TechEd and Teched Attendees Pick

Page 7: Managing Systems with PowerShell - Windows · PDF fileManaging Systems with PowerShell Jeffrey P. Snover ... As programmatic as Perl/Python/Ruby ... The PowerShell V2 CTP is not for

State of the Software - CommunityActive and useful Newsgroups and Forums

25 Codeplex projects (2 in the top 25 most active)

Strong group of MVPs

Strong community tools

Page 8: Managing Systems with PowerShell - Windows · PDF fileManaging Systems with PowerShell Jeffrey P. Snover ... As programmatic as Perl/Python/Ruby ... The PowerShell V2 CTP is not for

Tower of Power

Page 9: Managing Systems with PowerShell - Windows · PDF fileManaging Systems with PowerShell Jeffrey P. Snover ... As programmatic as Perl/Python/Ruby ... The PowerShell V2 CTP is not for

Exchange 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_Mailboxs

WScript.echo "AssocContentCount =” + objExchange_Mailbox.AssocContentCount

WScript.echo " DateDiscoveredAbsentInDS =” + objExchange_Mailbox.DateDiscoveredAbsentInDS

WScript.echo " DeletedMessageSizeExtended =” + objExchange_Mailbox. DeletedMessageSizeExtended

WScript.echo " LastLoggedOnUserAccount =” + objExchange_Mailbox. LastLoggedOnUserAccount

WScript.echo " LastLogoffTime =” + objExchange_Mailbox. LastLogoffTime

WScript.echo " LastLogonTime =” + objExchange_Mailbox. LastLogonTime

WScript.echo " LegacyDN =” + objExchange_Mailbox. LegacyDN

WScript.echo " MailboxDisplayName =” + objExchange_Mailbox. MailboxDisplayName

WScript.echo " MailboxGUID =” + objExchange_Mailbox. MailboxGUID

WScript.echo " ServerName =” + objExchange_Mailbox. ServerName

WScript.echo " Size =” + objExchange_Mailbox. Size

WScript.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 - Windows · PDF fileManaging Systems with PowerShell Jeffrey P. Snover ... As programmatic as Perl/Python/Ruby ... The PowerShell V2 CTP is not for

Partner

Dmitry Sotnikov

New Product Research Manager

Quest Software

Page 11: Managing Systems with PowerShell - Windows · PDF fileManaging Systems with PowerShell Jeffrey P. Snover ... As programmatic as Perl/Python/Ruby ... The PowerShell V2 CTP is not for

Announcing :

PowerShell V2 Community

Technology Preview (CTP)

Page 13: Managing Systems with PowerShell - Windows · PDF fileManaging Systems with PowerShell Jeffrey P. Snover ... As programmatic as Perl/Python/Ruby ... The PowerShell V2 CTP is not for

PowerShell V2 Themes

Universal Code Execution Model

GUI over PowerShell

Production Scripting

Community Feedback

Page 14: Managing Systems with PowerShell - Windows · PDF fileManaging Systems with PowerShell Jeffrey P. Snover ... As programmatic as Perl/Python/Ruby ... The PowerShell V2 CTP is not for

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 - Windows · PDF fileManaging Systems with PowerShell Jeffrey P. Snover ... As programmatic as Perl/Python/Ruby ... The PowerShell V2 CTP is not for

Features

Universal Code Execution Model

• Remoting

• Mobile Object Model

• Background Jobs

• Restricted Runspaces

• Eventing (coming soon)

Page 16: Managing Systems with PowerShell - Windows · PDF fileManaging Systems with PowerShell Jeffrey P. Snover ... As programmatic as Perl/Python/Ruby ... The PowerShell V2 CTP is not for

Features

GUI over PowerShell

• New hosting APIs

• Runspace pooling

Page 17: Managing Systems with PowerShell - Windows · PDF fileManaging Systems with PowerShell Jeffrey P. Snover ... As programmatic as Perl/Python/Ruby ... The PowerShell V2 CTP is not for

Features

Production Scripting

• Graphical PowerShell

• Script Cmdlets

• Script Internationalization/Data Language

• Debugging

• Packages and modules (coming soon)

Page 18: Managing Systems with PowerShell - Windows · PDF fileManaging Systems with PowerShell Jeffrey P. Snover ... As programmatic as Perl/Python/Ruby ... The PowerShell V2 CTP is not for

Features

Community Feedback

• Language enhancements

• Improved adapters

• New/improved cmdlets

Page 19: Managing Systems with PowerShell - Windows · PDF fileManaging Systems with PowerShell Jeffrey P. Snover ... As programmatic as Perl/Python/Ruby ... The PowerShell V2 CTP is not for

Call To Action

Download PowerShell CTP

On non-production systems

Kick the tires and let us know what we got right/wrong

Let us know about any incompatibilities with V1

Page 20: Managing Systems with PowerShell - Windows · PDF fileManaging Systems with PowerShell Jeffrey P. Snover ... As programmatic as Perl/Python/Ruby ... The PowerShell V2 CTP is not for

Invest and learn how to:Learn and leverage others work

Discover the system

Leverage utilities

Formalize your work

Methods apply to V1 and V2

Page 21: Managing Systems with PowerShell - Windows · PDF fileManaging Systems with PowerShell Jeffrey P. Snover ... As programmatic as Perl/Python/Ruby ... The PowerShell V2 CTP is not for

In the box documents

Release Notes

Getting Started, Users Guide, Quick Reference Guide

Help

Discovery utilities

Books

Community

Practice ad hoc development

Page 22: Managing Systems with PowerShell - Windows · PDF fileManaging Systems with PowerShell Jeffrey P. Snover ... As programmatic as Perl/Python/Ruby ... The PowerShell V2 CTP is not for

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 - Windows · PDF fileManaging Systems with PowerShell Jeffrey P. Snover ... As programmatic as Perl/Python/Ruby ... The PowerShell V2 CTP is not for

Demo: Discovery Utilities

Jeffrey P Snover

Partner Architect

Page 24: Managing Systems with PowerShell - Windows · PDF fileManaging Systems with PowerShell Jeffrey P. Snover ... As programmatic as Perl/Python/Ruby ... The PowerShell V2 CTP is not for

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 - Windows · PDF fileManaging Systems with PowerShell Jeffrey P. Snover ... As programmatic as Perl/Python/Ruby ... The PowerShell V2 CTP is not for

Demo: Object Manipulation

Utilities

Jeffrey P Snover

Partner Architect

Page 26: Managing Systems with PowerShell - Windows · PDF fileManaging Systems with PowerShell Jeffrey P. Snover ... As programmatic as Perl/Python/Ruby ... The PowerShell V2 CTP is not for

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 - Windows · PDF fileManaging Systems with PowerShell Jeffrey P. Snover ... As programmatic as Perl/Python/Ruby ... The PowerShell V2 CTP is not for

Demo: Transformation and

Output Utilities

Jeffrey P Snover

Partner Architect

Page 28: Managing Systems with PowerShell - Windows · PDF fileManaging Systems with PowerShell Jeffrey P. Snover ... As programmatic as Perl/Python/Ruby ... The PowerShell V2 CTP is not for

Instrumentation

Text

COM

.NET

Cmdlets

Object types

WMI

XML

ADO

ADSI

Page 29: Managing Systems with PowerShell - Windows · PDF fileManaging Systems with PowerShell Jeffrey P. Snover ... As programmatic as Perl/Python/Ruby ... The PowerShell V2 CTP is not for

Demo: PowerShell Scripting

Jeffrey P Snover

Partner Architect

Page 30: Managing Systems with PowerShell - Windows · PDF fileManaging Systems with PowerShell Jeffrey P. Snover ... As programmatic as Perl/Python/Ruby ... The PowerShell V2 CTP is not for

Now 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 - Windows · PDF fileManaging Systems with PowerShell Jeffrey P. Snover ... As programmatic as Perl/Python/Ruby ... The PowerShell V2 CTP is not for

Newsgroup: Microsoft.Public.Windows.PowerShell

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

PowerShellCommunity.Org: http://www.PowershellCommunity.Org

Channel 9 http://channel9.msdn.com/tags/PowerShell

Wikihttp://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 - Windows · PDF fileManaging Systems with PowerShell Jeffrey P. Snover ... As programmatic as Perl/Python/Ruby ... The PowerShell V2 CTP is not for

Questions?

Page 33: Managing Systems with PowerShell - Windows · PDF fileManaging Systems with PowerShell Jeffrey P. Snover ... As programmatic as Perl/Python/Ruby ... The PowerShell V2 CTP is not for

Complete an evaluation on CommNet

and enter to win!

Page 34: Managing Systems with PowerShell - Windows · PDF fileManaging Systems with PowerShell Jeffrey P. Snover ... As programmatic as Perl/Python/Ruby ... The PowerShell V2 CTP is not for

© 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 - Windows · PDF fileManaging Systems with PowerShell Jeffrey P. Snover ... As programmatic as Perl/Python/Ruby ... The PowerShell V2 CTP is not for

Access 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 instrumentation

Extend and discover properties/methods

Update-TypeData Office.Word.Types.ps1xml

$fso | Get-Member

Manipulate and format results

Define 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 - Windows · PDF fileManaging Systems with PowerShell Jeffrey P. Snover ... As programmatic as Perl/Python/Ruby ... The PowerShell V2 CTP is not for

PowerShell provides native WMI support

Get-WmiObject

Allows for inspection of WMI namespace

Get-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 - Windows · PDF fileManaging Systems with PowerShell Jeffrey P. Snover ... As programmatic as Perl/Python/Ruby ... The PowerShell V2 CTP is not for

PowerShell provides native access to any .NET class

Create any object

[reflection.assembly]::LoadWithPartialName("System.Windows.Fo

rms")

$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 - Windows · PDF fileManaging Systems with PowerShell Jeffrey P. Snover ... As programmatic as Perl/Python/Ruby ... The PowerShell V2 CTP is not for

PowerShell provides native XML support

Native 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 - Windows · PDF fileManaging Systems with PowerShell Jeffrey P. Snover ... As programmatic as Perl/Python/Ruby ... The PowerShell V2 CTP is not for

Invoke existing tools

Existing command run directly after variables are expanded

Harvest data from existing tools

Parse 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 text

Use 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