Archive for August, 2007

Break not working in ForEach

Kirk has a great summary of a difference between ForEach the PowerShell keyword and ForEach the alias for ForEach-Object cmdlet. Just a few days ago while working on the automated software testing pack I found myself confusing these two and having to debug the code which at first site looked perfectly legit:

# Locate the results entry for the currently selected test
$i = 0
$bNew = $true
$PreviousResults | ForEach {
    if ( $_.Name -eq $currentTest.Name ) {
        $bNew = $false
        break
    }
    $i++
}

Basically, I had to find a record for the test selected to run (records stored in the $PreviousResults array) and have the $i hold the index of the record so I can update it with the record with test results. The code seemed obviou: just go through the records (ForEach), break if the test is found (the name comparison), and increment the counter if not ($i++).

The problem is that this is not a cycle – because ForEach here is actually a ForEach-Object cmdlet that simply calls the block after itself for the items it gets from the pipeline stream. There’s no cycle – so there’s nothing to break (so in my case break was breaking the whole function).

If you need to use break – use ForEach the cycle keyword or just For, like in this code which I used instead:

# Locate the results entry for the currently selected test
$i = 0
$bNew = $true
for ( $i=0; $i -le ($PreviousResults.length - 1); $i++)  {
    if ( $PreviousResults[$i].Name -eq $currentTest.Name ) {
        $bNew = $false
        break
    }
}

Read Kirk’s post for other differences to keep in mind and try using full name for ForEach-Object to make your code more readable.

Dmitry

Tags:

Quick Reference to AD cmdlets 1.0.4

One downside of blogging is that you end up having information scattered through the blog rather than available in a nice well designed document. So I decided to put together a quick summary of the AD cmdlets currently available in v1.0.4.

As a reminder you can download the PowerShell AD cmdlets and full documentation completely free from this site.

So here’s the alphabetical list of the ones we currently have:

Hope this is a helpful summary. Full AD PowerShell documentation can be found here. Also, if you have any questions AD PowerShell discussion forums are the way to go.

Dmitry

Tags: , , , ,

Automated Software Testing with PowerShell and PowerGUI

PowerShell is great for automated software testing. We all know that and some of us even have some experience doing that. However, wouldn’t it be great to have a UI console to manage and run the individual test scripts?

Now you have one!

UI Console for Automated Software Testing based on PowerShell

This PowerGUI-based console allows you to:

  1. Run selected/all/based on criteria test scripts.
  2. See results, last run time, modification time, error messages.
  3. Filter and sort by any of the aforementioned properties.
  4. Add new test scripts.
  5. Edit test scripts.
  6. Copy test scripts to serve as models for new scripts.
  7. Delete scripts.
  8. Produce reports.
  9. Schedule tests for regular automated runs.

You can watch the automated software testing demo and download (absolutely free) the testing console at PowerGUI.org.

Enjoy and let me know if you have any feedback/feature requests!

Dmitry

Tags: , , , , , , , , , ,

PowerGUI 1.0.10 released – What’s New

This actually happened last week but I wanted to wait before blogging to see if any critical issues get reported. 😉 Just kidding – traveling is limiting my ability to blog.

PowerGUI 1.0.10 has been released. As usual you can download the latest version of PowerGUI from the site.

There are a bunch of new features (search folders based on filters is one of the new ones I like) and bugfixes based on the feedback we got from you in PowerGUI discussion forums.
The detailed what’s new list can be found on the feature roadmap page.

Enjoy!

Tags: ,

German PowerShell User Group Launched

Great news for PowerShell users in Germany, Austria and Switzerland – German-speaking PowerShell User Group has officially started. Check out any of the URLs to get to the site: http://www.powershell-ag.de/ps, http://www.powershell-ag.ch/ps, or http://www.powershell-ag.at/ps (isn’t it cool to have the site available on all three domains?).

I actually recall Richard, Scotty, and I sitting with Rolf after the PowerShell dinner at DEC and trying to talk him into starting a user group in Germany. Sounds like we were persuasive enough to make him invest a lot of time and effort to make this happen. This is great! Congratulations to Rolf and Herzlich Willkommen to the rest of us!

The actual face to face meeting is set for September 19th. So if you prefer to read about PowerShell in German – please go to the website and register.

Tags:

Renaming AD Objects

Another new cmdlet in the 1.0.4 release is Rename-QADObject. Here are a few examples of how it works.

Rename-QADObject dsotnikov -NewName "Dmitry Sotnikov"

In this particular case I’ve used the existing name to find the user and then supplied a new one.

Instead of providing a name (or any other identifier such as UPN, Domain\Username, SID, DC, CN, etc.) you can pipe an output of another cmdlet into this one.

For example, to add a postfix to all users from London OU:

