Introduction to powershell
- 2. AgendaWhat is PowerShell ?Problems with existing scripting language (VB script)How PowerShell solves the security issuesBasic commands in PowershellGUI (IDE) for PowershellHow to get help in PowerShellAliasSnap-insCmd-lets in PowerShellVariablesUnderstanding the pipe lineOperators in PowerShellLogical OperatorsSorting, Measuring, Select, Filter and compareExport, Import, ConvertFunctions Regular expressionsArrays and Hash TableXML handling
- 3. What is PowerShell?NEW scripting platform for Microsoft productsOne scripting language – Multiple productsWindows Desktop OS, Server OSSharePointSQL ServerSCOM/SCDPM/SVCMMExchange ServerVMWARE/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. Restricted - No scripting allowed unrestricted - You can any scriptingno signing requiredRemote signed – good for test, dev environmentsonly files from internet need to be signeddefault settingAll signed - local, remote script, it should be signed.user must agree to run scriptPowerShell’s Execution Policy
- 5. No common scripting for all the products.Net codeCOM ModelExeVBScriptScripts are really security concern, because they do have lot of powerEcho “Welcome”Del *.* ???Top Concerns:IntegrityIdentityDouble click RunCommand HijackingPowerShell addresses this issue by introducing Executing PolicyProblems with Existing scripting languages
- 6. “built-in” commands for PowerShell“verb-noun” nameseg. get-childitem (= ls)but: new-alias, new-objectextensible set: can write own cmdletsHeart and Soul of PowerShellEngine that make powershell work. They are the small units of functionality that perform the operations.Cmdlets
- 7. Basic TourShell promptHelp systemGetting help:Get-helpGet-help –verb getGet-help –noun fileGet-help stop-process –examplesGet-help stop-process –fullGet-help “sp*Out-filePs>file1.txt ps>>file2.txtPs |out-file process.txtGet-content
- 8. Snap-InPowershell snap-in provides a mechanism for registering sets of cmdletsExample: similar to MMC Set of cmd-lets for a specific product Eg. SharePointGet-pssnapinLists the core functionalityGet-Pssnapin – registeredShows the installed cmd-LetsTo Add a new PS Snapin:Add-Snapin <snap-in-Name>
- 9. Basic cmd-lets for process, servicesAsk Help: help *process*Get-process > Alias psEg. Get-process –name calcHow to get the –name parameter? Get-process | get-memberStop-process -> Alias KillStop-process –name calc Stop-process –name calc –whatifServicesGet-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-eventlogEg. get-eventlog system –newest 10Get-eventlog | -newest 10 format-listIDEPowerGUI - Open source yet powerfull, FREEPowershell + Primal script ISE – PowerShell 2.0
- 11. Variables Powershell assigns best suited data type for variables when assignedNew-variable -name var –value 10Or$var=10Remove-variable –name varIt supports Int, Datetime, Bool, string, Char, byte, decimal, array, xmlVariables are actually .net objects$test=“honeywell”Can say $test.toUpper()User get-member to retrieve all the member of the objectCan force the data type by [string]$var=5$var.gettype().fullname
- 12. PipelinesCommands manipulates and passes objects from One to anotherEg: Get the list of process -> filter > stop ->formatGet-process | where-object {$_.status –eq “Stopped”} |format-listGet-process | out-file C:rocess.txtGet-process | out-Printer <Name of the printer>Write-output vs write-hostFirst one sends output to the pipeline, Second doesn’tWrite-output “Hello” |where-object {$_.length – gt 2}We have some additional options like –foregroundcolorDCDA
- 13. OperatorsAll Basic math operations: +, -, *, /, %5+5; 10-1; 8*2; 10%3; 5+(5*5)ComparisonEQ10 –eq 5LT, -GT, -GE, -LEString comparison: not case sensitive“Hello” – eq “HELLO” > trueForcing case sensitive:“Hello” – ceq “HELLO” > trueLogical operators AND OR NOT
- 14. Sort – Measure –Select - filterSortGet-process | sort-object VM –descGet-service |sort statusMeasureGet-service |measure-objectGet-service |measure-object –property VM –sum –min-max –averageSelectGet-service | select-object displayname,statusGet-process | select-object –first 10
- 19. Script BlockExecutes the block of code from file, variable$b={write-host “Hello”}$b >>write-host “hello”To Execute : &$bFunctions:Function sayHello(){ write-host “Hello”}sayHello
- 20. Functions Cont.Function sayHello($SenderName){ write-host “Hello” + $senderName}sayHello “Honeywell”SayHello “honeywell” -> write-host “Hello” $args[0]Return statement: function determine{ if($var – gt 10){return $true}Else{return $false}}
- 21. Regular expressionStandard for Pattern matchingUse –MatchEg. “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.lengthSay “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. ResourcesDownload powershell through Microsoft.comVideoshttp://channel9.msdn.com/Media/?TagID=163Blogshttp://blogs.msdn.com/powershellhttp://thepowershellguy.comhttp://keithhill.spaces.live.comhttp://www.leeholmes.com/blogPowerShell Installation Instructions: http://shrinkster.com/rpyPowerTab by MoW - http://shrinkster.com/rpx“MSH Logo” by Lee Holmes - http://shrinkster.com/rpwPowerShell Community Extensionshttp://www.codeplex.com/PowerShellCXMSDN - http://shrinkster.com/rpuHow to create a cmdlet: http://shrinkster.com/rpvBlogsPowerShell 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 Newsgroupmicrosoft.public.windows.powershell