Archive for February, 2011

Get add-ons right in Script Editor

One of my favorite features in PowerGUI 2.4 is the add-on finder in PowerGUI Script Editor.

As you probably know, Script Editor has tons of extremely useful add-ons with great additional features: from script signing to built-in help explorer. However, you used to have to install the add-ons manually – and this was a bit of a hassle. Now, all you need to do is:

1. Go to Tools / Find Add-Ons Online,

2. Enter keyword to do a search (or just click Search to see everything),

3. Select the add-on you want and click Install.

That is it! Hopefully this makes your scripting experience even more exciting. 🙂

Register for PowerShell Deep Dive

PowerShell Deep Dive 2011 Conference LogoIn case you have not seen the announcement yet, PowerShell team and community is putting together the first ever PowerShell conference, April 17-19 in Las Vegas!

This is going to be the first and most concentrated PowerShell conference ever with a dozen sessions on various PowerShell topics likes remoting, proxy functions and so on, plus a bunch of sessions on managing various platforms with PowerShell: Exchange, SharePoint, Office 365, Forefront Identity Manager, Hyper-V and so on.

These sessions will take place on Monday and Tuesday (April 18 and 19). For those who can come a day early, on Sunday (April 17) morning there will be a pre-conference hands on PowerShell bootcamp – 4 hours of intense PowerShell lab training to get you up to speed before the advanced topics kick in on Monday.

And there is going to be a scripting night party on Monday night.

And direct access to the key members of the PowerShell team including Jeffrey Snover, Lee Holmes, Bruce Payette, Kenneth Hansen.

Is that compelling or not? 🙂

If it is, I would recommend registering sooner rather then later. Attendance is going to be limited to 100 attendees only – so don’t wait too long before registering.

I have already registered, so has Kirk Munro – so there is going to be a good representation from PowerGUI and AD cmdlets side for sure. 😉

Read more about the conference:

Hope to see you there! Register today and spread the news!

Bulk-change vCenter custom attributes

In this quick video, Aaron Newsome shows how he uses PowerGUI (and underlying PowerCLI) to quickly change custom attributes (such as Owner) across thousands of virtual machines. No scripting involved – just a few clicks in the UI. Great demo:

(Video “borrowed” from Mattias’s post on how his team is using this PowerGUI’s capability to set up vCenter monitoring with vFoglight.)

How to Schedule a PowerShell Script

Despite multiple articles on that in the blogosphere already I keep getting questions on running PowerShell as Windows scheduled tasks – so here’s a quick summary what I see as the way to do this (assuming that you are running PowerShell 2.0). I hope you learn something new.

1. Get your script ready

Surprising as it might sound, your script might actually not be ready to run in a scheduled task as is. This happens if it uses cmdlets from a particular PowerShell module or snapin, and it worked for you interactively because you used a specialized shell (e.g. Exchange Management Shell) or a tool like PowerGUI Script Editor which loads the modules for you.

If you indeed are using using any non-default cmdlets, simply add Add-PSSnapin or Import-Module to the beginning of the script. For example:

Add-PSSnapin Quest.ActiveRoles.ADManagement

2. Schedule the task

To schedule a task simply start Windows Task Scheduler and schedule powershell.exe executable passing the script execution command as a parameter. The -File parameter is the default one so simply specifying the script path as the argument would work in a lot of cases:

You can find powershell.exe in your system32\WindowsPowerShell\v1.0 folder.

4. Report task success or failure

If you want your script to report success or failure (or some sort of other numerical result) simply use the exit keyword in the script to pass the value, e.g.:

exit 4

Then your Windows Task Scheduler will show the value in the Last Run Result (you might need to hit F5 to refresh the column in the task scheduler):

3. Passing parameters

If you need to pass parameters things get a little trickier. Say, you have a script which adds two numbers:

param($a=2, $b=2)
"Advanced calculations ahead"
exit $a + $b

To pass the numbers as parameters, you would want to use powershell.exe -Command instead of powershell.exe -File. This -Command argument will then have the script invocation operator &, path to the script, and the parameters. E.g.:

Program: C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe
Add argument (optional): -Command "& c:\scripts\hello.ps1 -a 2 -b 3"

If you want to also get your exit code from the script, you would need to re-transmit that by adding exit $LASTEXITCODE to the command (I learnt this tip from MoW). E.g.

Program: C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe
Add argument (optional): -Command "& c:\scripts\hello.ps1 -a 2 -b 3; exit $LASTEXITCODE"

5. Run x86 PowerShell on x64 Windows

On 64-bit versions of Windows you actually have both 64-bit and 32-bit versions of PowerShell. In most cases you don’t care but in some cases (e.g. specific COM objects being used) you might need specifically a 32-bit version. To get that to run, simply pick the proper executable when you schedule the task:

Regular PowerShell (64-bit version on 64-bit Windows): %SystemRoot%\system32\WindowsPowerShell\v1.0\powershell.exe

32-bit PowerShell (x86): %SystemRoot%\syswow64\WindowsPowerShell\v1.0\powershell.exe

6. Other options

To learn about all parameters PowerShell executable has simply run it with /? option (from either cmd.exe or a PowerShell session).

I normally use -noprofile to make sure that nothing in the PowerShell profile interferes with the task.

Also, if your Execution Policy does not allow running scripts the -ExecutionPolicy parameter comes handy allowing you to make an exception just for this task. E.g.:

c:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe -NoProfile -File c:\scripts\hello.ps1 -ExecutionPolicy RemoteSigned

Here are some other parameters provided by PowerShell:

    Loads the specified Windows PowerShell console file. To create a console
    file, use Export-Console in Windows PowerShell.

I guess you could use that is you want the exact environment you have in the predefined shell from Exchange, AD, or SQL. E.g.: PowerShell -PSConsoleFile SqlSnapIn.Psc1

    Starts the specified version of Windows PowerShell.

I don’t think this one actually works.

Hides the copyright banner at startup.

Not really relevant for scheduled tasks, imho…

    Does not exit after running startup commands.

Might be useful for troubleshooting.

    Start the shell using a single-threaded apartment.

If your script needs STA mode (if you don’t know what this is – most likely you don’t need this. ;))

    Does not present an interactive prompt to the user.

Not really relevant for scheduled tasks, imho…

    Describes the format of data sent to Windows PowerShell. Valid values are
    "Text" (text strings) or "XML" (serialized CLIXML format).

    Determines how output from Windows PowerShell is formatted. Valid values
    are "Text" (text strings) or "XML" (serialized CLIXML format).

Not sure how I would use those… Here’s one example of how -InputFormat none can help fix issues with PowerShell becoming unresponsive when waiting for input.

    Sets the window style to Normal, Minimized, Maximized or Hidden.

Unfortunately, I could not make this work. I tried to use -WindowStyle Hidden to avoid the PowerShell console window popping up during task execution but with no luck.

    Accepts a base-64-encoded string version of a command. Use this parameter
    to submit commands to Windows PowerShell that require complex quotation
    marks or curly braces.

    # To use the -EncodedCommand parameter:
    $command = 'dir "c:\program files" '
    $bytes = [System.Text.Encoding]::Unicode.GetBytes($command)
    $encodedCommand = [Convert]::ToBase64String($bytes)
    powershell.exe -encodedCommand $encodedCommand

Can be useful when having to pass advanced expressions and getting issues with parser.

Any other aspects which I forgot to cover? Please leave your comments!

VDI and PowerShell

Here at Quest we strive to PowerShell-enable pretty much everything we see. 🙂 The newest Quest team to have gone PowerShell are our desktop virtualization folks – vWorkspace. They recently released a PowerShell module for vWorkspace that – as you could probably guess – adds great automation capabilities to the product.

(vWorkspace is a commercial product – so if you are not into desktop virtualization feel free to just consider this a commercial break – we will return to the free stuff in the next blog post. ;))

vWorkspace cmdlets include functionality such as:

  • Managing vWorkspace administrative accounts,
  • Turning on and off computers,
  • Remotely installing MSI’s, and
  • Setting a wide range of properties on the virtual machines and computers hosted by vWorkspace.

Additionally, vWorkspace offers a dynamic filtering syntax, much like Where-Object , that runs server-side rather than locally to speed up the query of large set s of objects. (As Don Jones put it in a session I once attended: “in PowerShell, keep your formatting as far to the right, and your filtering as far to the left as possible.”)

Get-QVWAdministrator –Filter { $QVWAdministrator.Name –like “mdivdi\*” –and  $QVWAdministrator.IsGroup –eq $true }

If you’re a vWorkspace customer, definitely check out the new module on the Quest Community site. They regularly make new versions available, adding more and more functionality each time. Often there’s also a beta of the new upcoming version of the vWorkspace PowerShell module available for anyone to test. In addition, there are some cool examples of how the vWorkspace PowerShell module can be used to ease day-to-day administration of vWorkspace.  Look for this kind of information in the Powershell section of the vWorkspace Community as well. I know the vWorkspace guys would love to hear some feedback on the module so please feel free to comment on their current release.

P.S. Thanks to Adam Driscoll from the vWorkspace team for helping me write this post. Also, if you have not checked out his interview at the recent PowerScripting podcast – you should. He is one of the smartest software engineers I know!

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

February 2011

%d bloggers like this: