PowerShell Fundamentals
1 2009 HP Confidential 2009 HP Confidential
PowerShell Webinars
Tuesday, February 02, 2010 PowerShell Fundamentals Part I Dung K Hoang Thursday, February 04, 2010 PowerShell Fundamentals Part II Dung K Hoang
Tuesday, February 09, 2010 PowerShell - Active Directory Guido Grillenmeier
Thursday, February 11, 2010 PowerShell - SCVMM - Guido Grillenmeier
Tuesday, February 16, 2010
PowerShell Clustering Aric Bernard
Thursday, February 18, 2010
PowerShell - Exchange David Standish/Joel Ricketts/ Pete Petree
Tuesday, February 23, 2010
Thursday, February 25, 2010
PowerShell - Exchange Web Service Kevin Laahs
PowerShell - SCOM - Tommy Carron
Tuesday, March 02, 2010 PowerShell - MOSS 2007/2010 - Veli-Matti Vanamo /Brian Carter
Thursday, March 04, 2010 PowerShell - VMware/Hyper-V Steve Fortuna/Ken J Bell
Tuesday, March 09, 2010 POwerShell - Citrix Workflow Albert Hols
Thursday, March 11, 2010 Advanced PowerShell Dung K Hoang
Tuesday, March 16, 2010
2 2009 HP Confidential
Thursday, March 18, 2010 PowerShell and Office apps David Lee
PowerShell - WMI SCCM David Lee
This presentation is brought to you by the Microsoft Profession
Communities
Knowledge Sharing Collaboration Community Events
Personal Development
Training Development Certification
Career Development Networking Recognition
Mentoring Access to worldwide community of peers and mentors
Decide
3 2009 HP Confidential
Enroll
Participate
Grow
For more information, please visit http://intranet.hp.com/TSG/WW2/professions
Objectives And Key Takeaways
Session Objectives
Overview Learn
of PowerShell
PowerShell 101
Key Takeaways
Learn
Learn Learn PowerShell!
Non Goals
Full
training on PowerShell
2009 HP Confidential
Old World and New World VBScript
Set listExchange_Mailboxs = GetObject("winmgmts:{impersonationLevel=impersonate}!\\COMPUTERNAME\ROOT\MicrosoftExchangeV2").InstancesOf("Exchange_Mail box") 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 WScript.echo " ServerName WScript.echo " Size WScript.echo " StorageGroupName WScript.echo " StorageLimitInfo WScript.echo " StoreName WScript.echo " TotalItems Next = + objExchange_Mailbox. MailboxGUID = + objExchange_Mailbox. ServerName = + objExchange_Mailbox. Size = + objExchange_Mailbox. StorageGroupName = + objExchange_Mailbox. StorageLimitInfo = + objExchange_Mailbox. StoreName = + objExchange_Mailbox. TotalItems
PowerShell
5
get-mailboxstatistics server $servername
2009 HP Confidential
Windows PowerShell
Next generation of interactive shell and scripting environment Foundation to build GUI management console
Equality between command-line tasks and GUI tasks
Extensible
PowerShell in Windows Server 2008 R2 components: Cluster- Server Manager PowerShell in Exchange 2007 SQL 2008 SCOM 2008 SCVMM 2008
2009 HP Confidential
Duality in PowerShell
For Administrators For Developers
Object-based Shell
.NET Framework
Interactive Shell
7 2009 HP Confidential
Scripting Language
PowerShell V1
Released to the Web
Over 3.5 million downloads in less than 30 months
Windows XP Windows Vista Windows Server 2003 Features in Windows Server 2008
2009 HP Confidential
PowerShell V2
Shipped in all Windows Server 2008 R2 Editions
Shipped in all Windows 7 Editions
Installed by default
Included in the Windows Management Framework
Released For
to the WEB ( RTW) http://support.microsoft.com/kb/968929 Windows XP / Windows Server 2003 and above
2009 HP Confidential
PowerShell V1 and V2
V1
129 PowerrShell cmdlets COM WMI ADSI ADO XML .NET Native commands and text processing
10 2009 HP Confidential
V2
244 PowerShell cmdlets Improved ADSI, WMI, COM, XML, ADO, text processing Native .Net Languages
C#,
VB.NET, F# Pinvoke to Win32
WS-MGMT Web Services Cmdlets for Windows components
New Cmdlets in V2
Remoting:
Invoke-Command Enter-PSSession Exit-PSSession Export-PSSession Get-PSSession Import-PSSession New-PSSession Remove-PSSession
Transactions:
Complete-Transaction Get-Transaction Start-Transaction Undo-Transaction Use-Transaction
WMI:
Invoke-WMIMethod Register-WMIEvent Remove-WMIObject Set-WMIInstance
Debugging:
Disable-PSBreakpoint Enable-PSBreakpoint Get-PSBreakpoint Get-PSCallStack Remove-PSBreakpoint Set-PSBreakpoint
More Cmdlets:
Clear-History Get-Random Out-GridView Set-StrictMode Update-List Debug-Process Start-Process Wait-Process
Converting Types:
Add-Type ConvertFrom-Csv
ConvertTo-Csv
ConvertFrom-StringData ConvertTo-Xml
Eventing:
Get-EventSubscriber Get-Event New-Event Register-EngineEvent Register-ObjectEvent Remove-Event Unregister-Event Wait-Event
Computer:
Event Viewer and ETW Logs:
Get-WinEvent
Script Internationalization:
Import-LocalizedData
Modules:
Get-Module Import-Module New-Module
11 2009 HP Confidential Remove-Module
Background Jobs:
Get-Job Receive-Job Remove-Job Start-Job Stop-Job Wait-Job
Add-Computer Checkpoint-Computer Remove-Computer Restart-Computer Restore-Computer Stop-Computer Disable-ComputerRestore Enable-ComputerRestore Get-ComputerRestorePoint Reset-ComputerMachinePassword Test-ComputerSecureChannel
Export-ModuleMember
Windows Components / Server Applications with PowerShell
Exchange Server Windows Diagnostics Server Manager SQL Server Active Directory Failover Clustering SC Operations Manager Windows Backup Server Migration SC Virtual Machine Manager Best Practices Analyzer AD Rights Management Services Essential Business Server Internet Information Services Background Intelligent Transfer Service SC Data Protection Manager Network Load Balancing Remote Desktop Services SharePoint 2010 Group Policy AppLocker High Performance Computing WS-Management WMI Message Queuing
12 2009 HP Confidential
Books on PowerShell
Now available in
Japanese German French More
13
2009 HP Confidential
2007
2008
Books on PowerShell
Recommended books for beginners
Authorative book for advanced users
14
2009 HP Confidential
Resources to Learn PowerShell
Windows PowerShell is Powerful Automation!
Powershell V2 for Developers Windows PowerShell is Powerful Automation! Variables, Types, and Operators Arrays Conditionals, Collections, and Loops Create Object Cmdlets Remote Sessions Advanced Remote Sessions
HP Knowledge Briefs ~91 articles!
Code and Script repository
List of PowerShell resources
15 2009 HP Confidential
Learning the Basics
16
2009 HP Confidential
PowerShell Fundamentals
Use <TAB> for expansion Pipelining Alias Four (4) commands to learn PowerShell!
Help
Get-Command Get-Member Get-PSDrive
Demo
17
2009 HP Confidential
PowerShell Cmdlets Syntax
18
2009 HP Confidential
Outputting - Formatting
Writing to
Write-host Write-file Write-output
Dont know the differences?
1. Try it! 2. Use Help!
Sending to..
Out-NULL Out-File Out-Printer
3. Look at examples in the help command!
Formatting with
Format-table Format-list
Demo
19
2009 HP Confidential
Help write-* , out-*, format-*
Variables
Name
$ + <Name of the variable> $a , $1_Password_for_you
Scope
$local:Myresult $script:ListofElements $global:username
Type
All
variables are objects: .NET objects Best match of the type based on what you have
20 2009 HP Confidential
Help about_Variables
PowerShell $ Variables
Variable Name $_ $^ $$ $? $Args $Error $foreach $HOME $Input $Match $MyInvocation $Host $LastExitCode $true $false $null $OFS $ShellID
21
Description The current pipeline object; used in script blocks, filters, the process clause of functions, where-object, foreach-object and switch contains the first token of the last line input into the shell contains the last token of last line input into the shell Contains the success/fail status of the last statement Used in creating functions that require parameters If an error occurred, the object is saved in the $error PowerShell variable Refers to the enumerator in a foreach loop. The user's home directory; set to %HOMEDRIVE%\%HOMEPATH% Input piped to a function or code block A hash table consisting of items found by the match operator. Information about the currently script or command-line Information about the currently executing host The exit code of the last native application to run Boolean TRUE Boolean FALSE A null object Output Field Separator, used when converting an array to a string. By default, this is set to the space character. The identifier for the shell. This value is used by the shell to determine the ExecutionPolicy and what profiles are run at startup. contains detailed stack trace information about the last error
$StackTrace
2009 HP Confidential
Collections : Arrays Hash Tables
Arrays
$MyLuckyNumbers = 1,25,32,49 $MyRange= 1..100 + -100..-1 $List= @() $List += 2 ; $List +=5
Hash Tables
$Months = @ { Jan= 1; Feb=2 ; Mar=3}
$Months.Jan , $Months.Feb $aHash = @{} $aHash[Expert] = You! $aHash[13] = Prime $aHash[pi] = 3.1415
Help about_Arrays Help about_Hash_Tables
Demo
22
2009 HP Confidential
Operators
Types Arithmetic Assignment Comparison +-*/% = += -= *= /= %= ++ --eq -ceq -ieq -ne -cne -ine -gt -cgt -igt -ge -cge -ige -lt -clt -ilt -le -cle -ile -contains -nocontains -match -nomatch -like -notlike -and -or -xor -not -band -bor -bxor -bnot & = call operator . = Property deference operator . = Dot sourcing operator :: = Static member operator .. = Range operator -f = format operator -split , -match , replace = string operator $( ) = Subexpression operator @( ) = Array subexpression operator
Special operators
23
2009 HP Confidential
Help about_Operators
Flow control
If / Else
While loop
Do / while loop For loop Foreach loop Switch
24
2009 HP Confidential
Help about_if, about_for, about_foreach, about_switch
Filtering
Where-object
Select-Object
Demo
25
2009 HP Confidential
Help where, help Select
Alias
Name Dir , ls Cd , md , rd Cmdlet Get-childitem Set-Location, New-Item, Remove-Item
Ft fl
Where Foreach, % Gal .
Format-Table , Format-List
Where-object Foreach-object Get-alias
26
2009 HP Confidential
Help about_alias
Functions & Scripts
Scripts
Text
file with extension .ps1
Script Block
{ get-process ; $i+=32} $Args variable
Built-in
variable Auto populate with objects containing values passed to the script/function
Param Statement
To
declare formal parameters
Param ([string]$server=localhost, [int]Max=10)
27
2009 HP Confidential
Help about_functions, about_scripts
Advanced Techniques in PowerShell
28
2009 HP Confidential
Get-Member Your Best Friend!
PowerShell emits objects Get-Member lists Properties and Methods of objects Useful for exploring other properties of objects
Get-Member .vs. Format-Table
Demo
29
2009 HP Confidential
Help Get-Member
PSDrive
Exposes system data as file system drive Administrator navigates through system data using standard file system commands : cd ls dirmkdir.
PSDrive Alias: Function: Cert: Env: Usage List of PowerShell aliases All available functions for the current session Access to the certificates store List of system/user environment variables
Variable:
HKCU: HKLM:
List of declared variables
Access to registry
Demo
30
2009 HP Confidential
Help *PSDRIVE*
Working with Strings
String Properties / Methods Import-CSV ConvertTo-CSV Select-String Here-String or @
String expansion
Numeric format strings {0:D3} f 2
Demo
31
2009 HP Confidential
Help *String* - help * csv*
Working with Files
Listing Files Reading from / Writing to files Search text in files select-string
Demo
32
2009 HP Confidential
Help *content*
Working with Date/Time
DateTime object Fomatting {0:F}-f [DateTime]01/12/1975 Measure time TimeSpan object
Demo
33
2009 HP Confidential
Working with Events - ACL
Get-WinEvent Get-EventLog Get-ACL / Set-ACL
Demo
34
2009 HP Confidential
Summary
Windows PowerShell is both an interactive shell and a scripting language Most Windows components and server applications ship with their own PowerShell Converged Infrastructure cmdlets Windows PowerShell enables automation of admin processes and reduces human error in Virtualized operations Resilient Orchestrated Optimized Modular
35
2009 HP Confidential
More Resources.
TechNet Script Center PowerShell Code Repository
MS Instructor-Led PowerShell training
HP Forums: PowerShell MS Professions Webinars!
36 2009 HP Confidential
Next PowerShell Webinars
Tuesday, February 02, 2010 PowerShell Fundamentals Part I Dung K Hoang Thursday, February 04, 2010 PowerShell Fundamentals Part II Dung K Hoang
Tuesday, February 09, 2010 PowerShell - Active Directory Guido Grillenmeier
Thursday, February 11, 2010 PowerShell - SCVMM - Guido Grillenmeier
Tuesday, February 16, 2010
PowerShell Clustering Aric Bernard
Thursday, February 18, 2010
PowerShell - Exchange David Standish/Joel Ricketts/ Pete Petree
Tuesday, February 23, 2010
Thursday, February 25, 2010
PowerShell - Exchange Web Service Kevin Laahs
PowerShell - SCOM - Tommy Carron
Tuesday, March 02, 2010 PowerShell - MOSS 2007/2010 - Veli-Matti Vanamo /Brian Carter
Thursday, March 04, 2010 PowerShell - VMware/Hyper-V Steve Fortuna/Ken J Bell
Tuesday, March 09, 2010 POwerShell - Citrix Workflow Albert Hols
Thursday, March 11, 2010 Advanced PowerShell Dung K Hoang
Tuesday, March 16, 2010
37 2009 HP Confidential
Thursday, March 18, 2010 PowerShell and Office apps David Lee
PowerShell - WMI SCCM David Lee