managing systems with powershell - windows · pdf filemanaging systems with powershell jeffrey...
TRANSCRIPT
Managing Systems with
PowerShell
Jeffrey P. Snover
Partner Architect
What is PowerShell?
V2 Community Technology Preview (CTP)
Learning PowerShell
Automating with PowerShell
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
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
Demo: Short Introduction to
PowerShell
Jeffrey P Snover
Partner Architect
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
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
Tower of Power
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”
Partner
Dmitry Sotnikov
New Product Research Manager
Quest Software
Announcing :
PowerShell V2 Community
Technology Preview (CTP)
Availability /Applicability
The PowerShell V2 CTP is not for everyone
Read this PowerShell Team blog entry:
http://blogs.msdn.com/powershell/archive/2007/11/02/ctp-
watch-this-space.aspx
PowerShell V2 Themes
Universal Code Execution Model
GUI over PowerShell
Production Scripting
Community Feedback
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
Features
Universal Code Execution Model
• Remoting
• Mobile Object Model
• Background Jobs
• Restricted Runspaces
• Eventing (coming soon)
Features
GUI over PowerShell
• New hosting APIs
• Runspace pooling
Features
Production Scripting
• Graphical PowerShell
• Script Cmdlets
• Script Internationalization/Data Language
• Debugging
• Packages and modules (coming soon)
Features
Community Feedback
• Language enhancements
• Improved adapters
• New/improved cmdlets
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
Invest and learn how to:Learn and leverage others work
Discover the system
Leverage utilities
Formalize your work
Methods apply to V1 and V2
In the box documents
Release Notes
Getting Started, Users Guide, Quick Reference Guide
Help
Discovery utilities
Books
Community
Practice ad hoc development
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
Demo: Discovery Utilities
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
Demo: Object Manipulation
Utilities
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
Demo: Transformation and
Output Utilities
Jeffrey P Snover
Partner Architect
Instrumentation
Text
COM
.NET
Cmdlets
Object types
WMI
XML
ADO
ADSI
Demo: PowerShell Scripting
Jeffrey P Snover
Partner Architect
Now available at: www.microsoft.com/downloads
Search for PowerShell
Try it, Deploy it, Use it, Share
Automate and Prosper!
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
Questions?
Complete an evaluation on CommNet
and enter to win!
© 2007 Microsoft Corporation. All rights reserved.
This presentation is for informational purposes only.
MICROSOFT MAKES NO WARRANTIES, EXPRESS OR IMPLIED, IN THIS SUMMARY.
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
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'"
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
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
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