Saturday, September 10, 2011

Creating a crash dump from within the running process

 The idea is that a running application, as part of its error handling could create a dump, preserving interesting information.
Sometimes a stack trace just isn't enough.
I used F# just for funsies.
let something =
    System.Console.WriteLine("Hello")
    let thisProcess = System.Diagnostics.Process.Start("C:\Program Files\SysInternals\procdump.exe", "ConsoleApplication1 dump.dmp")
    thisProcess.WaitForExit()
    System.Console.WriteLine(System.Environment.CurrentDirectory)
    System.Console.ReadLine() 

Procdump is a sysinternals tool, and more suited to this than anything like WinDBG or ADPlus. 

Tuesday, August 9, 2011

How to get powershell syntax highlighting in HTML

Step 1: Use PowerGUI.
Aside from the code-highlighting, it's all kinds of awesome.
Step 2: Select the code
Step 3: From the Menu: Edit->Copy As->HTML
Step 4: Paste and enjoy in your Blog.

Finding locked files with OpenFiles

OpenFiles is a command line executable that has been around since XP. It isn't a powershell command so it doesn't produce nice PSObjects to play with, just raw text.

To find a particular file in the daunting amount of output, I do this:

OpenFiles | %{ if ($_.contains("search string")) { $_ } } 

Powershell locks loaded assemblies. Remove-Module doesn't help.

I was testing an assembly generated by Visual Studio. I imported the module into powershell via Import-Module, created the class I wanted, invoked its members, and viewed the results. Then I tweaked the project in Visual Studio and rebuilt. Powershell had the output assembly loaded and locked, so the build failed.

No news there.

The first workaround was to just close powershell and reopen it.

But! I figured I could call Remove-Module and get passed the lock without actually closing Powershell...
While this seemed like a good idea. It failed. Powershell didn't remove the lock it had on the imported assembly and it was still listed in the modules that Powershell was using. Remove-Module didn't remove the module. It just stopped showing up in Get-Modules. In fact, the variables created using the .net dll were still alive in the powershell process. Remove-Module was no solution.

Next! I figured that I would load the module inside of a powershell session and then unload the session.
Enable-PSRemoting
$session = $New-PSSession
Enter-PSSession $session
#Do Stuff
Exit-PSSession
Remove-PSSession $session

This failed as well. :(

Next I figured that I could create an AppDomain, create a Runspace in the AppDomain, do stuff, and then unload the AppDomain. I'm certain that this approach would work, but it isn't worth the time, especially since the following pseudo-shadow copy solution will work just fine:

$fileName = [System.IO.Path]::GetTempFileName()
[Environment]::CurrentDirectory = "myProjectDirectory\bin\x86\Debug"
[System.IO.File]::Copy("MyDll.dll", $fileName + ".dll")
Import-Module ($filename + ".dll")

And thus the module is loaded, and doesn't lock the Visual Studio output files.

Monday, March 21, 2011

Loading the functions from a powershell script into session memory

.
The answer is ".".
Used like this:
. .\ApowershellScript.ps1
And then within the same powershell session I could do this:
SomeFunctionFromTheAforementionedScript ARandomArgument
And it would work.

I had been calling Process.Start, but the call started failing once I tried adding the call to the powershell function within it.
So this did not work:
1 ProcessStartInfo info = new ProcessStartInfo("powershell.exe");
2 info.Arguments = "{ . C:\\LivingRoom\\ir.ps1; sendIR " + command + " }";
3 info.RedirectStandardError = true;
4 info.RedirectStandardOutput = true;
5
6 info.UseShellExecute = false;
7
8 System.Diagnostics.Process process = Process.Start(info);

So this is what I did get to work:

1 Runspace rs = RunspaceFactory.CreateRunspace();
2 rs.Open();
3
4 Pipeline pipeline = rs.CreatePipeline();
5 Command command3 = new Command(" . C:\\LivingRoom\\ir.ps1", true, false);
6 Command command4 = new Command(" sendir " + command, true);
7 pipeline.Commands.Add(command3);
8 pipeline.Commands.Add(command4);
9 pipeline.Invoke();

It's all about the .

In order to use a Runspace in C#, I had to know the super-secret location of the .net assembly that contained the Runspace code.
C:\Program Files\Reference Assemblies\Microsoft\WindowsPowerShell\v1.0\System.Management.Automation.dll
The fact that they hid it is this odd location is weird. :P
Oh well.

Sunday, March 20, 2011

CoffeeScript and Chirpy

I've found some things that I think are cool:
CoffeeScript 
and
Chirpy

CoffeeScript allows me to use a language other than javascript to write javascript.
Chirpy lets me write that not-javascript inside of Visual Studio. It also does neat things with javascript minimization and packaging.

Saturday, March 19, 2011

World of Warcraft Automation using Coded UI Tests from Visual Studio 2010

Previously I discussed automating repetitive tasks in World of Warcraft using powershell. While that works well, there are some situations where the only thing that will work is a mouse click. SendKeys makes it easy to send keyboard input to a window, but for some reason, I could not find an easy way of sending mouse input using a .Net API. "Easy" is the key word here, I could always try sending a message directly to the window through the Windows API, or involve some expensive third party automated testing tool.

I eventually stumbled upon a new feature in Visual Studio 2010: a coded UI test. The tests themselves are easy to create, the wizard in VS walks you through them excellently. There are plenty of resources on the net that describe how to use this feature in VS, so I'll spare you a redundant and feeble attempt at doing that, and show you how to integrate the result into a powershell script for easy manipulation.

The coded UI test runs very well inside of Visual Studio, but we need to run it externally. The compiled dll from the test project doesn't run by itself though, we require a utility called MSTest to run it.

The finished powershell script integrated with MSTest, looks like this:

Import-Module C:\Windows\Microsoft.NET\Framework64\v4.0.30319\WPF\UIAutomationClient.dll
$a  = [System.Windows.Automation.AutomationElement]::RootElement.FindAll([System.Windows.Automation.TreeScope]::Descendants, [System.Windows.Automation.Condition]::TrueCondition) 
$wow = $a | Where { $_.Current.Name -eq "World of Warcraft" } 
$pattern = $wow.GetCurrentPattern([System.Windows.Automation.WindowPattern]::Pattern)
$pattern.SetWindowVisualState([System.Windows.Automation.WindowVisualState]::Normal)
for($i = 0; $i -lt 80; $i++) { 
     $mstest = "C:\program files (x86)\Microsoft Visual Studio 10.0\common7\ide\mstest.exe"
     Push-Location
     Set-Location "C:\Users\Ogre\Desktop\SVN\Powershell Samples\"
     & $mstest "/testcontainer:testproject1.dll"
     Pop-Location
     Sleep 4
     [System.Windows.Forms.SendKeys]::SendWait("=")
}

The Visual Studio test project was compiled to testproject1.dll and the output was copied into the same directory as this powershell script.

Creating a dll for a particular UI action in World of Warcraft as shown here does take a bit of doing, it certainly isn't as easy as using SendKeys, but you can, using this technique, automate any action in the UI, even the ones that Blizzard ordinarily has blocked from macro and lua programmatic usage.

Note that this only allows for automation of mindless repetitive tasks, if you want a full fledged bot, this falls far short.