Thursday, December 17, 2009

I really like the @PowerTip blog series but I find myself wanting to talk back with more than 120 characters. Please enable comments @PowerTip. Comments are enabled for authenticated users. In the meantime, I’ll blog my comments.

re: Get Process Owners

function Get-PSOwner($searchString)
{	
    $foundProcess = ps $searchString
    if($foundProcess -eq $null) { return; }
    gwmi Win32_Process -Filter ("Handle={0}" -f $foundProcess.id ) | 
        % { Add-Member `
            -InputObject $_ `
            -MemberType NoteProperty `
            -Name Owner `
            -Value ($_.GetOwner().User) `
            -PassThru } | 
        select Name, Handle, Owner
}

The four minor changes I made to the @PowerTip script are: 1) it’s functionized, 2) the input is a search string for process (wildcards accepted), 3) it spits out the correct errors if the process can’t be found, and 4) the process handle is added to the results.

Download the script here: http://bitbucket.org/xcud/powershell-snippets/src/tip/Get-PSOwner.psm1

Wednesday, September 30, 2009

(tongue set firmly in cheek)

I happened upon Dana Merrick’s blog entry with a ruby script that retrieves an order status from dominos. In the spirit of “anything you can do I can do better” i threw this Psh equivalent together and submitted it to PoshCode.org. It’s phenomenally simple. It makes a request to the Dominos SOAP order status service and selects and displays the order status nodes if they exist.

function Get-DominosOrderStatus($pn) {
  [xml]$content = (new-object System.Net.WebClient).DownloadString(
    "http://trkweb.dominos.com/orderstorage/GetTrackerData?Phone=$pn");
  $statii = select-xml -xml @($content) `
    -Namespace @{dominos="http://www.dominos.com/message/"} `
    -XPath descendant::dominos:OrderStatus
  if($statii.Count -gt 0) { $statii | %{ $_.Node } }
  else { "No orders" }
}

Sample Output:

Version               : 1.3
OrderAsOfTime         : 2008-06-04T16:40:48
StoreAsOfTime         : 2008-06-04T16:44:55
StoreID               : 3189
OrderID               : 2008-06-04#73694
Phone                 : 3145551234
ServiceMethod         : Delivery
AdvancedOrderTime     :
OrderDescription      : 2 Small(10") Hand Tossed Pizza

OrderTakeCompleteTime : 2008-06-04T16:27:52
TakeTimeSecs          : 0
CsrID                 : Power
CsrName               :
OrderSourceCode       : Web
OrderStatus           : Out the Door
StartTime             : 2008-06-04T16:27:52
MakeTimeSecs          : 237
OvenTime              : 2008-06-04T16:31:49
OvenTimeSecs          : 360
RackTime              : 2008-06-04T16:37:49
RackTimeSecs          : 179
RouteTime             : 2008-06-04T16:40:48
DriverID              : 0818
DriverName            : Edna
OrderDeliveryTimeSecs :
DeliveryTime          :
OrderKey              : 1dRprcnzmWxaOXvlzj06OlFdzuexcIC/
ManagerID             : 5560
ManagerName           : Danillo

#text : Out the Door

You can download it here: http://bitbucket.org/xcud/powershell-snippets/src/tip/Get-DominosOrderStatus.psm1

Monday, September 21, 2009

Inspired by Chad Miller’s work MSChart I threw this ANSI barchart module together:

#
# Out-AnsiGraph.psm1
# Author:       xcud
# History:
#       v0.1 September 21, 2009 initial version
#
# PS Example> ps | select -first 5 | sort -property VM | 
#             Out-AnsiGraph ProcessName, VM
#                 AEADISRV ███ 14508032
#                  audiodg █████████ 50757632
#                  conhost █████████████ 73740288
# AppleMobileDeviceService ████████████████ 92061696
#                    btdna █████████████████████ 126443520
#
function Out-AnsiGraph($Parameter1=$null) {
	BEGIN {
		$q = new-object Collections.queue
		$max = 0; $namewidth = 0;
	}

	PROCESS {
		if($_) {
			$name = $_.($Parameter1[0]);
			$val = $_.($Parameter1[1])
			if($max -lt $val) { $max = $val}		 
			if($namewidth -lt $name.length) { 
				$namewidth = $name.length }
			$q.enqueue(@($name, $val))			
		}
	}

	END {
		$q | %{
			$graph = ""; 0..($_[1]/$max*20) | 
				%{ $graph += "█" }
			$name = "{0,$namewidth}" -f $_[0]
			"$name $graph " + $_[1]
		}

	}
}

Export-ModuleMember Out-AnsiGraph

Download the script here: http://bitbucket.org/xcud/powershell-snippets/src/tip/Out-AnsiGraph.psm1

Saturday, September 19, 2009

Read Chad Miller’s blog entry on using MSChart from PowerShell. This is another great visualization extension for PowerShell. To make it work:

  • Download and install Microsoft Chart Controls for Microsoft .NET Framework 3.5
  • Download LibraryChart
  • The easiest way to enable it is to import the library with a call to ‘Import-Module’. Now run one of the samples:

    If you see this error or one like it it’s an easy fix. Edit the lib and edit the input param list in the function “New-Chart” from this:

    param ([int]$width,[int]$height,[int]$left,[int]$top,$chartTitle)

    To this:

    param ([int]$width,[int]$height,[int]$left,[int]$top,[string]$chartTitle)

    Voila. Re-import the module and the error is gone. The only item on my wish list for this lib is that it attempt to infer the X and Y fields. A call like this, for example, should be able to (but can’t) infer that xField is ‘name’ and yField is ‘ws’:

    ps | Sort-Object WS | select name, ws -first 5 | out-chart

    It makes eye-candy. Check this one out. Grab the top 5 memory consuming processes and push them into a relative pie chart that auto-updates itself.

    out-chart -chartType ‘pie’ -xField ‘name’ -yField ‘WS’ -scriptBlock { Get-Process | Sort-Object -Property WS | Select-Object Name,WS -Last 5}
    Tuesday, September 15, 2009

    If you’re trying to use Subversion from the PowerShell command line and you can’t get around this error message:

    PS C:\Users\Public\Source> svn commit .

    svn: Commit failed (details follow):

    svn: Could not use external editor to fetch log message; consider setting the $SVN_EDITOR environment variable or using the —message (-m) or —file (-F) options

    svn: None of the environment variables SVN_EDITOR, VISUAL or EDITOR are set, and no ‘editor-cmd’ run-time configuration option was found

    Set your SVN_EDITOR preference with this command (substituting “type” for your preferred editor):

    PS C:\Users\Public\Source> set-item -path env:SVN_EDITOR -value “type”