Saturday, June 28, 2008

This little bit of usefulness in powershell is also available in various guides and books, but I often find that technical documentation is written to be more complete than practical.  I will scan a long document full of reference data to assemble what I need to do which can really be communicated in a simple example.  So here's a few easy ways to check to see if a registry entry exists or to look up a value.  These are really handy for testing and auto configuring machines on your network.

# The Test-Path cmdlet is handy for detecting not only if files exist in the file system, 
# but reg key entries as well
#
# $QuickLog will get a $true or $false
#
$QuickLog=(Test-Path 'HKCU:\Software\JQL - www.jql.co.uk\Quick Log')
 
# This returns an array of all the values at this key
$regObj = get-item 'HKCU:\Software\JQL - www.jql.co.uk\Quick Log'
 
# This is a path for using with set-ItemProperty
$path = 'HKCU:\Software\JQL - www.jql.co.uk\Quick Log'
 
# Notice we use the object here
$keys = $regObj.GetValueNames()
foreach($key in $keys)
{
    # Do what you will with your reg keys
    if($key.Contains("Hints"))
    {
        # Set the execution policy so we can write here
        set-itemproperty -Path $path -Name ExecutionPolicy -Value unrestricted
        
        # This assumes you have a String property
        # Notice that we use the path and not the object here
        set-itemproperty -path $path -name $key -value 0
    }
}
Saturday, June 28, 2008 1:26:11 PM (Central Standard Time, UTC-06:00)
Oh this is hilarious.  I'm playing with Google AdSense on my blog and it being a new generator, it has decided to place ads for black singles based on my last name.  Now that's entertaining.  I'm curious to see if they will hone in on something more appropriate, so bear with my experimentation here.

Saturday, June 28, 2008 12:35:58 PM (Central Standard Time, UTC-06:00)
 Monday, June 16, 2008

I've been looking all over trying to find a smart way to create nested XML in powershell, and after combining a few sources I have come up with something that works pretty well.  Now I don't recommend actually doing it this way unless you have to.  This is a fairly laborious process that I'm undertaking only because my particular situation requires that I do not use compiled code.  A C# object model using the Linq XML serializer is far easier, consistent, and less prone to bugs than this.  However sometime you just have to write a hack.

# Create the results file
$output = $args[0] + "\results.xml"
$outxml = New-Object "System.Xml.XmlDocument"
$outxml.LoadXml("<?xml version=`"1.0`" encoding=`"utf-8`"?><Results xmlns:xsi=`"http://www.w3.org/2001/XMLSchema-instance`" xmlns:xsd=`"http://www.w3.org/2001/XMLSchema`"></Results>")
 
# Tests
$elem = $outxml.CreateElement("Tests")
$outxml.get_ChildNodes().Item(1).AppendChild($elem)
 
# Test
$elem = $outxml.CreateElement("Test")
$elem.SetAttribute("xsi:type","MemoryTest")
$outxml.SelectSingleNode("Results/Tests").AppendChild($elem)
 
# Name
$elem = $outxml.CreateElement("Name")
$elem.PSBase.InnerText = "Video"
$nsMgr = New-Object System.Xml.XmlNamespaceManager($outxml.get_NameTable())
$nsMgr.AddNamespace("type", "MemoryTest")
$outxml.SelectSingleNode("Results/Tests/Test", $nsMgr).AppendChild($elem)

 

Notice the use of the XmlNamespaceManager.  This allows you to have multiple Test nodes within Tests each with a different type.  This makes sure you can add elements to the right trees. 

Again, this is the grunt work of XML serialization that lots of programmers end up doing, but really shouldn't have to.  Go use the Linq serializer.

Monday, June 16, 2008 10:01:53 PM (Central Standard Time, UTC-06:00)