Top Banner
Introduction To Powershell Paul H Cassidy Test Automation Lead Front Porch – Sonora, CA http://www.twitter.com/qa_warrior
22

SVCC 5 introduction to powershell

Jan 15, 2015

Download

Technology

qawarrior

Session Given Oct 9th 2010 @ Silicon Valley Code Camp.
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: SVCC 5 introduction to powershell

Introduction To Powershell

Paul H CassidyTest Automation Lead

Front Porch – Sonora, CAhttp://www.twitter.com/qa_warrior

Page 2: SVCC 5 introduction to powershell

Agenda Introduce PowerShell Learn to investigate and explore PowerShell Demo’s showing a bit of what is possible

with PowerShell

Page 3: SVCC 5 introduction to powershell

Windows PowerShell

A command line and scripting language◦ As interactive and engaging as BASH/KSH◦ As programmatic as Perl/Python/Ruby

Page 4: SVCC 5 introduction to powershell

State of the Software - Community• Active and useful Newsgroups and Forums• Lots of active Codeplex projects• Strong group of MVPs• Strong community tools• Lots of books covering specific and general areas

Page 5: SVCC 5 introduction to powershell

The Difference is OBJECTS!

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

Get-P

rocess

Cm

dlet

Common Windows PowerShell Parser

Windows PowerShell Pipeline ProcessorW

here C

md

let

So

rt C

md

let

Fo

rmat

Cm

dlet

Page 6: SVCC 5 introduction to powershell

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 7: SVCC 5 introduction to powershell

Learn and LeverageIn the box documents

Release NotesGetting Started, User’s Guide, Quick ReferenceHelp

Discovery utilitiesBooksCommunityPractice ad hoc development

Page 8: SVCC 5 introduction to powershell

Command Function

Get-Command

Get information about anything that can be invoked

Get-Help Help about PowerShell commands and topics

Get-Member Show what can be done with an object

Get-PSDrive Shows what object stores are available

Utilities - Discovery

Page 9: SVCC 5 introduction to powershell

Discovery Utilities

Windows PowerShell

demo

Page 10: SVCC 5 introduction to powershell

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

Utilities – Object Manipulation

Page 11: SVCC 5 introduction to powershell

Object Manipulations Utilities

Windows PowerShell

demo

Page 12: SVCC 5 introduction to powershell

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

Transformation and Output

Page 13: SVCC 5 introduction to powershell

Transformation and Output Utilities

Windows PowerShell

demo

Page 14: SVCC 5 introduction to powershell

PowerShell provides native access to any .NET class

Create any object◦ [reflection.assembly]::LoadWithPartialName("System.Wind

ows.Forms")◦ $d = New-Object System.DateTime 2006,12,25

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

Access Statics◦ [DateTime]::Now◦ [DateTime]::IsLeapYear(2008)

Scripting with .NET

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

Page 15: SVCC 5 introduction to powershell

.Net Scripting

Windows PowerShell

demo

Page 16: SVCC 5 introduction to powershell

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”

Manipulate and format results◦Define and import custom formating

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

Scripting with COM

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

Page 17: SVCC 5 introduction to powershell

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'"

Scripting with WMI

Page 18: SVCC 5 introduction to powershell

WMI Scripting

Windows PowerShell

demo

Page 19: SVCC 5 introduction to powershell

PowerShell provides native XML support◦ [XML]”<root>…. </root>”

Projects a “data-view” of XML

Scripting with XML

Page 20: SVCC 5 introduction to powershell

XML Scripting

Windows PowerShell

demo

Page 21: SVCC 5 introduction to powershell

Community ResourcesNewsgroup: Microsoft.Public.Windows.PowerShellTeam blog: http://blogs.msdn.com/PowerShell/PowerShell Community: http://www.PowershellCommunity.OrgCommunity Script Repo http://www.poshcode.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.mspxCodePlex:

http://codeplex.com/Project/ProjectDirectory.aspx?TagName=powershellMany excellent books

Manning 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

Session Demos for Introduction To Powershell

Page 22: SVCC 5 introduction to powershell

Q&A