Post from March, 2009

Return values gotcha

Tuesday, 17. March 2009 20:00

A while ago my colleague Martin Zugec posted about a Gotcha concerning return values from subroutines. Read the full story here:

http://martinzugec.blogspot.com/2008/08/returning-values-from-fuctions-in.html

Category:Gotcha, PowerShell | Comment (0) | Author: Dennis Damen

Assign Citrix policies to servers

Monday, 16. March 2009 14:45

In our scripting framework basically every server takes care of it own configuration. This is also the case when managing Citrix policies. The policies themselves are imported and exported for DTAP and disaster recovery purposes but the server assignment is done by each server itself according to the role of that particular server.

[...]

Category:Citrix, PowerShell | Comments (2) | Author: Dennis Damen

List available COM objects

Monday, 16. March 2009 8:32

A nice oneliner I found here will list all available COM object for your machine:

gci HKLM:\Software\Classes -ea 0| ? {$_.PSChildName -match ‘^\w+\.\w+$’ -and (gp “$($_.PSPath)\CLSID” -ea 0)} | ft PSChildName

Category:General, PowerShell | Comment (0) | Author: Dennis Damen

How To Reset A Wireless Network Adapter?

Sunday, 15. March 2009 10:55

I switch between wireless networks frequently. Usually I am facing issues with getting connected which get resolved by clicking around in Vista’s Network and Sharing Center. As this takes always a couple of minutes I want a PowerShell script that I can use to reset my wireless lan adapter directly.

This can be accomplished using WMI:

# NAME
#     Reset-Wlan.ps1
#
# SYNOPSIS
#     Disables, pauses, and then enables the wlan adapter
#
# SYNTAX
#     .\Reset-Wlan.ps1
#
# REMARKS
#     Run this script elevated
#
# AUTHOR
#     Frank-Peter Schultze www.fpschultze.de
#
# DATE
#     15-Mar-2009
#
$wlan = Get-WmiObject -Class Win32_NetworkAdapter -Filter "Name LIKE '%wireless%'"
$wlan.Disable()
# Maybe waste of time. Never mind!
Start-Sleep -Seconds 10
$wlan.Enable()

Category:Scripting, Windows PowerShell | Comment (0) | Author: Frank-Peter

Windows PowerShell In Action, Second Edition

Sunday, 15. March 2009 0:10

Windows PowerShell In Action, Second Edition, updated for PS V2 by PowerShell language designer Bruce Payette, is expected around September 2009.

For all those, who cannot wait: browse to http://manning.com/payette2/ in order to read the chapters of the Early Access Edition. Currently, four chapters are available (the first chapter is free)

Category:Scripting, Windows PowerShell | Comment (0) | Author: Frank-Peter

NameTranslate using PowerShell

Saturday, 14. March 2009 17:01

I had a need for translating Active Directory object names from any to any form. For instance lab\administrator to cn=administrator,cn=users,dc=lab,dc=local and back. I remembered having seen a cool function called NameTranslate somewhere to see if I could use it somehow.

When searching the internet I found some vbscript code which created an object called “Nametranslate”. Since I absolutely hate vbscript I decided to translate this script into a native PowerShell script. First step was to create a new ComObject like so:

[...]

Category:Active Directory, PowerShell | Comments (4) | Author: Dennis Damen

Casting Gotcha!

Friday, 13. March 2009 16:56

Some things may seem a little strange when working with PowerShell when coming from other scripting languages and especially when coming from a development background. What I noticed today in one of my scripts took me quite a while to troubleshoot.

The problem concerned a comparison between two values using “-eq”. I retieved a value from a subroutine and compared it to a string value. The code looked something like this:

$returnValue = Somesubroutine(xxxx…
if ($returnValue -eq “xxxxxxx”) { do something }

Because of an error in my subroutine the returning value was a boolean instead of a string. Still I wondered why the “do something” was executed. I mean, $True can’t possibly equal “xxxxxxx” right? Try the following on your commandline:

if ($true -eq “blahblahblah”) {write-host “They are the same”}

Have you tried it? The text “They are the same” will actually be written to the console! How weird is that?

PowerShell has a very powerfull typing system. This typing (or casting if you will) will automagically convert the type of an object to an other type if needed. However, PowerShell should only do this IF NO INFORMATION IS LOST.

In this case I’m not sure what is happening. Either the text “blahblahblah” is actually converted to a boolean and since it exists it will be converted to $true. Or, the if statement encounters a “$true” and will then immediatly yield true and execute the command ‘write-host “They are the same”‘.

Whatever is the case: GOTCHA!

Category:Gotcha, PowerShell | Comments (2) | Author: Dennis Damen