Sunday, 22 February 2009

PowerShell : Dealing with a ‘-‘ literal in data

Lets say for example that you are parsing an xml document with PowerShell and one of the nodes is called “direct-message” as in the Twitter REST API, and using the cool PowerShell dot notation for navigating the document you might expect that you can just do this


[array]$messages = $xml.direct-messages.direct_message


but sadly you will just get this error


You must provide a value expression on the right-hand side of the '-' operator.

At C:\Utils\TwitterClient\TwitterClient.ps1:12 char:38


+ [array]$messages = $xml.direct- <<<< messages.direct_message



Basically PowerShell is seeing the ‘–’ and treating it as an operator.. but all is not lost because you can simply just escape the offending node in single quotes, as such :




[array]$messages = $directXml.'direct-messages'.direct_message

Saturday, 21 February 2009

Regex : Matching a word, how hard can it be ?

You would be tempted to think that matching a word in sentence would be an easy thing to do with a regex.. but not a simple as you might have expected.

Lets take a typical twitter tweet for example where we want to pull out the URLs :

I found this cool site http://tinurl/somevalue and this one too http://tinurl/somevalue

I first tried :

(http:.*?)\s

But alas no, you don’t get the last url… which is obvious since the last ones does not have a trailing space – so knee jerk to only look for the space if it is thre

(http:.*?)\s*

umm dope.. now I only get the http: part ( duh! ) ….Right, now I will get you,  should be just as simple as searching for a space or the end of the line so -

(http:.*?)[\s|$] – fail
(http:.*?)[\s$] – fail
(http:.*?)[\s|\b] – fail

Right so I suck at regex, and someone better at this would probably solve this in their sleep…

What should have been a simple regex is starting to look overly complex so I found a cunning way to work around this – put a space at the end of the sentence – and hey presto you get them both with the first a simple looking regex – ahhh.

What to play around with regex’s with a nice visual feedback then check out http://www.gskinner.com/RegExr/

Tuesday, 17 February 2009

Break Pointing a Windows API

Have you ever wanted to know when a method is being called – so you think, not problem I will just put a break point there and bob’s your uncle there you go. Then one day you want to do the same with one of the windows dll API’s eg CreateThread.

So what to do ? There does not appear to be any cunning way to do this with Visual Studio ( I am keen to hear if you know me to be wrong! ), but you can do it with the power that is WinDbg which you can get as part of the Debugging Tools for Windows.

Now this is not as easy tool to just pick up and use ( at least not well ) but it is nice and simple in that you don’t have to do too much config to get it going.. it just works! Hey I got it up an running – how hard can it be !

So what do you do if just want to simply break point the CreateThread :

1) Launch WinDbg
2) Ctrl+E : Point it to your exe
3) Ctrl+S : Point to your symbols
4) Ctrl+P : Point to your source

Right now you have all the information you could want ( probably more ).. now lets add that illusive break point using the edit box at the bottom ( basically a command line )

5)

image

Ahh don’t you love it when it is simple :-)

6) F5 : Then let it run

Then it will run and break, and wait for you

image
7) k : and view the call stack ( or kP if you want extra param info in your call stack )

Monday, 16 February 2009

Getting the rest of the params in a batch file

If you try this you will be sadly disappointed

set FIRST=%1
shift /1
set THEREST %*

The best way I have seen to do this is as follows :

for /f "tokens=1*" %%a in ("%*") do (   
    set FIRST=%%a   
    set THEREST=%%b)

Thursday, 12 February 2009

Fighting WIX Merge Module

This one caused me some pain…. imagine if you are trying to use Wix3 to create a nice merge module for a C++ application in VS2005. Chances are you are going to end up with this error

Error 7 Undefined preprocessor variable '$(var.MyProject.TargetFileName)'

if you have a wxs that looks something like this :

<?xml version="1.0" encoding="UTF-8"?>
<Wix xmlns="http://schemas.microsoft.com/wix/2006/wi">
  <Module Id="TestMergeModule" Language="1033" Version="1.0.0.0">
    <Package Id="ef2a568e-a8db-4213-a211-9261c26031aa" Manufacturer="Me" InstallerVersion="200" />
    <Directory Id="TARGETDIR" Name="SourceDir">
      <Directory Id="MergeRedirectFolder">
        <Component Id="MyProject" Guid="{1081C5BC-106E-4b89-B14F-FFA71B0987E1}">
          <File Id="Test" Name="$(var.MyProject.TargetFileName)" Source="$(var.MyProject.TargetPath)" DiskId="1" />
        </Component>
      </Directory>
    </Directory>
  </Module>
</Wix>

After much fighting and time wasting I found that this is known bug and there is a request in to fix it, you can see details here on StackOverflow.

Tomato Timing

After reading about the tomato timing, I decided that I should give it a go…

So the first thing I needed was a way to time myself, so after looking on the internet for a simple count down timer I found that most suck ( which is hard to do since you just want a count down ) and they want you to pay – why ? are they trying to rip off the masses who can’t do any form of developing or scripting.. *sigh* and power things like GIT are free… go figure!

