1. Introduction to PowerShellLearn it before it become URGENT Salaudeen.Rajack@Gmail.com Blog: http://www.Salaudeen.Blogspot.com
2. Agenda What is PowerShell ? Problems with existing scripting language (VB script) How PowerShell solves the security issues Basic commands in Powershell GUI (IDE) for Powershell How to get help in PowerShell Alias Snap-ins Cmd-lets in PowerShell Variables Understanding the pipe line Operators in PowerShell Logical Operators Sorting, Measuring, Select, Filter and compare Export, Import, Convert Functions Regular expressions Arrays and Hash Table XML handling
3. What is PowerShell? NEW scripting platform for Microsoft products One scripting language – Multiple products Windows Desktop OS, Server OS SharePoint SQL Server SCOM/SCDPM/SVCMM Exchange Server VMWARE/Citrix Runs on top of .net framework, 2.0+ Automate almost every thing you can do with GUI (some times, things which are not possible with GUI) Not just command prompt or Script language, But Command-Shell. It’s the Microsoft Way… Shell prompt, just like DOS shell, But more powerful
4.
5. No common scripting for all the products .Net code COM Model Exe VBScript Scripts are really security concern, because they do have lot of power Echo “Welcome” Del *.* ??? Top Concerns: Integrity Identity Double click Run Command Hijacking PowerShell addresses this issue by introducing Executing Policy Problems with Existing scripting languages
6. “built-in” commands for PowerShell “verb-noun” names eg. get-childitem (= ls) but: new-alias, new-object extensible set: can write own cmdlets Heart and Soul of PowerShell Engine that make powershell work. They are the small units of functionality that perform the operations. Cmdlets
7. Basic Tour Shell prompt Help system Getting help: Get-help Get-help –verb get Get-help –noun file Get-help stop-process –examples Get-help stop-process –full Get-help “sp* Out-file Ps>file1.txt ps>>file2.txt Ps |out-file process.txt Get-content
8. Snap-In Powershell snap-in provides a mechanism for registering sets of cmdlets Example: similar to MMC Set of cmd-lets for a specific product Eg. SharePoint Get-pssnapin Lists the core functionality Get-Pssnapin – registered Shows the installed cmd-Lets To Add a new PS Snapin: Add-Snapin <snap-in-Name>
9. Basic cmd-lets for process, services Ask Help: help *process* Get-process > Alias ps Eg. Get-process –name calc How to get the –name parameter? Get-process | get-member Stop-process -> Alias Kill Stop-process –name calc Stop-process –name calc –whatif Services Get-service <service name> Restart-service <service name> Stop-service <service name>
10. Basic cmd-lets for process, services (Cont.) Get-service –include “Sharepoint*” Get-service –exclude “Sharepoint*” Event log: Get-eventlog Eg. get-eventlog system –newest 10 Get-eventlog | -newest 10 format-list IDE PowerGUI - Open source yet powerfull, FREE Powershell + Primal script ISE – PowerShell 2.0
11. Variables Powershell assigns best suited data type for variables when assigned New-variable -name var –value 10 Or $var=10 Remove-variable –name var It supports Int, Datetime, Bool, string, Char, byte, decimal, array, xml Variables are actually .net objects $test=“honeywell” Can say $test.toUpper() User get-member to retrieve all the member of the object Can force the data type by [string]$var=5 $var.gettype().fullname
12. Pipelines Commands manipulates and passes objects from One to another Eg: Get the list of process -> filter > stop ->format Get-process | where-object {$_.status –eq “Stopped”} |format-list Get-process | out-file C:rocess.txt Get-process | out-Printer <Name of the printer> Write-output vs write-host First one sends output to the pipeline, Second doesn’t Write-output “Hello” |where-object {$_.length – gt 2} We have some additional options like –foregroundcolor D C D A
13. Operators All Basic math operations: +, -, *, /, % 5+5; 10-1; 8*2; 10%3; 5+(5*5) Comparison EQ 10 –eq 5 LT, -GT, -GE, -LE String comparison: not case sensitive “Hello” – eq “HELLO” > true Forcing case sensitive: “Hello” – ceq “HELLO” > true Logical operators AND OR NOT
15. Export-Import and compare Export-CSV Get-process |export-csv Export-CSV $process=import-csv c:a.csv Compare: $p1=get-process Now open a new process, say calc $p2=get-process Compare-object $p1, $p2 –property name
16. Logical constructs IF, Switch, For, While IF, Switch – Decision For, while – looping Eg IF($var –gt 100) { write-host “yes”} Elseif() { } Else { }
19. Script Block Executes the block of code from file, variable $b={write-host “Hello”} $b >>write-host “hello” To Execute : &$b Functions: Function sayHello() { write-host “Hello” } sayHello
20.
21. Regular expression Standard for Pattern matching Use –Match Eg. “Honeywell” –match “Honey” . (dot) – one char * - Zero or more match “A” match “t*” + - one or more match “TTT” match “^T+” ? – Zero or one match [AB] – either A or B ^ - start $ - end eg. “Sala” –match “^s..A$” – any word character -W –Non word – space -S -D (n,m) eg. “TTTT” –match “^T{4, 6}”
22. Strings, Arrays, Hash tables $H=“Honeywell” $h.length Say “hello” >> “Say “”hello””” Array: $arr=1,2,3 or $arr=@(1,2,3) $arr2=@((1,1),(2,2),(3,3)) Get : $arr2[1][1] Hash table: $Hash=@{No=1;”CName“=“Honeywell”} $hash.no $hash[“Cname”]
24. Resources Download powershell through Microsoft.com Videos http://channel9.msdn.com/Media/?TagID=163 Blogs http://blogs.msdn.com/powershell http://thepowershellguy.com http://keithhill.spaces.live.com http://www.leeholmes.com/blog PowerShell Installation Instructions: http://shrinkster.com/rpy PowerTab by MoW - http://shrinkster.com/rpx “MSH Logo” by Lee Holmes - http://shrinkster.com/rpw PowerShell Community Extensions http://www.codeplex.com/PowerShellCX MSDN - http://shrinkster.com/rpu How to create a cmdlet: http://shrinkster.com/rpv Blogs PowerShell Team Blog - http://blogs.msdn.com/powershell/ Lee Holmes - http://www.leeholmes.com/blog/ David Aiken - http://blogs.msdn.com/daiken/ The PowerShell Guy (MoW) - http://thepowershellguy.com/ Popular Newsgroup microsoft.public.windows.powershell