PowerShell Core Skills (TechMentor Fall 2011)

1,000
-1

Published on

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

  • Be the first to like this

No Downloads
Views
Total Views
1,000
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
49
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide
  • TechMentor Las Vegas 2011
  • TechMentor Las Vegas 2011
  • PowerShell Core Skills (TechMentor Fall 2011)

    1. 1. Windows PowerShell Core Skills Tutorial Don Jones ConcentratedTech.com Learn Windows PowerShell in 2 Days / Part 1
    2. 2. Welcome! <ul><li>This is one of four sessions designed to teach specific Windows PowerShell skills </li></ul><ul><li>Don Jones </li></ul><ul><ul><li>Windows PowerShell MVP Award Recipient </li></ul></ul><ul><ul><li>PowerShell Columnist for Microsoft TechNet Magazine </li></ul></ul><ul><ul><li>“ Decision Maker” Columnist for Redmond Magazine </li></ul></ul><ul><ul><li>Author, Learn Windows PowerShell in a Month of Lunches </li></ul></ul><ul><ul><li>Co-Author, Windows PowerShell v2.0: TFM </li></ul></ul><ul><ul><li>Creator of numerous self-paced PowerShell training videos for CBTNuggets.com. </li></ul></ul>
    3. 3. Caution: Demos Ahead! <ul><li>Please note: This session is built primarily around demonstrations, and answering your questions. You won’t see many slides. </li></ul><ul><li>If you’re looking for additional written resources, there are lots to choose from (including many free ones) – the closing slide will list some key URLs. </li></ul><ul><li>We’ll be using slides mainly to form the agenda and for some conceptual stuff. </li></ul>
    4. 4. CAUTION: DEPLOY AIRBAGS! <ul><li>This is very much a “crash course.” </li></ul><ul><li>We’re going to cover a lot, and we’re gonna do it quickly. </li></ul><ul><li>Feel free to ask questions… but understand the value in “just being exposed to it.” </li></ul><ul><li>You’ll really get the best comprehension when you start using these techniques in your job. So plan to do that when you go home! </li></ul>
    5. 5. Bonus <ul><li>I’ll post any scripts, as well as these slides. </li></ul><ul><li>Download location will be listed at the end of this session. </li></ul><ul><li>Don’t bother copying down commands – you’ll be able to download the whole session! </li></ul>
    6. 6. A Plug <ul><li>Much of what we’re covering is based on Learn Windows PowerShell in a Month of Lunches </li></ul><ul><li>There’s also a companion DVD that provides 99 video demos that correspond to the book </li></ul><ul><li>I have copies with me if you’d like to take a look at them… or maybe acquire one…  </li></ul><ul><li>Each physical copy includes a free ebook edition! </li></ul>
    7. 7. What is PowerShell? <ul><li>It isn’t a scripting language. It’s a command-line shell… </li></ul><ul><li>… and like many shells, it contains a scripting language. </li></ul><ul><li>For right now, we’re going to focus on using it as a shell. </li></ul><ul><li>It works a lot like the shell (Cmd.exe) you’re hopefully used to. Let’s see. </li></ul>
    8. 8. Things We Need to Cover <ul><li>System requirements (and the future) </li></ul><ul><li>Cmdlets (and their names) </li></ul><ul><li>Parameters </li></ul><ul><li>Aliases </li></ul><ul><li>PSProviders and PSDrives </li></ul><ul><li>The Help System (and why not to use Get-Help) </li></ul><ul><li>Get-Command </li></ul>
    9. 9. Parameter Confusion <ul><li>Parameter names can be truncated , so long as you type enough for the shell to uniquely identify the parameter. </li></ul><ul><li>Some parameters are positional, which means you can omit the parameter name if the value goes into the right spot. </li></ul><ul><li>Get in the habit of using tab completion in the shell. I’ll show you. </li></ul><ul><li>Also stick with always typing parameter names for now – it’s less confusing. When you use the parameter names, order doesn’t matter. </li></ul>
    10. 10. Connecting Commands <ul><li>Ever run Dir | More ? </li></ul><ul><li>If so, you know what the pipe does: It takes the output of one command and sends it to the input of the next command </li></ul><ul><li>PowerShell can do the same thing, but it’s much more powerful (and a bit more complicated) </li></ul><ul><li>Let’s see a couple of brief examples </li></ul>
    11. 11. Things to Cover <ul><li>-WhatIf and –Confirm </li></ul><ul><li>Piping and Parameters (Out-File) </li></ul><ul><li>The other Out- cmdlets </li></ul><ul><li>Export cmdlets </li></ul><ul><li>ConvertTo cmdlets </li></ul>
    12. 12. But Wait, There Must be More <ul><li>Run Get-Service… that can’t possibly be all PowerShell knows about services </li></ul><ul><li>Let’s talk about “tables of data.” </li></ul><ul><li>And also the Get-Member cmdlet. </li></ul><ul><li>And also some proper terminology: </li></ul><ul><ul><li>Table = Collection of objects </li></ul></ul><ul><ul><li>Row = Object </li></ul></ul><ul><ul><li>Column = Property </li></ul></ul><ul><ul><li>Properties + Methods = Members of an Object </li></ul></ul>
    13. 13. Some Core Commands <ul><li>Sorting (Sort or Sort-Object) </li></ul><ul><li>Filtering (Where or Where-Object) </li></ul><ul><li>Picking specific properties (Select or Select-Object) </li></ul><ul><li>This is a good time for some audience participation. </li></ul>
    14. 14. Let’s Play with Formatting <ul><li>PowerShell has pre-defined “views” for many types of objects – it will use those if you don’t specify something different </li></ul><ul><li>The Format- cmdlets produce a special kind of object designed for output formatting </li></ul><ul><li>Only Out-Host, Out-Printer, and Out-File can really make sense of those special objects </li></ul><ul><li>Moral: If you use a Format- cmdlet, it needs to be the last thing on the command-line . </li></ul><ul><li>Let’s see that, and a common “gotcha.” </li></ul>
    15. 15. Adding Commands <ul><li>Like the MMC, PowerShell’s main use comes when you add stuff to it. </li></ul><ul><li>There’s a 3-step process for adding commands, finding what you added, and learning how to use them. </li></ul><ul><li>Those 3 steps apply to the two mechanisms used to extend the shell. </li></ul>
    16. 16. Extending PowerShell <ul><li>PSSnapins (v1+) </li></ul><ul><li>Get-PSSnapin –registered </li></ul><ul><li>Add-PSSnapin name </li></ul><ul><li>Get-Command –pssnapin name </li></ul><ul><li>Modules (v2+) </li></ul><ul><li>Get-Module -ListAvailable </li></ul><ul><li>Import-Module name </li></ul><ul><li>Get-Command -module name </li></ul><ul><li>Note: It won’t list modules not installed in the correct path, but you can import using a path instead of just a name </li></ul>
    17. 17. Let’s Dig into the Pipeline More <ul><li>When you pipe something to a command, there’s no magical way for that command to accept that piped-in input </li></ul><ul><li>All command input must be through a parameter </li></ul><ul><li>So when you pipe something, PowerShell has to figure out what parameter to attach it to </li></ul>
    18. 18. Pipeline Input Plan A: ByValue <ul><li>Look at the TypeName of the object in the pipeline </li></ul><ul><li>Does the receiving command have a parameter that accepts that type of data from the pipeline using the ByValue technique? </li></ul><ul><li>If so, then that parameter gets the piped-in input </li></ul>
    19. 19. Plan B: ByPropertyName <ul><li>Only used when Plan A (ByValue) doesn’t work out </li></ul><ul><li>Look at each property of the piped-in object(s) </li></ul><ul><li>See if the receiving command has parameters that accept pipeline input ByPropertyName </li></ul><ul><li>For each receiving parameter whose name matches a property name of the input object, connect the matches </li></ul>
    20. 20. ByPropertyName <ul><li>Get-Service </li></ul><ul><li>Name </li></ul><ul><li>Status </li></ul><ul><li>DisplayName </li></ul><ul><li>(etc) </li></ul><ul><li>Stop-Process </li></ul><ul><li>Name* </li></ul><ul><li>ID* </li></ul><ul><li>InputObject </li></ul><ul><li>(etc) </li></ul><ul><li>*Accepts ByPropertyName </li></ul>MATCH!
    21. 21. Remember… <ul><li>The help files can help you to figure out what’s possible </li></ul><ul><li>Generally, commands with the same noun will connect “properly” </li></ul><ul><li>But you can be creative… let’s see an example using AD </li></ul>
    22. 22. Real World? <ul><li>Where is that CSV file likely to come from? </li></ul>
    23. 23. Fix it in the Pipeline <ul><li>Get-Whatever | Select-Object @{ name='wanted-property-name'; expression={$_.original-property-name} } | Do-Whatever </li></ul>
    24. 24. Or… <ul><li>Get-Whatever | Select-Object @{ n ='wanted-property-name'; e ={$_.original-property-name} } | Do-Whatever </li></ul>
    25. 25. Or… <ul><li>Get-Whatever | Select-Object @{ label ='wanted-property-name'; expression ={$_.original-property-name} } | Do-Whatever </li></ul>
    26. 26. Or… <ul><li>Get-Whatever | Select-Object @{ l ='wanted-property-name'; e ={$_.original-property-name} } | Do-Whatever </li></ul>
    27. 27. And Hey! <ul><li>You can use that same structure with Format-List and Format-Table </li></ul><ul><li>With Format-Table you can use these keys… </li></ul><ul><ul><li>Expression (e) </li></ul></ul><ul><ul><li>Label or Name (l or n) </li></ul></ul><ul><ul><li>FormatString (N0, etc) </li></ul></ul><ul><ul><li>Align (left/right) </li></ul></ul><ul><li>But remember: Format- goes last. So if you need to modify and then use an object, do it with Select-Object not Format-. </li></ul>
    28. 28. Comparisons and Filtering <ul><li>-eq -ne </li></ul><ul><li>-lt -le </li></ul><ul><li>-gt -ge </li></ul><ul><li>-not -and -or </li></ul><ul><li>(parentheses) group expressions </li></ul><ul><li>Within the –FilterScript of Where-Object, use $_ to represent “whatever was piped in” </li></ul>
    29. 29. WMI <ul><li>Egh. </li></ul><ul><li>Get-WmiObject if your friend. </li></ul><ul><li>A decent “WMI Explorer” tool is even more your friend. </li></ul><ul><li>But let’s play with Get-WmiObject </li></ul>
    30. 30. Things to Cover… <ul><li>Listing classes </li></ul><ul><li>Finding namespaces </li></ul><ul><li>Examining classes with Get-Member </li></ul><ul><li>Remote computers (and the protocol) </li></ul><ul><li>Alternate credentials and authentication and a trick for credential management </li></ul>
    31. 31. Working with Bunches <ul><li>A “Batch” cmdlet (like Stop-Service or Restart-Computer) </li></ul><ul><li>A WMI method that works with Invoke-WmiMethod (like Win32Shutdown() of Win32_OperatingSystem) </li></ul><ul><li>ForEach-Object (a bit yucky – Change() of Win32_Service) </li></ul>
    32. 32. PowerShell Security <ul><li>Must type a path </li></ul><ul><li>.PS1 filename association </li></ul><ul><li>Execution Policy </li></ul><ul><ul><li>Unrestricted (dumb) </li></ul></ul><ul><ul><li>RemoteSigned (less dumb) </li></ul></ul><ul><ul><li>AllSigned (smarter) </li></ul></ul><ul><ul><li>Restricted (default) </li></ul></ul><ul><ul><li>Bypass (specialized) </li></ul></ul><ul><li>Also: PowerShell.exe switches, GPOs, etc. </li></ul>
    33. 33. Input and Output <ul><li>Write-Verbose </li></ul><ul><li>Write-Debug </li></ul><ul><li>Write-Output </li></ul><ul><li>Read-Host </li></ul><ul><li>Write-Host </li></ul>
    34. 34. An Intro to Variables <ul><li>$ is a shell cue to access the contents of a variable </li></ul><ul><li>$var accesses the contents of var – but the variable name is var, not $var </li></ul><ul><li>${this is a legal variable name, sadly} </li></ul><ul><li>The VARIABLE: drive and the –Variable cmdlets </li></ul><ul><li>[typing]$variables </li></ul><ul><li>'Tricks' with &quot;Quotes&quot; and `Escapes </li></ul>
    35. 35. A Simple, Parameterized Script <ul><li>Reference: [CmdletBinding()] param( [string]$whatever = 'default' ) </li></ul>
    36. 36. Well, That Was Just a Lot. <ul><li>The story continues in the other three sessions in this series… won’t you join me tomorrow? </li></ul>
    37. 37. Thank You! <ul><li>Please feel free to hit me up with any remaining questions between sessions </li></ul><ul><li>Please submit a session evaluation! These are an extremely important part of ensuring that the conference continues to provide you with the education you need! </li></ul><ul><li>Resources and URLs on the next slide… </li></ul>
    38. 38. Remember! <ul><li>Slides, and scripts will be posted within a few days. </li></ul><ul><li>You’re welcome to download whatever you like; please don’t re-post anything elsewhere on the Internet. </li></ul><ul><li>For the download URL, see my Twitter feed: </li></ul><ul><ul><li>@concentrateddon </li></ul></ul><ul><ul><li>Twitter.com/concentrateddon </li></ul></ul>
    39. 39. More PowerShell Resources <ul><li>Web Sites </li></ul><ul><li>ShellHub.com </li></ul><ul><li>Bit.ly/DonJones </li></ul><ul><li>YouTube.com/ ConcentratedDon </li></ul><ul><li>ITPro. ConcentratedTech.com </li></ul><ul><li>Thank you again for attending! </li></ul><ul><li>Available Here! </li></ul>
    1. A particular slide catching your eye?

      Clipping is a handy way to collect important slides you want to go back to later.

    ×