Tuesday, February 15, 2011

Watching PS Vars for changes - Events

I have a Testing framework built around Powershell and .net. In the framework there are a couple of main objects that their property values are assigned to Globals that are well know and used. The problem was I did not want to make them read only as this causes issues if reloading the framework and or trying to change paths. I needed a way to report back the value of a variable if it changes so i could keep the objects in sync. First I used Set-PSBreakPoint –Action to Check the value but it breaks before the value is set. Then I thought I need an Event but Powershell has limited native events. So I just started tweeting and thanks to @oising I finally have a solution. Here is a simple example that can be built upon.

 

#Set value to a new variable
$n = 0

#set A break point to watch for any write actions to that variable
#this will fire a new event VariableChanged
$BP = Set-PSBreakpoint -Variable n -Mode Write -Action {
New-Event -SourceIdentifier VariableChanged
}

#Now register to recieve the VariableChanged events
#Here we just check the value of our variable and get its new value
$EE = Register-EngineEvent -SourceIdentifier VariableChanged -Action {
Write-Host $n -ForegroundColor Yellow
}

#assign a new value to n and see the vent fires
$n = 1

#do some cleanup
Get-PSBreakpoint | Remove-PSBreakpoint
Get-EventSubscriber | Unregister-Event -Force
Get-Event | Remove-Event


 



Good-Times | Get-Command –:)

Thursday, January 27, 2011

PowerShell Plus 4.0 beta

Idera has released a Beta for the 4.0 version of Powershell Plus.

PowerShell Plus 4.0 beta introduces a completely redesigned UI so you can code faster, debug more easily and quickly access your favorite features.

  • Redesigned UI puts the most used features at your finger tips
  • Work more efficiently by customizing the UI to meet your specific needs
  • New Start Page links to educational resources and a customizable news feed
  • Syntax Error List helps you identify errors at a glance for faster debugging

 

Register and Download it Here.

Powershell Plus 4.0 Beta

Tuesday, January 4, 2011

Solved: Powershell fails as a functional language if commands are in modules.

 

the issue comes down to the scope that the scriptblock being passed into the function gets bound. In the original example the scriptblock was being bound to the callers scope which can not access anything in the module except what was imported into the scope. The fix turns out to be explicitly binding the scriptblock to the modules scope. See the fixed example below. Thanks go out to @Oisin for the fix.

New-Module -Name 'Testing' -ScriptBlock {
function TestOuter{
param(
[
string]$Name,
[
scriptblock]$TestScript
)
function TestInner{
param(
[
int]$Count
)
Write-Host $Count
}
Write-Host $Name
$Mod = Get-Module Testing
& ( $Mod.NewBoundScriptBlock($TestScript) )
}
}
| Import-Module -Force -Global
TestOuter -Name "Paul" -TestScript { TestInner -Count 10 }
Remove-Module Testing -Force

Powershell fails as a functional language if commands are in modules.

I have some functions that contain functions. When I put the Functions in a .ps1 file and Dot Source directly the behave great but if those functions get imported in a module they fail to find the inner function with a CommandNotFound error. It does not matter if the functions are directly in a .psm1 file or if they are dot sourced by the .psm1 file they still fail to find the inner functions. Here are some code examples. Why is this? You can copy the below code into Powershell Console or ISE and run to see the errors.

function TestOuter{
param(
[
string]$Name,
[
scriptblock]$TestScript
)
function TestInner{
param(
[
int]$Count
)
Write-Host $Count
}
Write-Host $Name
& $TestScript
}
TestOuter -Name "Paul" -TestScript { TestInner -Count 10 }


New-Module -Name 'Testing' -ScriptBlock {
function TestOuter{
param(
[
string]$Name,
[
scriptblock]$TestScript
)
function TestInner{
param(
[
int]$Count
)
Write-Host $Count
}
Write-Host $Name
& $TestScript
}
}
| Import-Module -Force -Global
TestOuter -Name "Paul" -TestScript { TestInner -Count 10 }

Friday, September 10, 2010

Save HTML as DOCX

the other day we had a need to save an HTML file as DOCX. We are creating the HTML file using ConvertT0-HTML. The file contains data that needs to be saved in SharePoint and needs to be editable by others. We decided to save as a DOCX because those in management all have WORD installed but don't have a  Dedicated HTML editor. So here is the Code Enjoy.

!!! YOU MUST HAVE WORD 2007 or 2010 INSTALLED !!!!!

function ConvertTo-DOCX{
param($In,$Out)

#int the the enum returns for DOCX format
$SaveAs = 16

#Create WORD object and perform conversion
$Word = New-Object -ComObject Word.Application
$OpenDoc = $Word.Documents.Open($In)
$OpenDoc.SaveAs($Out,$SaveAs)
$OpenDoc.Close()

#stop all WORD instances
Get-Process | Where-Object { $_.Name -eq "WINWORD" } | Stop-Process -Force
}

Monday, May 17, 2010

5th Silicon Valley Code Camp Oct 9th and 10th 2010

CodeCamp at FootHill College.  Click Here for Details and Registration

I will be giving a session “Advanced Scripting with Powershell” and My co-worker SPowser will be doiing “Powershell Modules” come and enjoy, Its free and lunch is provided. It will only cost you a weekend but why not hang with geeks and further you knowledge.