Archive for June, 2008

Summer PowerShell Olympics

Scripting Challenge logoJust like Scripting Games became real PowerShell Olympics this February, this summer – in fact tomorrow – starts the Summer Olympics – PowerPack Challenge 2008.

In a nutshell, all you need to do – is create a PowerPack (PowerGUI extension), export it, and post it to the PowerGUI library. This is easy and fun (watch this online tutorial) and you can compete for a bunch of prizes (sponsored by Quest). The jury includes Jeffrey Snover (!), Marco Shaw, Hal Rottenberg, Kirk “Poshoholic” Munro, and myself.

You can read more at the contest page. Time to have some serious fun here!

Tags: , , ,


PowerShell script in a .bat file

How do you put PowerShell code into a batch/cmd file without having to also have a separate .ps1 file? (If you can have an external .ps1 file – you just invoke powershell.exe and supply the path to the script file as parameter.)

I got this question recently from one of our field guys and thought I would blog about the trick publicly.

The problem is that PowerShell syntax can obviously have elements that .bat files cannot stand, and that you cannot pass multiline script as powershell.exe parameter.

There are actually a couple of ways to do so:

1. Encode the script:

As Bruce points here PowerShell has the -EncodedCommand parameter, which lets you pass any PowerShell code as base-64-encoded string.

So if you have some sort of script, like this:

