Introduction to PowerShell

1,817 views

Published on

Introduction to PowerShell

Published in: Technology
0 Comments
2 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
1,817
On SlideShare
0
From Embeds
0
Number of Embeds
458
Actions
Shares
0
Downloads
82
Comments
0
Likes
2
Embeds 0
No embeds

No notes for slide

Introduction to PowerShell

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

×