Introduction To Powershell Paul H Cassidy Test Automation Lead Front Porch – Sonora, CA http://www.twitter.com/qa_warrior
Jan 15, 2015
Introduction To Powershell
Paul H CassidyTest Automation Lead
Front Porch – Sonora, CAhttp://www.twitter.com/qa_warrior
Agenda Introduce PowerShell Learn to investigate and explore PowerShell Demo’s showing a bit of what is possible
with PowerShell
Windows PowerShell
A command line and scripting language◦ As interactive and engaging as BASH/KSH◦ As programmatic as Perl/Python/Ruby
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
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
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”
Learn and LeverageIn the box documents
Release NotesGetting Started, User’s Guide, Quick ReferenceHelp
Discovery utilitiesBooksCommunityPractice ad hoc development
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
Discovery Utilities
Windows PowerShell
demo
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
Object Manipulations Utilities
Windows PowerShell
demo
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
Transformation and Output Utilities
Windows PowerShell
demo
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
.Net Scripting
Windows PowerShell
demo
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
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
WMI Scripting
Windows PowerShell
demo
PowerShell provides native XML support◦ [XML]”<root>…. </root>”
Projects a “data-view” of XML
Scripting with XML
XML Scripting
Windows PowerShell
demo
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
Q&A