[GiM logo] gim.org.pl is down || odświeżony jogger (v.0.4) GiMa

At work I try to use powershell just to learn at least a few things about it. Of course I could use zsh, bash or any other unix shell, but that would be no fun, would it?

head & tail

What I was missing at the beginning, were unix head and tail, this one's quite easy.
head:

  1. (hg glog)[0..20]

tail:

  1. (hg glog)[-20..-1]

Why not reverse the orser of lines in head? No problem

  1. (cat somefile.txt)[20..0]

We can even use tail and head at once with (In case of hg glog this probably isn't the best idea ;)):

  1. (hg glog)[-20 .. 20]

for each...

I often have to run some command on files in given dir, in *nix systems I'd probably use something like:

  1. find dir -exec foobar {} \;

In powershell I do it like this:

  1. ls -r foo | % { .\doNoEvil.exe $_.get_FullPath(); }
  2. ls -r foo | % { .\doNoEvil.exe $_.FullName; }

% is an alias for ForEach-Object commandlet. $_ is a special pipeline variable, which holds reference to current object.

Hexdump

In unices I usually use xxd or od if xxd is not present, In powershell I use the following one-liner, I've split it to several lines to make it easier to read:

  1. (gc -Encoding byte -readCount 16 -totalCount 64 somefile.bin)[1..2] |
  2. %{
  3.     $dmp = $_ |
  4.     %{
  5.         ("{0:x2}" -f $_)
  6.     }
  7.     "$dmp"
  8. }

gc is an alias for Get-Content commandlet. In the first line I read 64 bytes of input in blocks of 16 bytes. Get-Content returns an array of 4 16-bytes block, I choose blocks 1-2 using subscript.
If you know where's the data you'd like to look at, it's good idea to pass totalCount to gc, since this speeds things A LOT :).
The codes in lines 2-8 is executed for each 16-bytes block, so $_ in third line refers to 16byte block of data. That block is passed to inner loop in lines 4-6.
The loop executes strange-looking ("{0:x2}" -f $_) on every byte. This line formats a byte using .net formatter you can read more about it on msdn Format.String page.
So formatted string is assigned to $dmp value which is print to the screen in line 7.
Now time for a better trick that I leave you for analysis:

  1. ls | % {
  2.     $fn=$_;
  3.     $sig = (gc -Encoding byte -readCount 4 -totalCount 32 $_)[7]
  4.     if ($sig[0] -eq 0x53 -or $sig[0] -eq 0x47 -or $sig[0] -eq 0x4d) {
  5.         .\doNoEvil.exe $fn;
  6.     }
  7. }

Other usefull stuff

By default executing python script in powershell will spawn a new shell (which is baad thing, because we can't play with the output).
Solution is to add python scripts extenstion to PATHEXT environment variable. From powershell console this can be made using following syntax:

  1. $env:pathext += ";.py"

Alternatively you can change is system-wide in control panel => System => Advanced System Settings => Advanced (card) => Environment Variables.

A bit annoying thing is that by default powershell converts output of command outomagically into internal representation, so running for example:

  1. python superscript.py > blurp.txt

Will generate output in utf-16 (that probably depends on your environment, dunno).
What you have to do is use Out-File commandlet:

  1. python superscript.py | out-file -encoding UTF8 blurp2.txt

Supposedly you could play with $OutputEncoding variable, I've tried, but it seems it does a bit different thing than I'd like it to do. If you'll find sollution, please gimme a tip :)

P.S. For the DDL guys, pleas be patient, I have lot of other things to do ATM

catz: [kom.puterowe] [micr.osoft] [Techblog]
tagz: [head] [hexdump] [powershell] [tail]
dnia sobota, 23 maj 2009, 180839 by Michał 'GiM' Spadliński

Komentarze:

Proszę wpisy pisane po angielsku komentować również w tym języku.

You don't have to use $_.get_FullPath(), you can just simply use $_.FullPath - it's .NET, not Java, we really have attributes in objects ;-)

dnia sobota, 23 maj 2009, 201802 by Ktos

Looking at those examples makes me understand why I was so hesitant about forcing myself into using powershell scripting... My first impression was "why the hell is this so complicated?", and your notes disillusioned me - it really is complicated, it wasn't only my subjective view.

dnia niedziela, 24 maj 2009, 143735 by leafnode

@leafnode: it's not that complicated, it just takes some time to get used to it. Pipeline is somewhat 'strange', but for my daily tasks I hadn't got problems with it.

@Ktos: somehow while listing Get-Method for file object, I've chosen method instead of property (attribute if you prefer). btw thanks, this line contained error, since there's no get_FullPath method ;)

dnia niedziela, 24 maj 2009, 162321 by GiM

great post, thx :) I was looking sth like this :)

dnia niedziela, 02 sierpień 2009, 133735 by irider

..tożsamość..:
..meritum..:
..lokum..:
Wpisz kod:code