Get-QADUser -OU ps64.local/London | ForEach { Rename-QADObject $_ -NewName ($_.Name + "-London"}

One thing to keep in mind is that this command will change the Name attribute as well as distinguished name (DN) and canonical name, but not other related attributes such as samAccountName or UPN. If you need to change more properties use the good old Set-QADUser (or depending on object class another set- cmdlet.)

Tags: , , , , , , ,

New PowerShell Channel 9 Videos

Two new PowerShell videos just went live on Channel 9:

The PowerGUI one is quite fascinating to watch. This was shot in spring and it is just amazing to compare it to what we have in PowerGUI 1.0.9 today.

[UPDATE] Just noticed that they have also posted a conversation of Don Jones and Jeffrey Snover. For a full list of PowerShell-related Channel 9 videos go to this page.

Tags: , , ,

Are you a Poshoholic?

Are you getting obsessed with PowerShell? Well, you are not the first one. Kirk Munro is now the first officially self-declared Poshoholic.

If you feel you are the one as well, here’s a message for you:

Don’t get discouraged. PowerShell is an obsession. It can hit anyone.Young, old. Rich, poor. Black, white.

And it doesn’t matter how long you’ve been scripting or what you’ve been scripting. It’s what PowerShell does to you that counts.

To help you decide whether you might have a problem with your own PowerShell, we’ve prepared these 7 questions. The answers are nobody’s business but your own.

A Simple 7-Question Quiz designed To Help You Decide

1. Do you use PowerShell because you feel it is the coolest thing out there?

2. Do you think of PowerShell whenever you come across any problem or task to solve?

3. Do you find yourself talking about PowerShell with your friends or demoing it to them?

4. Have you begun to scripting in the morning, before school or work?

5. Do you try to make all your scripts oneliners?

6. Did you ever try to stop using PowerShell or using it less — and fail?

7. Does PowerShell make you feel like you are a better IT professional?

If you can answer yes to any one of these questions, maybe it’s time you took a serious look at what PowerShell might be doing to you.

Join the PowerShell newsgroup and the rich PowerShell community to meet other IT professionals facing similar issues. Together we can make our IT life better.

Dmitry

Seriously though, I came to know Kirk when he became the most active member of PowerGUI discussion forums. He is a great guy. His first blog post on how he learned about PowerShell is just hilarious, his post on PowerGUI is also of interest. I am glad he is now blogging and am subscribing to his blog right away!

Tags: ,

Moving AD objects

Another new cmdlet in the 1.0.4 pack is Move-QADObject which allows you to move AD objects: users, groups, computers, OUs within the current domain.

The syntax is pretty easy:

Move-QADObject -Identity object-to-move -NewParentContainer target

Identity is the default parameter so you can skip the switch. You can supply almost anything as the parameter as long is it can identify the object in a unique fashion: samAccountName, DN, canonical name, domain\user, UPN, SID, GUID, etc.

The -NewParentContainer switch has a handy alias -to. The actual parameter is again pretty much anything unique enough. In my opinion DN or canonical name would make sense in most cases.

And the best of that all: the cmdlet accepts pipeline.

Here’s a bunch of examples of how this all works:

# Move a user
Move-QADObject dsotnikov -to qsft.local/employees/cto

# Move by location
Get-QADUser -City London | Move-QADObject -to qsft.local/employees/london

# Move all disabled account
Get-QADUser -Disabled | Move-QADObject -to qsft.local/disabled_accounts

# Use csv: first row is "object,target",
# then each column has comma-separated object and container identities

Import-CSV c:\tomove.csv | ForEach { Move-QADObject $_.object -to $_.target }

# Move groups, computers, etc.
Move-QADObject qsft\researchers -to qsft.local/groups

Get-QADComputer ny* | Move-QADObject -to qsft.local/ny/computers

# Move OU (with all objects and subcontainers)
Move-QADObject qsft.local/users/london -to qsft.local/employees/eu/uk

So next time you have to completely reconfigure your AD domain you know what to do, right? 😉

Dmitry

Tags: , , , , , , ,

AD cmdlets 1.0.3 to 1.0.4 upgrade

If you have AD cmdlets 1.0.3 already installed and are moving to 1.0.4 now there is a gotcha to keep in mind:

To make sure that the upgrade works fine it is highly recommended that you uninstall 1.0.3 before installing 1.0.4.

I’ve seen on some of computers upgrade kind of going – you can successfully finish the setup, etc. – but then when you try to actually use the cmdlets they would not run. Clean installation seems to work just fine in all cases.

Sorry for the inconvenience.

Tags: , , , , , ,


My Recent Tweets

Legal

The posts on this blog are provided “as is” with no warranties and confer no rights. The opinions expressed on this site are mine and mine alone, and do not necessarily represent those of my employer - WSO2 or anyone else for that matter. All trademarks acknowledged.

© 2007-2014 Dmitry Sotnikov

August 2007
M T W T F S S
 12345
6789101112
13141516171819
20212223242526
2728293031  

%d bloggers like this: