PowerShell crashcourse

1,391 views

Published on

Published in: Technology
0 Comments
1 Like
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
1,391
On SlideShare
0
From Embeds
0
Number of Embeds
1
Actions
Shares
0
Downloads
66
Comments
0
Likes
1
Embeds 0
No embeds

No notes for slide
  • MGB 2003 © 2003 Microsoft Corporation. All rights reserved. This presentation is for informational purposes only. Microsoft makes no warranties, express or implied, in this summary.
  • PowerShell crashcourse

    1. 1. Windows PowerShell Crash Course Don Jones Senior Partner & Principal Technologist Concentrated Technology, LLC
    2. 2. This slide deck was used in one of our many conference presentations. We hope you enjoy it, and invite you to use it within your own organization however you like. For more information on our company, including information on private classes and upcoming conference appearances, please visit our Web site, www.ConcentratedTech.com . For links to newly-posted decks, follow us on Twitter: @concentrateddon or @concentratdgreg This work is copyright ©Concentrated Technology, LLC
    3. 3. CAUTION: DEMOS AHEAD <ul><li>This is a demonstration-intensive session (very few slides) </li></ul><ul><li>I will capture a shell transcript and save all of my scripts </li></ul><ul><li>You can download these (in a week or so) from ConcentratedTech.com (there ’s a “Conference Materials” link in the menu) </li></ul> • Don Jones • ConcentratedTech.com
    4. 4. Welcome to the Shell <ul><li>Why did Microsoft write PowerShell? </li></ul><ul><li>Is this a replacement for Cmd.exe or VBScript? </li></ul><ul><li>How much “scripting” do I need to learn? </li></ul><ul><li>How long will PowerShell be around? </li></ul><ul><li>What versions of Windows does it work with? </li></ul><ul><li>What MS products are PowerShell-ed? </li></ul> • Don Jones • ConcentratedTech.com
    5. 5. Command Quiz <ul><li>Change directories </li></ul><ul><li>Make a directory </li></ul><ul><li>Get a list of files and folders </li></ul><ul><li>Display contents of a text file </li></ul><ul><li>Move a file </li></ul><ul><li>Copy a file </li></ul><ul><li>Delete a file </li></ul> • Don Jones • ConcentratedTech.com
    6. 6. Running Commands <ul><li>Run all the commands you ’re used to, with almost exactly the same syntax (Ping, Ipconfig, etc) </li></ul><ul><li>New “cmdlets” have a more consistent naming convention and more consistent parameters… but they’re just commands </li></ul><ul><li>Use Help to read more; add –full or –examples for even more help; use wildcards to discover more commands and help topics </li></ul> • Don Jones • ConcentratedTech.com
    7. 7. One Set of Commands <ul><li>The file system is a hierarchical data store </li></ul><ul><li>What other data stores does Windows use? </li></ul><ul><li>Why not treat them as “disk drives” so that you can use the same set of commands? </li></ul> • Don Jones • ConcentratedTech.com
    8. 8. Extending the Shell <ul><li>PSSnapins (the old way) </li></ul><ul><ul><li>Get-PSSnapin –registered </li></ul></ul><ul><ul><li>Add-PSSnapin name </li></ul></ul><ul><ul><li>Get-Command –pssnapin name </li></ul></ul><ul><li>Modules (the new way) </li></ul><ul><ul><li>Get-Module –listavailable </li></ul></ul><ul><ul><li>Import-Module name </li></ul></ul><ul><ul><li>Get-Command –module name </li></ul></ul><ul><li>There ’s only one shell – the “pre-made shells” are just pre-loading a snap-in or module for you </li></ul> • Don Jones • ConcentratedTech.com
    9. 9. Piping <ul><li>Just like Dir | More </li></ul><ul><li>Export-, Out-, Format-, ConvertTo- are all useful verbs for piping </li></ul><ul><li>Tip: Format- cmdlets go at the end of the pipeline. What they produce is only useful to Out-File, Out-Printer, Out-Host. </li></ul> • Don Jones • ConcentratedTech.com
    10. 10. Output <ul><li>Run Get-Process </li></ul><ul><li>See the resulting table? </li></ul><ul><li>The real table, in memory, is much bigger – PowerShell just doesn ’t show it </li></ul><ul><li>Pipe the table to Get-Member to see other available columns </li></ul><ul><li>Or pipe it to Format-List * (since a list can hold more data than a table) </li></ul><ul><li>You don ’t have to pipe stuff to a file and then grep it; you can simply refer to columns by name </li></ul> • Don Jones • ConcentratedTech.com
    11. 11. More Piping <ul><li>Sorting: Sort-Object (or “Sort”) </li></ul><ul><li>Filtering: Where-Object (or “Where”) </li></ul><ul><li>Grouping: Group-Object (or “Group”) </li></ul><ul><li>Measuring: Measure-Object (or “Measure”) </li></ul><ul><li>Use Format- cmdlets to specify the columns (called “properties”) that you’d prefer to see </li></ul> • Don Jones • ConcentratedTech.com
    12. 12. Phys Ed <ul><li>Let ’s see the pipeline work in real life. </li></ul><ul><li>Volunteers needed. You may win a prize. </li></ul> • Don Jones • ConcentratedTech.com
    13. 13. How Pipeline input works <ul><li>Get-Service generates why type of table (ok, these are really “objects” and not a “table in memory,” but it’s all just words) </li></ul><ul><li>Get-Service | Get-Member </li></ul><ul><li>So when you run Get-Service | Stop-Service how does Stop-Service know what to do with what you piped in? </li></ul> • Don Jones • ConcentratedTech.com
    14. 14. Pipeline Input ByValue <ul><li>Look at the parameters of Stop-Service </li></ul><ul><li>Do any of them accept pipeline input ByValue and accept the type of data being produced by Stop-Service? </li></ul> • Don Jones • ConcentratedTech.com
    15. 15. Now Consider This <ul><li>“ BITS”,”w32time” | Stop-Service </li></ul><ul><li>Those are strings “BITS” | Get-Member </li></ul><ul><li>Does Stop-Service accept anything of the type String ByValue from the pipeline? </li></ul><ul><li>This would work too: Get-Content names.txt | Stop-Service </li></ul><ul><li>World this work? Get-Process | Stop-Service Hmmm… </li></ul> • Don Jones • ConcentratedTech.com
    16. 16. Pipeline Input ByPropertyName <ul><li>If nothing will work ByValue, then the shell tries to match up input columns with parameter names ByPropertyName </li></ul><ul><li>Let ’s look at Get-Process | Get-Member </li></ul><ul><li>Do any of those column names match up with parameter names of Stop-Service? </li></ul><ul><li>Do the matching parameter names accept input from the pipeline ByPropertyName? </li></ul> • Don Jones • ConcentratedTech.com
    17. 17. Debugging Pipeline Input <ul><li>Trace-Command -Name PipelineBinding -PSHost -expression { Get-Process | Stop-Service } </li></ul> • Don Jones • ConcentratedTech.com
    18. 18. Fun trick! <ul><li>Import user info from a CSV and make new users out of them </li></ul> • Don Jones • ConcentratedTech.com
    19. 19. Workarounds <ul><li>What about when pipelining won ’t work? </li></ul><ul><li>Get-WmiObject -class Win32_BIOS -computername (type names.txt) </li></ul><ul><li>Get-WmiObject -class Win32_BIOS -computername ( Get-ADComputer –filter * | Select –expand Name ) </li></ul> • Don Jones • ConcentratedTech.com
    20. 20. Remote Control <ul><li>Requires PSH v2 </li></ul><ul><li>Run Enable-PSRemoting to enable (or via GPO) on machines that will accept incoming connections </li></ul><ul><li>Communicates over HTTP(s); authenticates with Kerberos </li></ul><ul><li>Help about_remote* for more help </li></ul> • Don Jones • ConcentratedTech.com
    21. 21. 1:1, 1:n Remoting <ul><li>Enter-PSSession –computername x Exit-PSSession </li></ul><ul><li>Invoke-Command -scriptblock { command(s) } -computername x,x,x,x </li></ul><ul><li>Notes: </li></ul><ul><ul><li>PSComputerName property </li></ul></ul><ul><ul><li>FilePath switch to do a whole script </li></ul></ul> • Don Jones • ConcentratedTech.com
    22. 22. More Options <ul><li>-credential </li></ul><ul><li>-port </li></ul><ul><li>-useSSL </li></ul><ul><li>Etc </li></ul><ul><li>Or create a persistent session using New-PSSession. </li></ul><ul><li>Get the sessions with Get-PSSession Invoke-Command –scr { whatever } -session (Get-PSSession) </li></ul> • Don Jones • ConcentratedTech.com
    23. 23. Implicit Remoting <ul><li>Start a session (maybe save it in a $variable for ease of referring to it) </li></ul><ul><li>Import a module in the remote session </li></ul><ul><li>Import the remote session into the local shell – just the commands from that module, and add a noun prefix </li></ul><ul><li>Execute remote commands as if they were local! </li></ul> • Don Jones • ConcentratedTech.com
    24. 24. WMI <ul><li>Get-WmiObject -class whatever (e.g., “Win32_XXX”) -computername x,y,z - EA SilentlyContinue (why?) -Filter ”something” (why?) </li></ul><ul><li>Let ’s play with that… </li></ul> • Don Jones • ConcentratedTech.com
    25. 25. Awesome Advanced Trick <ul><li>Format-Table can accept a special item called a hashtable or dictionary as a property in a property list </li></ul><ul><li>The dictionary must contain two items </li></ul><ul><ul><li>N: The name of the column you want to create </li></ul></ul><ul><ul><li>E: The value to go into the column This can even be an entirely new command! Use $_ to refer to whatever table row (object) is current </li></ul></ul><ul><li>Use to combine WMI info from two classes into a single output! Crazy syntax, but crazy useful! </li></ul> • Don Jones • ConcentratedTech.com
    26. 26. NOW… let ’s do some Q&A <ul><li>I ’ve got more demos to show you, but want to make sure I address your questions </li></ul><ul><li>Ask ‘em now! </li></ul><ul><li>The remainder of the session will be additional demos </li></ul><ul><li>Remember: Transcript will be downloadable; no need to copy down the syntax </li></ul><ul><li>Ask for a “reminder card” if you want to download this stuff next week. </li></ul> • Don Jones • ConcentratedTech.com
    27. 27. Final Notes… <ul><li>Please be sure to submit a session evaluation form! </li></ul><ul><li>Download slides & materials from www.ConcentratedTech.com within one week! </li></ul><ul><li>Blog, URLs, and other information is also available at www.ConcentratedTech.com for your reference </li></ul><ul><li>More resources at www.ShellHub.com </li></ul><ul><li>Thank you very much! </li></ul> • Don Jones • ConcentratedTech.com
    28. 28. This slide deck was used in one of our many conference presentations. We hope you enjoy it, and invite you to use it within your own organization however you like. For more information on our company, including information on private classes and upcoming conference appearances, please visit our Web site, www.ConcentratedTech.com . For links to newly-posted decks, follow us on Twitter: @concentrateddon or @concentratdgreg This work is copyright ©Concentrated Technology, LLC

    ×