Ying Li(MVP) at myITforum.com

PowerShell & System Center

August 2008 - Posts

PowerShell script to remove virtual machine from VMWare Infrastructure

Forget about the trip to Las Vegas I mentioned before, I started to like VMWare VI Tookkit the more I use it!

You just need to cultivate a habit, every time if you need to repetitively doing something, you should ask yourself if you can do it with PowerShll. More likely than not, the answer is YES, WE CAN!

Like I have a bunch of cloned vm box acting as a backup to our MOSS staging environment. We are tight on resource and so we decided to delete all the clones. Instead right click one by one, you can use the below script. Thanks Hal's comment to my previous post, it's now much simpler!

get-vm clone*|remove-vm -deletedfromdisk

But I do notice that after I remove vm this way, I got something like

clone-xyz(orphaned), you may have to add remove-inventory but I wasn't able to test this part!

Similarly you could do:

get-vm xyzmoss* |shutdown-vmguest

and

get-vm xyzmoss* |start-vm

Posted: Aug 19 2008, 08:57 PM by yli628 | with 3 comment(s)
Filed under:
PowerShell Script to Take Snapshot on Multiple VM Machines

In my previous Blog, I introduced some basics about VMWare Infrastructure Toolkit. Now let's put it into action!

Let's say you have a test MOSS or SMS virtual environment, you have a couple of servers and you configured everything and test everything. Before you make the next big change, you may want to take a snapshot of your machines so you can have some to fall back on if something goes bad!

It couldn't easier with VI Toolkit!

$Now = Get-Date

#get all your moss servers
$VMs = get-vm |where{$_.name -like "xyzmoss*"}
foreach ($vm in $VMs)

#Take snapshots for each VM Box
{new-snapshot -vm $vm -name $Now -confirm}

Posted: Aug 18 2008, 11:26 PM by yli628 | with 4 comment(s)
Filed under:
PowerShell Script to refresh GPO remotely on multiple computers

Here is a KB article explains how to refresh the Group Policy Settings on remote computers. Basiclly you need to download PsTools from SysInternals and run something like this:

For Windows XP Computers:
Psexec.exe -@ComputerList.txt Gpupdate.exe /Target:User /force
Psexec.exe -@ComputerList.txt Gpupdate.exe /Target:Computer /force

Where you have the computer names in the ComputerList.txt file. I made a little effort to translate that into PowerShell

$colComputers = gc c:\users\yl.admin\pstools\ComputerList.txt

Foreach ($strComputer in $colComputers)

{.\psexec.exe \\$strComputer gpupdate.exe /target:computer /force}

You can change the target to user at you wish.

Posted: Aug 14 2008, 03:34 PM by yli628 | with 1 comment(s)
Filed under:
Data Warehouse failed to enumerate database components to be deployed

Right after I imported a couple of new MP to our OpsMgr environment, I started to get the below alert:

Alert: Data Warehouse failed to enumerate database components to be deployed

Source: XYZOPMP01.related.com

Path: XYZOPMP01.related.com

Last modified by: System

Last modified time: 8/12/2008 7:24:26 PM Alert description: Data Warehouse failed to enumerate database components to be deployed. Failed to enumerate Data Warehouse components for deployment. The operation will be retried.

Exception 'SqlException': Timeout expired. The timeout period elapsed prior to completion of the operation or the server is not responding.

One or more workflows were affected by this.

Workflow name: Microsoft.SystemCenter.DataWarehouse.Deployment.Component

Instance name: XYZOPMP01.related.com

Instance ID: {986ED2F5-5B26-0554-2092-0FF3851DFA86}

Management group: XYZ Enterprise Apps

We have OpsMgr SP1 and the latest Data Warehouse Library (6.0.6278.0).  The fix from Vitaly Filimonov [MSFT] is to run sp_updatestats on OperationsManagerDW DB and it indeed solved our problem!

Posted: Aug 12 2008, 07:43 PM by yli628 | with no comments
Filed under:
Introducing VMWare Infrastructur Toolkit (for Windows) 1.0

Recently VMWare released their version of PowerShell Cmdlets called VI ToolKit. You can download it Here and it is free!

VMWare is also challenging the world's VI administrators for a scripting contest, you can win a trip to VMworld 2008 Las Vegas or $5000 cash. Sounds too good to be true - it is not!

But before we get too excited, we have to setup a good foundation - At least we need to get it installed before we can write anything!

The installation itself is very straight forward process but it requires PowerShell v1.0 and it won't work with PowerShell CTP 2.0.