#iterate numbers 1 through 10
1..10 | foreach-object {
# just output them
"Current output:"

You simply (in PowerShell command-line or script) put it in curcly brackets and assign (as scriptblock) to a variable:

$code = {
    #iterate numbers 1 through 10
    1..10 | foreach-object {
    # just output them
    "Current output:"

Then use this PowerShell command to get the encoded version:


Then copy/paste the output of the command to your batch file:


2. Keep the code as PowerShell but turn it to a string:

If the first approach for whatever reason does not work for you (e.g. you care about readability), you can try to flatten the script and pass it as a string:

  1. Take the PowerShell script.
  2. Remove all the comments ( everything that starts with #).
  3. Put ; at the end of each line.
  4. Remove all line breaks.
  5. Supply the string you get as the -command parameter for powershell.exe.

The reason for all of this is that powershell.exe (the executable which allows you to run any PowerShell code allows you to either start an external .ps1 script file (which often creates additional complexity of having to maintain and ship 2 files) or execute a single line of PowerShell code as the -command parameter. Hence the requirement to flatten the script and turn something like this:

#iterate numbers 1 through 10
1..10 | foreach-object {
# just output them
"Current output:"


powershell.exe -command '1..10 | foreach-object { "Current output:"; $_; }'

See also this blog post by MoW on making PowerShell pass its exit code to command files.

Tags: , , ,

Taming SharePoint

As we all know SharePoint does not have a native set of PowerShell cmdlets – which does not mean that PowerShell crowd does not want to manage it with PowerShell. 😉 If you check out PowerGUI library you will find 4 (!) powerpacks (as usual with all PowerShell code behind!) for SharePoint management:

  • SharePoint powerpack which list the SharePoint Webs, folders, and List, you can also monitor the SharePoint usage.
  • SharePoint farm PowerPack allows you to display the properties of the local farm, get a list of servers running in your farm, list SharePoint services and monitor the status and which services needs upgrade, and list the web services and web applications.
  • SharePoint Navigator PowerPack comes with full site navigation through the sites, webs, going down to folders and browsing through the files, viewing document properties and versions.
  • SharePoint remote access powerpack (browsing lists, document libraries, users, groups, roles, and supporting file and folder deletion, allows remote access to a Sharepoint machine.

The first three were submitted by Ayman Wassif (and are using .NET API) and the last one by Jamie Redman (and illustrates remote management via web services!). Check them out – you will learn quite a few tricks and will get a nice extensible SharePoint management tool for your day-to-day administration.

And please rate them and add your comments to the library entries. I know that these are very important to anyone posting their packs to the library!

Tags: , , ,

PowerGUI TechEd Demo

As promised here is the demo I was doing in Jeffrey’s CTP2 talk at TechEd 2008.

The session was on PowerShell v2 and Jeffrey asked me to demo the new capabilities which v2 gives to UI applications. We selected background execution and eventing as the demo features.

The demo was very simple (in fact, thanks to the transparency of these features in v2 surprisingly so). I took the AD user provisioning action (which is a script action, and a fairly long and complicated one), used it on this scv file: presidents.csv

The provisioning took 20 seconds or so and because the execution was synchronous the UI was basically locked until the last presidential account was there. Not good!

But luckily the machine had PowerShell v2 installed so we went ahead and made this script a…

Background job

To do that, I opened the script (I was actually copy/pasting the script to a new Script Action), and added Start-PSJob as the first line, and then enclosed the whole script into a here-string parameter for the command:
Start-PSJob -Name Provision -Command @’
# here goes my script

Then I jut ran the new action the the UI was back right away! Meanwhile the whole provisioning was being done in the background!

I added a node to the PowerGUI console which was executing Get-PSJob and allowed me to see the status and results of any background jobs I had.

However, obviously another change made more sense – changing the system so the script would just run automatically whenever the CSV file with the information on the new AD accounts to be provisioned appears at a specific location. So we re-did the script – this time using


To do this we enclosed the script in a scriptblock (don’t ask me why scriptblocks are required for this one and strings for the previous one – I guess just different people architecting the features ;)) and added the commands creating a file system watcher and registering it with the new Register-ObjectEvent cmdlet:
$action = {
# here goes my script

$watcher = New-Object System.IO.FileSystemWatcher
$watcher.Path = c:\temp2
$watcher.Filter = *.csv
$watcher.EnableRaisingEvents = $true
Register-ObjectEvent $watcher Created -SourceIdentifier FileSystemWatcher.Created -Action $action

To check out all the triggers I had in place I created a Triggers node in PowerGUI using Get-PSEventSubscriber to show them all.

Now I copy/pasted my CSV to the folder the trigger was watching and all the accounts got provisioned!

Very simple!

As a bonus section here are a couple of tricks I used:

  1. I added Add-PSSnapin to the beginning of the script before doing the demo. This was to workaround the issue with background jobs not loading PowerShell profiles and not inheriting the loaded snapins from the current session.
  2. I hardcoded the path to the CSV file so I did not have to type the path each time I started the action, and did not have to make slightly more advanced things in the scripts (i.e. passing the parameter to the subscriber action and background job).

Apart from that – no modifications were required! I could just re-use my scripts in these new scenarios!

Tags: , , , ,

OCS Management on Steroids

PowerPack Main Tree ScreenShotIf you have Office Communications Server 2007 there’s a new must have tool for you: OCS powerpack for PowerGUI just posted by Curtis Johnstone: it is free, totally based on PowerShell (you can see the PowerShell code behind each and every node, link, or action), and has a lot of functionality not present in the native OCS MMC. Just see the console tree screenshot to the left!

Here’s a quick walkthough of the key features of the pack:

View and Provision OCS settings on users:

  • View all AD details for OCS users
  • Use canned filters to quickly view which users are enabled /disabled for key OCS functionality.
  • Enable/Disable key OCS functionality (e.g. Public IM)
  • Email OCS Assigned and Unassigned Users

Remotely View key Edge settings (from an OCS Administrative Tool or Front-End computer), including:

  • Internal and External Edge server settings
  • Federated Partners and Federated Public IM Network Settings
  • Can use Alternate Credentials to access Edge role

Easily access and view all OCS Global Settings in one location, including:

  • Pools,
  • Enterprise Voice and Meeting Settings and Policies,
  • Location Profiles,
  • Normalization Rules,
  • Archive,
  • CDR,
  • Federation.

On any local OCS machine:

  • View and Control OCS Services
  • View OCS specific Event Log Errors and Warnings
  • Provision Address Book settings that not available in the OCS Administrative console

And a few more screenshots to keep you excited:

Global Settings

  • Allows the administrator to see key UC and Enterprise Voice Global Settings in customizable views.
  • Can easily generate reports through PowerGUI

Global Meeting Policies


  • Allows quick customizable views and reporting of OCS enabled users and all pertinent OCS properties.
  • Default Filters to quickly see which users are provisioning for certain OCS features (e.g. Federation, Public IM, etc…)
  • Additional links to get Voice, Meeting and AD details for any user(s).
  • Actions are available to provision basic OCS features (e.g. Remote Access, Public IM, …)

OCS Users Management

Edge and Federation

  • Easily see all important edge and federation configuration settings in one tree.
  • All internal and external IP addresses and ports are shown.
  • All federation settings including partners and remote edge server peers.
  • Avoids having to remote into the Edge server and start the Edge server Administrative console to see this information.
  • Certificate information to be added in the future.

OCS Public IM Management


  • See which servers and their corresponding roles are installed in your OCS deployment.
  • For the local OCS Server (soon to be any OCS server), see OCS specific Service information and Event Log information and take actions.
  • View Address Book settings and take actions that are not available through the native OCS Administration console.

Installed OCS Server Roles:

Installed OCS Server Roles

OCS Specific Services and Event Log Entries and Actions:

OCS Specific Services and Event Log Entries and Actions

Address Book Information and Actions:

Address Book Information and Actions

This is the most amazing example of the community work I have ever seen. Curtis took the PowerShell scripts which James O’Neill and others created for the Microsoft® Office Communications Server 2007 Resource Kit companion CD, enhanced them, plugged them into PowerGUI and made them available for everyone else to use (and enhance further ;))

You can download the OCS PowerPack here. By the way, please do leave your comments for Curtis at the powerpack page – I know he is anxious to hear some feedback!

Tags: , , ,

Google Gadgets

Check out Steve’s Script Loop Timer PowerPack in PowerGUI’s extensions library. The pack basically takes your PowerShell script and runs it multiple times in a row, which can be handy when testing scripts or cmdlets to see if they tend to become slower over time.

However, the most interesting part is how he is producing the output as a nice graphical chart:

Screenshot of Google charts used from PowerGUI

Turned out (just right-click his node, select Properties, and see the code) that Steve is using free Google charts simply opening an IE window, navigating to a URL, and passing the data points to be displayed.

Sounds like an alternative for those who do not have PowerGadgets installed. Now, if someone could take Steve’s code and create a bunch of general purpose out-* functions for everyone to use…

Tags: ,

Full AD Recovery Cmdlets

I had a few questions after I blogged about the tombstone reanimation cmdlets last week, so here are a few quick answers:

  1. Yes, tombstone reanimation has its limitations – mainly it does not bring back most of the object attributes (including group membership – see the full list in Gil’s article here).
  2. Yes, there is a full set of cmdlets to comprehensive AD restores which don’t have the limitation.

The cmdlets come with Quest’s Recovery Manager for Active Directory. They allow you to do a lot of advanced stuff:

  • Restore any AD object with all attributes or a subset of them.
  • Compare any two backups and get a full list of what changed.
  • Compare live directory against a backup.
  • Perform all of this for AD or ADAM (ADLDS or whatever it is called these days).
  • Manage the backups, backup schedules, and so on.

Here’s the full list of the cmdlets with a quick description:

Compare-RMActiveDirectoryObject – compare two backups or compare a backup against live directory.

Restore-RMActiveDirectoryObject – restore deleted or changed objects or any of their attributes.

Get-RMDeletedActiveDirectoryObject – enumerate deleted AD/ADAM objects
Restore-RMDeletedActiveDirectoryObject – reanimates a tomstone for selected AD/ADAM objects.

Start-RMReportViewer – display graphical window with comparison or restoration report.

Start-RMBackup – do a backup right now (as opposed to scheduled backups you manage with RMCollection cmdlets).

New-RMCollection – create a new collection of domain controllers or ADAM servers to do automated backups.
Get-RMCollection – enumerate currect backup collection settings.
Set-RMCollection – change backup settings for a collection (schedule, retention policy, credentials, and so on.)
Remove-RMCollection – deletes a backup collection.
Rename-RMCollection – obviously changes the collection name.
Add-RMCollectionItem – add another DC or ADAM instance to the collection.

Get-RMSession – get information on all or select backup sessions in the past, their results, and so on.

Get-RMBackup – enumerate backups allows you to find a backup to particular DCs/servers, get the latest one or a backup for a particular date, and so on.

Get-RMBackupContent – see what is in a specific backup.

Add-RMBackup – register a backup in Recovery Manager so it can be restored later on.

Export-RMBackup and Import-RMBackup – allow you to move backup configurations between Recovery Manager installations.

Note that these cmdlets are not free. They require a license for Recovery Manager for AD which is a commercial software. You can get a trial license from the product web page, or apply for the MVP license (using the URL from microsoft.private.mvp.3rdpartyoffers, see the MVP Private Newsgroups instructions at

Tags: , , , ,

Breakthrough Product of TechEd 2008

At the TechEd 2008 in Orlando PowerGUI has received the highest award of the show – The Breakthrough Product of the show.

Here’s the award description:

Breakthrough Product

This award is for the best single product of the Tech•Ed 2008 IT Professionals, and could be from any IT Pro award sub-category.

I am super-excited. This is an incredible achievement for the team, and frankly for the whole PowerGUI and PowerShell community. Without you guys providing all the feedback and feature requests, contributing your PowerPacks to the library, localizing us to every language in the world, and simply spreading the word we would not have been where we got. Love you all! 🙂

You can find the list of winners in various subcategories here.

Tags: , , , , ,

Dmitry Discusses PowerGUI on ITTV

 Dmitry Sotnikov Discusses PowerGUI with Jeff JamesYesterday at the TechEd we sat for a few minutes with Jeff James from Windows IT Pro magazine and the new ITtv channel they have just launched and discussed PowerGUI and PowerShell for a few minutes. And I have just got a note from Jeff saying that the video is already posted on their website. That’s really fast!

Check it out here: Dmitry Sotnikov Discusses PowerGUI with Jeff James.

Tags: , , , ,

PowerShell UK Usergroup Meeting

Richard has just posted the details of the UK usergroup meeting next Tuesday, June 17. This one will be a very special for me because I could finally arrange my traveling schedule to actually be there in person.

I am planning to demo what is coming in PowerGUI 1.5.1 and the RTM of AD cmdlets 1.1, and will talk about the general philosophy behind the projects, and longterm directions.

Would be great if you can make it and be there in person – I would really love to see you face to face. Richard hopes to get a LiveMeeting set up as well – but if you are based in UK I really hope you can make it to the meeting in person.

Tags: , , ,

See Richard’s blog for all the details: time, address, directions, etc.

My Recent Tweets


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

June 2008

%d bloggers like this: