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

PowerShell crashcourse

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

Editor's Notes

  • #2 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.