Some cmdlets only supported in API version 2.5 or newer.

After the setup you can start the Toolkit from start - all programs - VMware - VMWare VI Toolkit - VMWare VI Toolkit (for Windows) or

If you like me, "hate" to open one powershell console for each application, you can open the regular powershell console and run:

Add-PSSnapin VMWare.VImAutomation.Core and then run Initialize-VIToolkitEnvironment.ps1 in C:\Program Files\VMware\Infrastructure\VIToolkitForWindows\Scripts

Now you regular powershell console becomes VMWare VI Toolkit!

By now we have everything in order, we can put VMWare VI Toolkit in action.

you can go get-VC or Get-ESX to connect to your virtual center or ESX host

after you connect to your VC or ESX, you can do

get-vm |select name, powerstate

You will get something like:

MOSS-SQLS01
PoweredOn

SCCMS01
PoweredOn

MOSS-SPRS01
PoweredOn

VEMS01
PoweredOn

Clone-ANSS01
PoweredOff

Clone-APPS01
PoweredOff

Clone-SPRS01
PoweredOff

SCCMS04
PoweredOn

...

You can do start-vm "clone-anss01" to poweron the vm box

or stop-vm "SCCMS01" to poweredoff the vm box

To get a list of VI command, type:

get-vicommand

I only played with it for a couple of hours and I like it already. It indeed feels and looks like PowerShell! From here, your imagination is your limit - I don't know about you, I started to hear the slot machine singing already. :)

Posted: Aug 11 2008, 10:26 PM by yli628 | with no comments
Filed under:
PowerShell Script to Schedule a Task - Task Scheduler API

In my previous post, I blogged about how to use PowerShell to retrieve scheduled tasks (Task Scheduler API). Today I will go one step further and discuss how to create a scheduled task using PowerShell. This will run only on Vista/Windows Server 2008!

#First we create the TaskService object and connect to remote Windows Server 2008 box "Whatever"

$ST = new-object -com("Schedule.Service")

$ST.connect("Whatever")

#Get to root folder (\) to create the task definition

$RootFolder = $ST.getfolder("\")

$TaskDef = $ST.NewTask(0) # Still I couldn't explain why we need a 0!

#Now we define the task, creating the $RegiInfo object

$RegInfo = $Taskdef.RegistrationInfo

$Reginfo.Description = "Task will send email when a specified user logs on"

$Reginfo.Author = "Mydoamin\yl.admin"

$Settings = $Taskdef.Settings

$Settings.StartWhenAvailable = $True

#Next we will create a logon trigger

$Triggers = $Taskdef.Triggers

$Trigger = $Triggers.Create(9) # 9 is TASK_TRIGGER_LOGON

#We will define when the trigger will be active in UTC time format and convert to EST (-05:00)

$Starttime = "2008-08-05T12:00:00-05:00"
$Endtime = "2008-08-06T12:00:00-05:00"

$Trigger.StartBoundary = $Starttime
$Trigger.EndBoundary = $Endtime

$Trigger.ExecutionTimeLimit = "PT5M" # time out in 5 minutes

$Trigger.Userid = "mydomain\yli" # must be a valid user

#Next we will create the action for the task to execute which is sending an #email 6 - TASK_ACTION_SEND_EMAIL

$Action = $Taskdef.Actions.Create(6)
$Action.Server = "smtp.contoso.com"
$Action.Body = "This is the email message."
$Action.From = "ying.li@contoso.com"
$Action.Subject = "This is the subject."
$Action.to = ying.li@contoso.com

#Last but not least is we will have to register or create the task

$Rootfolder.RegisterTaskDefinition("Test", $Taskdef, 6, "mydomainyl.admin", "Password", 3)

#Where "test" is the name or path of the Task

#$Taskdef is the task defination

#6 meansTASK_CREATE_OR_UPDATE

#username

#password

#3 means Task_Logon_Interactive_token - the task will be run only in an existing interactive session.

Guess what, we just create a scheduled task using PowerShell! The trigger - someone logon to a remote windows server 2008 box; The action - to send an email.

Posted: Aug 06 2008, 12:23 AM by yli628 | with 1 comment(s)
Filed under:
Open Microsoft SQL Server Management Studio on Windows Server 2008 box

As you may already noticed, when working with Windows Server 2008, there are a lot of nuances we need to pay attention like if you are trying to open Microsoft SQL Server Management Studio, you will get an error like below even though the account you use has the admin right on SQL

SQL Error

What we need to do is to use Run As Administrator to start SQL Server Management Studio to bypass that error!