Microsoft DFAgent stopped working
While debugging an Azure project a couple of weeks ago the Azure Emulator startup process kept throwing the unhandled exception “A heap has been corrupted.”
After poking around for a bit looking for x86/x64 settings compat problems I remembered that as a part of some PowerShell v3 testing I’d done earlier in the week on this same machine I’d set a registry key so that PowerShell would use the latest CLR. Unwinding those registry keys seems to have resolved the DFAgent error.
Run these commands to unwind the ‘OnlyUserLatestCLR’ change on your machine (the 2nd line only applies to x64):
reg add hklm\software\microsoft.netframework /v OnlyUseLatestCLR /t REG_DWORD /d 0
reg add hklm\software\wow6432node\microsoft.netframework /v OnlyUseLatestCLR /t REG_DWORD /d 0
Get-NetworkStatistics (or ‘netstat’ for PowerShell)
Shay Levy did an excellent PowerShellization of ‘netstat’ (link) on his blog back in February. JRich also had an excellent take on implementing NetStat in PowerShell (link). There is also a simple one-liner [net.NetworkInformation.IPGlobalProperties]::GetIPGlobalProperties().GetActiveTcpConnections() that will get you Tcp connection information without the process information but IMHO the info provided is practically useless in most real world scenarios without the process info.
One can argue over which solution is more PowerShellicous, “inlining c#” or “scraping cmd output”. Meanwhile while that flame war is happening I’ll be over here extending either of these solutions because PowerShell is awesome like that. Here I added filtering to the Shay’s solution. Behold:
PS C:\Users\ben> Get-NetworkStatistics skype | ft -AutoSize Protocol LocalAddress LocalPort RemoteAddress RemotePort State ProcessName PID -------- ------------ --------- ------------- ---------- ----- ----------- --- TCP 0.0.0.0 443 0.0.0.0 0 LISTENING Skype 4068 TCP 0.0.0.0 9841 0.0.0.0 0 LISTENING Skype 4068 PS C:\Users\ben> Get-NetworkStatistics -ProcessId 3368 Protocol : TCP LocalAddress : 192.168.10.115 LocalPort : 49899 RemoteAddress : 204.152.18.196 RemotePort : 443 State : ESTABLISHED ProcessName : chrome PID : 3368
Here’s the source:
Get Process Owners
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.
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
Get-DominosOrderStatus, a profound poshcode contribution
(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
Out-AnsiGraph
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
MSChart in Psh
Read Chad Miller’s blog entry on using MSChart from PowerShell. This is another great visualization extension for PowerShell. To make it work:
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:
To this:
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’:
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.

enable ‘svn commit’ from PowerShell
If you’re trying to use Subversion from the PowerShell command line and you can’t get around this error message:
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):

