Archive for the ‘shell’ tag
Powershell Productivity
Powershell can be a tricky shell to learn but it’s worth it, it’s a great .Net based scripting environment that once familiar you’ll quite happily alias cmd to it and wish that you had the same intuitive environment on your linux counterparts. Scott Hanselmann was right years ago when evangelising this great product and it is a shame that I’m only embracing it now.
Here’s a few snippets that I’ve used in the past few days. They are a bit verbose at this stage, but it’s all part of the learning, hopefully one day I might be answering your Powershell questions over on StackOverflow.com.
1) Run this in a solution directory and it will produce a summary of classes that contain Tests, it will sum the number of tests and group the tests by Directory. Admittedly this took me 15 minutes to write, but probably would take less than a minute if I had to restart
PS C:\dev\pxan> gci -rec -include "*.cs" | where { ($numberTests = $(gc $_.FullName | where { $_.ToString() -match "\[Te st\]" }).Count );($numberTests -gt 0) } | where { $numberTests -gt 0 } | select @{n="Name";e= { $_.Name }}, @{n="Directo ry"; e={$_.Directory }},@{n="NumberOfTests";e={ $numberTests }} | group -property Directory | foreach { Write-Host $_.Na me; (foreach { $_.Group } | Select Name, NumberOfTests ) } C:\dev\pxan\Shark.Client\Tests Name NumberOfTests ---- ------------- FormBucketTests.cs 14 FormDataExtractorTests.cs 17 ProcSessionTests.cs 13
2) Show me the top five CPU consuming processes, hmm.. This Windows Live Mesh consumes a lot more power than FolderShare.
PS C:\Documents and Settings\Matt Freeman> gps | sort CPU -desc | select -first 5 Handles NPM(K) PM(K) WS(K) VM(M) CPU(s) Id ProcessName ------- ------ ----- ----- ----- ------ -- ----------- 695 24 62800 81644 226 206.94 504 Moe 526 19 51548 38548 346 54.52 2784 WindowsLiveWriter 459 9 3412 1656 45 26.41 692 lsass 1116 0 0 240 2 9.52 4 System 173 6 26436 29200 78 9.44 1864 nod32krn
3) Occasionally I find myself needing to search through a directory of files for a particular string in a file (eg. a database credentials in some PHP app etc..). You could substitute the extra $true to filter further down by modified date, or file size, or extension etc..
PS C:\dev\phpcms> gci -rec | where { $fileName = $_.Name; $true} | gc -ErrorAction SilentlyContinue | where { $_.Contains
("password") } | foreach { Write-Host $fileName " ::::: " $_ }
setup.php ::::: $password = "abc";
adminsetup.php ::::: $password = "abc";
I’ve found that the standard powershell application fine for performing most shell tasks although this week I’m going to wrap some of snippets I used regularly into scripts in which I’d probably use an IDE like PowerShell Analyzer for such.
What’s also pretty cool is that one can use powershell from build scripts so I forsee this replacing some of less obviously syntax I’ve had to use to achieve tasks in MSBuild and NAnt directly.