Tuesday, November 29, 2011

Powershell and JScript Issue

So I was reading PowerShell ABC's - J is for JavaScript and it talks about embedding JavaScript into a PowerShell script and thought I could write something similar.


Here is my code and for some reason it is not working.  I can execute the JavaScript in a JavaScript console and it works just fine.  Any help would be much appreciated.


EDIT: The error i was getting
Exception calling "ISODate" with "0" argument(s): "Object doesn't support this property or method"


EDIT: So apparently this throws an error on 64bit machines, I first ran this on WinXP:

Retrieving the COM class factory for component with CLSID {0E59F1D5-1FBE-11D0-8FF2-00A0D10038BC} failed due to the following error: 80040154.

Monday, April 11, 2011

Create a hashtable using an objects properties

A few blog posts ago I put up some code that created a custom object with properties from a hashtable, now I am wondering, how would you do the opposite?  How would one create a hashtable using an objects properties?  Here is the code from the other post.  Basically what I am trying to do is figure out a way to iterate through the properties of an object, and thought this would be one way to do it, I am sure there is another more simple way to do it. 

Thursday, April 7, 2011

Wednesday, March 16, 2011

First to convert this to a #Powershell one-liner wins a high five



# Take an IP address as a string, increment the last octet by one and return the string

$array = ("192.168.1.1").split(".")

[int]$array[3] = ([int]$array[3]) + 1

$IP = $array -join "."

# Can it be done?

Thursday, September 23, 2010

Comparing XML files with Select-XML and Compare-Object

My situation: 80+ automated powershell tests scripts that take 6 hours to run, all of which make changes to an XML config file, and don't always restore the config when the test is finished, causing other tests to fail.

My solution: I have a script that runs before any testing is done and after every test script that saves the config file to a directory, with a time stamp in the name. All I have to do now is compare the first XML, which I know to be good, with the rest. This is what I came up with:

# Get all the XML's that are saved and sort by CreationTime
$xmls = Get-ChildItem -Path "C:\RESULTS\logs\*config.xml" | Sort-Object -Property CreationTime

# Get the first out of the list and create an XML object
[xml]$first = Get-Content -Path $xmls[0].fullname

# Use the Select-XML and the specified xpath to get all the nodes under the \\Config node
# this can be changed howerever you want, depending on how your XML is set up
$base = Select-Xml -Xml $first -XPath "//Config/*"

# Get loopy
# Loop through the XML's, cast each as an XML object, then compare to the nodes of the first
$xmls | ForEach-Object {

[xml]$temp = Get-Content $_.fullname

Compare-Object -ReferenceObject $base -DifferenceObject (Select-Xml -Xml $temp -XPath "//Config/*")

}

Have fun and Get-Command!!

Friday, September 17, 2010

[Not Working] Set-PSBreakPoint when a variable becomes $false

So I'm looking at a script from the Windows PowerShell Cookbook by Lee Holmes call Enable-BreakOnError and I want to do something similar by using Set-PSBreakPoint on a variable but only break when the variable is set to False. Here is Lee's function:

#############################################################################
##
## Enable-BreakOnError
##
## From Windows PowerShell Cookbook (O'Reilly)
## by Lee Holmes (http://www.leeholmes.com/guide)
##
##############################################################################

<# .SYNOPSIS Creates a breakpoint that only fires when PowerShell encounters an error .EXAMPLE PS >Enable-BreakOnError

ID Script Line Command Variable Action
-- ------ ---- ------- -------- ------
0 Out-Default ...

PS >1/0
Entering debug mode. Use h or ? for help.

Hit Command breakpoint on 'Out-Default'


PS >$error
Attempted to divide by zero.

#>

Set-StrictMode -Version Latest

## Store the current number of errors seen in the session so far
$GLOBAL:EnableBreakOnErrorLastErrorCount = $error.Count

Set-PSBreakpoint -Command Out-Default -Action {

## If we're generating output, and the error count has increased,
## break into the debugger.
if($error.Count -ne $EnableBreakOnErrorLastErrorCount)
{
$GLOBAL:EnableBreakOnErrorLastErrorCount = $error.Count
break
}
}
Seems simple enough. So I set up a script to test my idea, and I cannot get it to work at all. Here is how I am testing this:

$Value = $True

Set-PSBreakpoint -Variable Value -Mode Write

$Value = $False
$Value = $False
$Value = $true
$value = $true

Get-PSBreakpoint | Remove-PSBreakpoint

So in using -Mode Write, this stops execution immediately before a new value is written to the variable, so it stops every time. This does not work for me because I need to know what $value is being set as, but it breaks before it is set. Is there a magical way around this?