So since I need to learn how to use powershell, I might as well write a Tomato timer in Powershell… so here you go, for free -  a PS tomato timer :

param([int]$mins =25 )
while($mins -gt 0 ) {
  
$host.ui.RawUI.WindowTitle ="$mins mins remaining"
  
Start-Sleep(60)
  
$mins =$mins -1
    }
Write-Host`a



Wednesday, 11 February 2009

The Pomodoro Technique ( Tomato )

This is an interesting technique for managing time, details of this can be found in this PDF document.

Basically it is a technique that challenges you to work in short burst of 25 minutes – but absolutely un-interrupted. With an enforced few minute break, between each pomodors ( tomatoes ), followed by a 25-30 minute break after every 4 tomatoes.

Then you can measure the  work you have done in Tomatoes, and help you chose how to spend your tomatoes on your tasks of interest.

What interests me about this is that I am surprised by the shortness of the tomato.

Monday, 9 February 2009

SVN : Keeps asking for your credentials ?

The issue

You too may have come across this annoying one: you do an update, and halfway thought it stops and asks you for your username and password.. ummm what ? like how did you get the first stuff then ?

Well it appears that it is related to the use of externals in the SVN repo, and the fact that each of these repos are do not have the same users. So when you hit the external repo you get asked for your credentials again.

Detail

Looking a bit deeper there are 2 ways in which this can happen :

1) One repo with Basic Auth and one with SSPI auth ( a way to use your domain credentials )
2) Both with Basic Auth, but without the same credentials on both repos

Work around

In general the work around is to stop using your Basic Auth user, and use your domain credentials

If you manage it yourself with Basic Auth, then just ensure you configure your auth files to have the same user.

Automated Tool : AutoHotkey

Did you ever wish you could find a tool that would take some of the grunt work away from you, but was not hard to use or set up… well do I have the toy for you or what :-)

What if you wanted to automate the clicking of a mouse… I don’t know say to reproduce a bug which requires clicking very very fast and your fingers are getting sore along with your ego ‘cause you see others doing it – hypothetical of course.

So you want to do 2 things left click and right click the mouse and then do it over and over… hmm sounds easy, and it should be.

Now if you were to have a blank page such that you could write a script to do this, and someone would come after you and write the code to just make it work, what would be the simplest way you could think to do it ?

lest start with the obvious

Click ; left should be implied
Click right

Cool that would be nice… now loop it well how about nice C syntax

loop 100 {
    Click
    Click right
}

But that would be too easy… but what that is spot on the syntax you need for AutoHotKey… nice !

Check it out here : http://www.autohotkey.com/ or just grab it from my machine \\fpstebail2\Installs\AutoHotkey104706_Install.exe

Other pluses I have seen in the first few mins of looking at it…

* It is open source
* Well documented
* Has context highlighting for all your favourite editors

image

Perhaps it gets sucky for the more complex stuff but this is exactly what I was looking for for my simple behaviours I want to repeat.

If I were a testers, I would take this simple script, well maybe this one :

$F1::  ; Make the F1 key into a hotkey (the $ symbol facilitates the "P" mode of GetKeyState below).
Loop  ; Since no number is specified with it, this is an infinite loop unless "break" or "return" is encountered inside.
{
    if not GetKeyState("F1", "P")  ; If this statement is true, the user has physically released the F1 key.
        break  ; Break out of the loop.

    Click              ; Click the left mouse button at the cursor's current position.   
    Click right       
}
return

And when ever I test a game I would launch this and hold down the F1 key and wiggle it around the game to see what I can break ( good old legendary Buys testing )!

VS2005 Ctrl+J

have you ever wished that you get get the cool intellisense but just for your class, ‘cause when you get it, you normally have everything under the sun ( and it does not shrink filter when you type ! )

image

But with this key combination that I slipped on Ctrl+J , you get just the methods in your class :

 

image

Powershell Editor ( IDE ) with Intellisense

If you have been thinking about looking at Powershell, but realize that it is closer to a complete language like perl than it is to the standard CMD, then perhaps you will be less intimidated if you have an editor that gives you all cool intellisense you could need for navigating around

Check out Power GUI it has a framework for hosting all the tools and commandlets that an admin could want, but also has a script editor

PowerGUI 

Not only that you even get killer things like debugging, with break points and watch windows etc.

PowerGUI_Debug

This is one of those things I am going to force myself to learn the year, I think I will follow John Robbins approach and just refuse to use CMD and only use PS if you do the same let me know how it goes so we can learn together.

Multiple Tags/Categories in SharePoint

If you have any interest in blogging at work, then the first thing you will find is that SharePoint appears to only support a single tag per article.. which really sucks… but it is just not true :-)

To fix this is quite simple, just follow this blog post here : http://sharepointsolutions.blogspot.com/2007/06/select-multiple-categories-for.html 

For those not interested in blogging, this is still useful because this means that you can also configure your documents in the same way, and have it show up under multiple categories.

Happy tagging in SharePoint.

Wednesday, 4 February 2009

Web pages treated as 2 files on windows

In response to this  twitter comment :

NeilDavidson Create an empty file called test.html and a directory called test_files and try to delete one and not the other from Windows. Weird. 16 minutes ago from TweetDeck

Not that weird that is how windows sores the files so that it can separate content, but if  you don’t like it just go to your folder options and set the “Manage pairs of Web pages and folders to …. Show and manage the pair as a single file

image

Tuesday, 3 February 2009

Depends Caches Information

If you drag a dll into depends it will give you all the information you could ever want, nice!

But just a quick warning, if you leave depends open and keep dragging dlls into it, you will notice that is resolves a lot quicker after the first time. Sadly this is not because it has cached any search paths or anything like that, it has simply cached the information of the dll ( in the current session ).

What this means is simply that if you are using depends to test something like rebasing a dll and using depends to check the preferred base address, the information will not update until you refresh ( F5 ), the drag is not treated as a refresh… which might lead you to think your change did not take affect.

A method you implement may just disappear...

Weird C++ behaviour

class Base {
public:
   void Method( int a, int b ){};
};

class Derived : public Base {
public:
   void Method( int a ){};
};

int _tmain(int argc, _TCHAR* argv[]) {
   Base b;
   b.Method( 1, 2 ); // works

   Derived d;
   d.Method( 1,2 ); // compiler complains
  
d.Method( 1 );    // works

   return 0;
};

Despite the prototype for each of these methods being different, since the same name has been defined above the rule of Dominance kicks in, and only the base class method is available.
 
Weird, but part of the spec.

Outlook 2007 SP2 : A Shutdown that works

How is this for interesting... Imagine, if you shut down outlook it actually goes away :-)
 
"As a result of customer feedback about the need to close Outlook and have Outlook stop running, Outlook 2007 SP 2 changes the way Outlook closes, ensuring that the intent of the end user─to close Outlook─is respected. These changes significantly affect the way that the Outlook COM server shuts down, which can impact solutions using the Outlook object model outside of the Outlook process."
 
 

SVN : Keeps asking for your credentials ?

The issue

You too may have come across this annoying one: you do an update, and halfway thought it stops and asks you for your username and password.. ummm what ? like how did you get the first stuff then ?

Well it appears that it is related to the use of externals in the SVN repo, and the fact that each of these repos are do not have the same users. So when you hit the external repo you get asked for your credentials again.

Detail

Looking a bit deeper there are 2 ways in which this can happen :

1) One repo with Basic Auth and one with SSPI auth ( a way to use your domain credentials )
2) Both with Basic Auth, but without the same credentials on both repos

Work around

In general the work around is to stop using your Basic Auth user, and use your domain credentials ( this assumes that your repo is managed by our CSI team, or that you have set up SSPI.

If you manage it yourself with Basic Auth, then just ensure you configure your auth files to have the same user.

Monday, 2 February 2009

What registry settings does an application use

If you have a small application that does not run for a long time, so you cannot process explore it, and you want to know what registry setting it uses, then here is a simple way to do it.

Since the registry key is a string, just open up the exe in an editor, and search for what might be a string. Typically you can search for the keyword SOFTWARE\ since the values like HEKY_* are just #defines of sorts.

Reading from a file with a Batch Script

Weird given how easy it is to write to a file from a batch script:

echo "Test" > file.txt

You would be mistaken for thinking it would be that easy to get it back out again into a happy variable.After a bit of searching, this is the best that I have come up with:

for /f %%a in ( file.txt ) DO ( something %%a )

Which really only allows you to use it in that line, which I guess for most cases is okay, and the other cases you can push what you need to do off into another batch file.

So what is this doing ?

Simply this is stepping though each line of the file and performing the action in the DO with the line in question in the variable %%a.

note: the double %% that is when you are writing a script, if you were doing this at the command line you only need one % for variables.

Late Variable Binding in Batch Files

This is something that I am sure makes sense if you understand it, but for me it is just confusing..

Lets say that you are developing a batch file and you wan to set up a variable, you would think that if on one like you set it, the next line you can use it… but oh nooo, you have to wait…. was this feature written by Home Affairs ?

Check this batch file:

rem notice that we define TEST in the if statement, but it does not display...

if 1 == 1 (
Set TEST="test"
echo %TEST% )



rem Notice that we can use it now

if 1 == 1 (
echo %TEST% )

pause


Now like me you might expect on the first line to see the output test, but you only see it on the second.



image



The only thing I can reason ( it is just not worth researching, so I am guessing here ) is that because I am in a control structure, if , it has already set up its “stack” as some kind of optimization, so when I creating the TEST variable I am creating in the global stack which is why I can use it later, but this local stack inside the if cannot be updated.



Moral of the story



If you want to set a variable and use it then don’t do in inside a control statement. To make the first example work it would be as simple as :



Set TEST="test"
if 1 == 1 (
echo %TEST% )



Update

You can find more specific details here on SO