Post from April, 2010

Scriptbox – the Windows Script Library

Wednesday, 28. April 2010 16:51

By chance (meaning by googling something) I stumbled over a script library called Scriptbox, and I found a really useful script there. It’s a huge resource! With about 6.500 vbs, js, and ps1 scripts I consider it one of biggest libraries ever.

Category:Scripting, VBScript, Windows PowerShell | Comments (1) | Author: Frank-Peter

What Is A PowerShell Loop Label?

Wednesday, 21. April 2010 14:37

One of these days, while analyzing a PowerShell script, I noticed something strange that I didn’t see before: a so-called Loop Label.

Loop labels allow you to name a looping statement (like for, foreach, or while) and to specify that name with a continue or break statement in order to instruct PowerShell to skip the rest of the current iteration (continue) or completely halt the execution (break) of that specific loop.

When does a loop label make sense? Loop labels are only useful with nested loops meaning that if you want to halt the outer loop execution within the inner loop.

Take a look at the code below. The inner loop will be halted as soon as the inner loop counter $j has reached the value 2:

for ($i = 0; $i -lt 5; $i++) {
  for ($j = 0; $j -lt 5; $j++) {
    if ($j -eq 2) {
      break
    }
    Write-Host "outer loop counter = $i, inner loop counter = $j"
  }
}

If you want to halt execution of the outer loop instead you need to define and specify a loop label as follows:

:outerloop for ($i = 0; $i -lt 5; $i++) {
  for ($j = 0; $j -lt 5; $j++) {
    if ($j -eq 2) {
      break outerloop
    }
    Write-Host "outer loop counter = $i, inner loop counter = $j"
  }
}

Category:Scripting, Windows PowerShell | Comments (1) | Author: Frank-Peter

Must-have for GPO handling

Tuesday, 20. April 2010 13:00

This small webpage is perfect in case you plan to do something with Group policy objects:

http://gps.cloudapp.net/

Just wanted to share ;)

Martin

Category:Note, Tools, Uncategorized | Comment (0) | Author: Martin Zugec

Default Import-Module behavior

Saturday, 10. April 2010 21:31

Ok, we all know modules in Powershell 2.0 are very cool… For me, probably the best of all newly introduced concepts is namespace support.

To make long story short – if I import function Get-Command from module SCCM, I can access it using namespace – SCCM\Get-Command.

Looks really pretty, is secured and you can use consistent function names in different modules, which is perfect from usability perspective.

You don’t need to use namespaces if you don’t want to however. So you can either use SCCM\Get-Command OR Get-Command.

Problem is that when you use Import-Module, last one always wins. So my SCCM\Get-Command still works fine, however of course it will overwrite Get-Command.

I think this is extremely dangerous behavior. If you also thing it should be changed, please vote here. My proposal is that Import-Module should get switch that will identify that all exported objects are accessible by specifying namespace also.

As a workaround, you can use following code:

Get-Command -CommandType cmdlet | ForEach-Object {Set-Alias -Name $_.Name -Value "$($_.PSSnapIn)\$($_.Name)"}

Insert this code at beginning of your script and it will take care that you won’t be able to accidentally overwrite existing cmdlets (aliases always wins).

 

Martin

Category:PowerShell, Scripting, Windows PowerShell | Comment (0) | Author: Martin Zugec