svcc 5 introduction to powershell
DESCRIPTION
Session Given Oct 9th 2010 @ Silicon Valley Code Camp.TRANSCRIPT
![Page 1: SVCC 5 introduction to powershell](https://reader033.vdocuments.net/reader033/viewer/2022061218/54b7ca824a7959556a8b45bc/html5/thumbnails/1.jpg)
Introduction To Powershell
Paul H CassidyTest Automation Lead
Front Porch – Sonora, CAhttp://www.twitter.com/qa_warrior
![Page 2: SVCC 5 introduction to powershell](https://reader033.vdocuments.net/reader033/viewer/2022061218/54b7ca824a7959556a8b45bc/html5/thumbnails/2.jpg)
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](https://reader033.vdocuments.net/reader033/viewer/2022061218/54b7ca824a7959556a8b45bc/html5/thumbnails/3.jpg)
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](https://reader033.vdocuments.net/reader033/viewer/2022061218/54b7ca824a7959556a8b45bc/html5/thumbnails/4.jpg)
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](https://reader033.vdocuments.net/reader033/viewer/2022061218/54b7ca824a7959556a8b45bc/html5/thumbnails/5.jpg)
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](https://reader033.vdocuments.net/reader033/viewer/2022061218/54b7ca824a7959556a8b45bc/html5/thumbnails/6.jpg)
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](https://reader033.vdocuments.net/reader033/viewer/2022061218/54b7ca824a7959556a8b45bc/html5/thumbnails/7.jpg)
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](https://reader033.vdocuments.net/reader033/viewer/2022061218/54b7ca824a7959556a8b45bc/html5/thumbnails/8.jpg)
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](https://reader033.vdocuments.net/reader033/viewer/2022061218/54b7ca824a7959556a8b45bc/html5/thumbnails/9.jpg)
Discovery Utilities
Windows PowerShell
demo
![Page 10: SVCC 5 introduction to powershell](https://reader033.vdocuments.net/reader033/viewer/2022061218/54b7ca824a7959556a8b45bc/html5/thumbnails/10.jpg)
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](https://reader033.vdocuments.net/reader033/viewer/2022061218/54b7ca824a7959556a8b45bc/html5/thumbnails/11.jpg)
Object Manipulations Utilities
Windows PowerShell
demo
![Page 12: SVCC 5 introduction to powershell](https://reader033.vdocuments.net/reader033/viewer/2022061218/54b7ca824a7959556a8b45bc/html5/thumbnails/12.jpg)
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](https://reader033.vdocuments.net/reader033/viewer/2022061218/54b7ca824a7959556a8b45bc/html5/thumbnails/13.jpg)
Transformation and Output Utilities
Windows PowerShell
demo
![Page 14: SVCC 5 introduction to powershell](https://reader033.vdocuments.net/reader033/viewer/2022061218/54b7ca824a7959556a8b45bc/html5/thumbnails/14.jpg)
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](https://reader033.vdocuments.net/reader033/viewer/2022061218/54b7ca824a7959556a8b45bc/html5/thumbnails/15.jpg)
.Net Scripting
Windows PowerShell
demo
![Page 16: SVCC 5 introduction to powershell](https://reader033.vdocuments.net/reader033/viewer/2022061218/54b7ca824a7959556a8b45bc/html5/thumbnails/16.jpg)
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](https://reader033.vdocuments.net/reader033/viewer/2022061218/54b7ca824a7959556a8b45bc/html5/thumbnails/17.jpg)
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](https://reader033.vdocuments.net/reader033/viewer/2022061218/54b7ca824a7959556a8b45bc/html5/thumbnails/18.jpg)
WMI Scripting
Windows PowerShell
demo
![Page 19: SVCC 5 introduction to powershell](https://reader033.vdocuments.net/reader033/viewer/2022061218/54b7ca824a7959556a8b45bc/html5/thumbnails/19.jpg)
PowerShell provides native XML support◦ [XML]”<root>…. </root>”
Projects a “data-view” of XML
Scripting with XML
![Page 20: SVCC 5 introduction to powershell](https://reader033.vdocuments.net/reader033/viewer/2022061218/54b7ca824a7959556a8b45bc/html5/thumbnails/20.jpg)
XML Scripting
Windows PowerShell
demo
![Page 21: SVCC 5 introduction to powershell](https://reader033.vdocuments.net/reader033/viewer/2022061218/54b7ca824a7959556a8b45bc/html5/thumbnails/21.jpg)
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](https://reader033.vdocuments.net/reader033/viewer/2022061218/54b7ca824a7959556a8b45bc/html5/thumbnails/22.jpg)
Q&A