• Share
  • Email
  • Embed
  • Like
  • Save
  • Private Content
Why and How Powershell will rule the Command Line - Barcamp LA 4
 

Why and How Powershell will rule the Command Line - Barcamp LA 4

on

  • 5,883 views

Presentation by Ilya Haykinson

Presentation by Ilya Haykinson

Statistics

Views

Total Views
5,883
Views on SlideShare
5,763
Embed Views
120

Actions

Likes
2
Downloads
140
Comments
0

2 Embeds 120

http://blogs.gotdotnet.ru 116
http://www.slideshare.net 4

Accessibility

Categories

Upload Details

Uploaded via as Microsoft PowerPoint

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment

Why and How Powershell will rule the Command Line - Barcamp LA 4 Why and How Powershell will rule the Command Line - Barcamp LA 4 Presentation Transcript

  • Why and How PowerShell will rule the command line ilya haykinson / ilya@hulu.com
  • /Text
  • /Text/Provides
  • /Text/Provides/Context
  • /Text/Provides/Structure
  • /Text/Provides/Uniformity
  • # echo “ text is comfortable ”
  • # echo “ the inputs are limited by the keys on the keyboard ”
  • # echo “ there’s nothing to click ”
  • $ echo “ text interfaces were around in our early computers ”
  • [user@localhost ~]$ echo “ they’re still around now ”
  • mysql> select ‘ we talk text to our databases ’
  • (gdb) print “ and to debuggers ”
  • A:> echo to systems that are old
  • C:UsersAdministrator> echo even to systems that are new
  • “ we use text to command our computers to do our bidding ”
  • “ we’ve created little programs ”
  • “ commands ”
  • “ with funny names ”
  • “ like ‘tar’ or ‘finger’ or ‘mount’ ”
  • “ with cryptic names ”
  • “ or ‘ps’ or ‘cacls’ or ‘fsck’ or ‘df’ ”
  • “ we created ways for these programs ”
  • cat to_talk | grep -e “.* to one another ” >> using_pipes
  • # echo “ we added $variables ”
  • # echo “ and `functions` ”
  • # echo " and `(cat -s /tmp/ other ) && (echo ' concepts ')`"
  • Set or_Special = Wscript.CreateObject("Scripting.FileSystemObject") strFolder = “ programming languages ” or_Special.DeleteFolder(strFolder) Wscript.Echo “ to solve every day problems and run our computers well ”
  • “ and then we did something strange and rather confusing ”
  • “ we made all these programs ”
  • “ talk text to one another ”
  • “ this text is fine for us, humans ”
  • 22:39:55 up 21 days, 18:39, 4 users, load average: 0.05, 0.14, 0.15 USER TTY FROM LOGIN@ IDLE JCPU PCPU WHAT we_know pts/0 pool-72-71-240-1 00:58 21:41m 4.39s 4.35s pine user123 pts/2 lgb-static-66.18 Fri17 29:11m 0.01s 0.01s -bash how_to pts/3 understand 33-22l 21:38 0.00s 0.29s 0.03s sshd: howto [priv] what_we pts/4 lgb-static-66.18 Fri12 29:22m 0.04s 0.04s see
  • “ but a computer has to parse ”
  • “ so we build a command ”
  • “ we give it a lot of intelligence ”
  • “ we empower it to do one thing, and do it really, really well ”
  • “ using brilliant data structures to store the data during processing ”
  • “ it comes up with great output ”
  • “ that ends up as text ”
  • “ and then the next command ”
  • “ in our pipeline ”
  • “ has to understand that text ”
  • “ figure out how to separate the ”
  • /dev/ important / ext3 rw 0 0 /dev/md1 /usr/ from proc rw 0 0 #/dev/ the / not so much 0 0
  • “ so that it can get this data ”
  • “ back into brilliant data structures for processing ”
  •  
  • “ doing one thing well does not mean that we have to deal with text ”
  • “ doing one thing well does not mean having your own command line parameter syntax ”
  • ps -ef ps -aux ps aux
  • ls -a ls --all wget -e wget --execute
  • “ a command does not live on its own, even if it’s single-purposed ”
  • “ it lives in a shell environment ”
  • “ and interacts, indirectly, with other commands ”
  • “ so enter PowerShell ”
  • “ it’s a command shell for Windows operating systems ”
  • “ and an evolved approach to how commands can interact ”
  • “ it holds that each command ”
  • “ should do only one thing ”
  • “ and do it very well ”
  • “ and that anyone should be able to write a new command ”
  • “ and that commands are to be connected by pipes ”
  • “ and that you need variables and control structures ”
  • “ it’s really a fully-fledged programming language ”
  • “ you talk to it using text ”
  • “ but it talks to you using objects ”
  • “ and talks to itself using objects ”
  • “ you can see those objects ”
  • “ or just see the text that represents them ”
  •  
  • “ in PowerShell, the command is called a ‘cmdlet’ ”
  • “ pronounced ‘commandlet’ ”
  • “ commands have a common naming pattern ”
  • “ ’ verb-noun’ ”
  • Get-ChildItem Move-Item Sort-Object Set-Location Where-Object
  • “ these commands can also have easier to remember aliases ”
  • Get-ChildItem = dir, ls, gci Move-Item = move, mv, mi Sort-Object = sort Set-Location = cd, chdir, sl Where-Object = where, ?
  • “ each command manipulates objects ”
  • “ for example, ‘dir c:’ gets a listing of files in the root directory ”
  • “ but you can also say ‘dir HKLM:SoftwareMicrosoft’ to iterate the registry ”
  • “ because ‘dir’ just means ‘Get-ChildItem’ – get a list of children of an object that is a container ”
  • “ the file system’s directories are considered containers ”
  • “ as are registry keys ”
  • PS C:WindowsSystem32driversetc> dir Directory: Microsoft.PowerShell.CoreFileSystem::C:WindowsSystem32driversetc Mode LastWriteTime Length Name ---- ------------- ------ ---- -a--- 4/2/2007 3:51 AM 829 hosts -a--- 9/18/2006 2:41 PM 3683 lmhosts.sam -a--- 9/18/2006 2:41 PM 407 networks -a--- 9/18/2006 2:41 PM 1358 protocol -a--- 9/18/2006 2:41 PM 17244 services
  • “ actually, the whole command line operates on objects ”
  • “ if you do nothing else with them, they will be displayed as text ”
  • “ if you pass them on, they will be processed as objects ”
  • PS C:WindowsSystem32driversetc> $x = dir PS C:WindowsSystem32driversetc> $x Directory: Microsoft.PowerShell.CoreFileSystem::C:WindowsSystem32driversetc Mode LastWriteTime Length Name ---- ------------- ------ ---- -a--- 4/2/2007 3:51 AM 829 hosts -a--- 9/18/2006 2:41 PM 3683 lmhosts.sam -a--- 9/18/2006 2:41 PM 407 networks -a--- 9/18/2006 2:41 PM 1358 protocol -a--- 9/18/2006 2:41 PM 17244 services
  • PS C:WindowsSystem32driversetc> $x[0] Directory: Microsoft.PowerShell.CoreFileSystem::C:WindowsSystem32driversetc Mode LastWriteTime Length Name ---- ------------- ------ ---- -a--- 4/2/2007 3:51 AM 829 hosts
  • PS C:WindowsSystem32driversetc> $x[0].Name hosts
  • PS C:WindowsSystem32driversetc> dir | where { $_.Name -eq "hosts" } Directory: Microsoft.PowerShell.CoreFileSystem::C:WindowsSystem32driversetc Mode LastWriteTime Length Name ---- ------------- ------ ---- -a--- 4/2/2007 3:51 AM 829 hosts
  • Get-ChildItem | Where-Object { $_.Name -eq "hosts" } Dir | Where { $_.Name -eq “hosts” } ls|?{ $_.Name -eq “hosts”}
  • PS C:WindowsSystem32driversetc> dir | foreach { "The file $($_.Name) is $($_.Length) bytes long" } The file hosts is 829 bytes long The file lmhosts.sam is 3683 bytes long The file networks is 407 bytes long The file protocol is 1358 bytes long The file services is 17244 bytes long
  • PS C:WindowsSystem32driversetc> dir | measure-object -property Length -Sum Count : 5 Average : Sum : 23521 Maximum : Minimum : Property : Length
  • PS C:WindowsSystem32driversetc> dir | foreach { $_.Length } | Measure-Object -Sum Count : 5 Average : Sum : 23521 Maximum : Minimum : Property :
  • PS C:> get-process | where { $_.WorkingSet -gt 150MB } Handles NPM(K) PM(K) WS(K) VM(M) CPU(s) Id ProcessName ------- ------ ----- ----- ----- ------ -- ----------- 658 19 175704 155676 470 506.77 36964 firefox PS C:> get-process | where { $_.WorkingSet -gt 150MB } | stop-process
  • PS C:> get-process | where { $_.Name -eq 'firefox' } | format-list * __NounName : Process Name : firefox Handles : 333 VM : 207273984 WS : 101548032 PM : 84729856 NPM : 18560 Path : C:Program FilesMozilla Firefoxfirefox.exe Company : Mozilla Corporation CPU : 11.9028763 FileVersion : 1.8.1.9: 2007102514 ProductVersion : 2.0.0.9 Description : Firefox Product : Firefox Id : 6052 PriorityClass : Normal HandleCount : 333 WorkingSet : 101548032 PagedMemorySize : 84729856 ...
  • PS C:> function multBy2 { $_ * 2 } PS C:> 1..10 | foreach { multBy2 } 2 4 6 8 10 12 14 16 18 20
  • PS C:> function concat($a, $b) { "$a-$b" } PS C:> concat "hello" "there" hello-there PS C:> concat -a "hello" -b "there" hello-there PS C:> concat -b "hello" -a "there" there-hello
  • PS C:> [System.Net.Dns]::GetHostByName("localhost") | fl HostName : TARDIS Aliases : {} AddressList : {127.0.0.1}
  • PS C:> $rssUrl = "http://twitter.com/statuses/friends_timeline/766734.rss" PS C:> $blog = [xml] (new-object system.net.webclient).DownloadString($rssUrl) PS C:> $blog.rss.channel.item | select title -first 8 title ----- ori: @ori thinks there's a lot of drunk-twittering going on at barcampla. Yeah I just referred to myself in the thir... Zadi: Going to curl up and watch Eraserhead, finally. 'Night guys. Remember to turn back the clock. annieisms: i can haz crochet mouse? http://tinyurl.com/3ylmj7 ccg: hey BarCampers (and others) remember to set your clocks (and by clocks, I mean phones) back an hour tonight, Ya... Mickipedia: Everyone follow @egredman for some great music! Mickipedia: ...and party every day! heathervescent: Omg theses french djs blown mah mind. groby: @barcampla Clearly, you guys don't know what's good! Salty Black Licorice FTW! ;)
  •  
  • “ there is more to PowerShell ”
  • “ you can write GUI apps in it, if you really want to ”
  • “ you can administer servers ”
  • “ you can completely replace your normal cmd.exe usage with it ”
  • “ but even if you ignore it ”
  • “ remember its lessons ”
  • “ about the power of text ”
  • “ and the power of objects ”
  • thank you