Welcome to MSDN Blogs Sign in | Join | Help

Controlling Virtual Server through PowerShell

A number of people have been asking me about using PowerShell to control Virtual Server.  This is a very cool idea - however, out of the box it does not work.  If you try and access the Virtual Server COM object (with a command like: $vs=new-object –com VirtualServer.Application –Strict) it will succeed - but inspecting the object will show no data:

Virtual Server and PowerShell

The reason this happens is that PowerShell is a .Net application - and as a .Net application it does not run with sufficient privilege to be able to talk to our COM interfaces.  In order to address this - you will need to make a library that allows you to set the COM security level on an object to 'impersonate'.  Below is a chunk of C# code that does exactly this (I have attached this code in a file to this post as well):

using System;
using System.Runtime.InteropServices;
using System.Reflection;

namespace Microsoft.VirtualServer.Interop
{
   
using System;
   
using System.Runtime.InteropServices;
   
using System.Reflection;

   
public class 
   Powershell
   {
      
const uint EOAC_NONE = 0;
      
const uint RPC_C_AUTHN_WINNT = 10;
      
const uint RPC_C_AUTHZ_NONE = 0;
      
const uint RPC_C_AUTHN_LEVEL_DEFAULT = 0;
      
const uint RPC_C_IMP_LEVEL_IMPERSONATE = 3;

      [
DllImport(
         
"Ole32.dll"
         CharSet =
CharSet.Auto
         )
      ]
      
public static extern int 
      CoSetProxyBlanket(
         
IntPtr pProxy, 
         
uint dwAuthnSvc, 
         
uint dwAuthzSvc,
         
uint pServerPrincName, 
         
uint dwAuthLevel,
         
uint dwImpLevel, 
         
IntPtr pAuthInfo,
         
uint dwCapabilities
      );

      public static int 
      SetSecurity(
         
object objDCOM
         )
      {
         
IntPtr dispatchInterface = Marshal.GetIDispatchForObject(objDCOM);
         
int hr = CoSetProxyBlanket(
            dispatchInterface,
//pProxy
            
RPC_C_AUTHN_WINNT, //dwAuthnSvc
            
RPC_C_AUTHZ_NONE, //dwAuthzSvc
            
0, //pServerPrincName
            
RPC_C_AUTHN_LEVEL_DEFAULT, //dwAuthnLevel
            
RPC_C_IMP_LEVEL_IMPERSONATE, //dwImpLevel
            
IntPtr.Zero, //pAuthInfo
            
EOAC_NONE //dwCapabilities
         
);
         
return hr;
      }
   }
}

You can compile this code into a DLL by saving it in a .CS file, opening the Visual Studio 2005 Command Prompt and running 'csc /t:library VSWrapperForPSH.cs'.  Once you have done this - you can load this DLL into PowerShell by running '[System.Reflection.Assembly]::LoadFrom(“<<path to DLL>>”)' (note that you need to use the full path to the DLL - if the path is excluded - PowerShell will look in Windows\System32 and complain if the file is not there).

Once you have done all of this - you can now change the COM security level on an object by running '[Microsoft.VirtualServer.Interop.Powershell]::SetSecurity($objectName)'.  And as you can see here - you will then be able to access the object properly:

Virtual Server and PowerShell

However - if you make any new objects (which you will):

Virtual Server and PowerShell

You will get bitten again.  This is simply handled by setting the COM security on objects as you create them:

Virtual Server and PowerShell

Well.  Now that we have all of that working - you can expect to see some posts from me in the near future about how to perform different tasks under PowerShell.  But for now - a couple of final notes to make are:

  1. Under Vista hosts - PowerShell needs to be running 'As Administrator' for this to work (otherwise you will fail to create the first COM object)

  2. I could not have done this without the help of Mike Kolitz and Jon White (from the PowerShell team) - thanks guys!

Cheers,
Ben

Published Tuesday, June 13, 2006 4:25 PM by Virtual PC Guy
Attachment(s): VSWrapperForPSH.cs

Comments

Tuesday, June 13, 2006 9:34 PM by Lorenzo Barbieri @ UGIblogs!

# Controllare Virtual Server attraverso PowerShell (Monad)

Tuesday, June 13, 2006 9:34 PM by Lorenzo Barbieri @ UGIblogs!

# Controllare Virtual Server attraverso PowerShell (Monad)

Wednesday, June 14, 2006 2:14 AM by Erno de Weerd

# PowerShell and Virtual Server

Wednesday, June 14, 2006 6:47 AM by Phylyp

# re: Controlling Virtual Server through PowerShell

> you can expect to see some posts from me in the near future about how to perform different tasks under PowerShell

Hmm, why don't you give us some ideas that we could try implementing? I'm not familiar enough with Virtual Server to come up with ideas myself.
Wednesday, June 14, 2006 8:28 AM by Erno de Weerd

# PowerShell and Virtual Server

Wednesday, June 14, 2006 10:12 PM by VMblog.com - Virtualization Information

# CODE: Controlling Virtual Server via PowerShell

For those of you who have been reading up on Microsoft's latest and future virtualization technologies, you probably came across a number of discussions about Microsoft PowerShell.  If not, PowerShell is a new command-line shell and task-based scripting
Tuesday, June 20, 2006 7:28 AM by teen lesbian rape

# re: Controlling Virtual Server through PowerShell

Persone los pioneros non rabata. Great...
Saturday, June 24, 2006 11:04 AM by adsi4nt

# re: Controlling Virtual Server through PowerShell

Do you think there might be some way to roll up 'change files' with online backups using powershell?
Saturday, June 24, 2006 5:54 PM by David.Wang

# re: Controlling Virtual Server through PowerShell

adsi4nt - sure. It's just scripting.

Since this is over COM Interop of the VS Admin COM API, you can write the same logic in JScript/VBScript, C/C++, .Net, or PowerShell and then invoke it in any.

It is your choice where the programming language and logic resides.

//David
Wednesday, July 05, 2006 2:35 AM by Tim Mintner

# Use Powershell to add virtual machines to Virtual Server

Back to a topic that I know I won't get dinged on.&amp;nbsp; :)
I was playing arround with the list of available...
Thursday, August 03, 2006 3:56 PM by Jose Aguilar's Blog

# .NET and the Virtual Server 2005 COM API

Accessing Virtual Server&amp;rsquo;s COM objects from .NET is not as direct as just including the references...
Monday, January 22, 2007 6:09 PM by jaybaz [MS] WebLog

# Controlling Virtual Server through Microsoft PowerShell

In his post by the same name , Ben describe a series of steps required in order to manipulate Virtual

Tuesday, January 30, 2007 3:11 AM by Virtual PC Guy's WebLog

# Querying guest operating system information with Powershell

I recently posted how to query guest operating system information using VBScript . It was pointed out

Wednesday, January 31, 2007 2:27 AM by Virtual PC Guy's WebLog

# Listing virtual machines under Powershell

There are a lot of things you can do with Virtual Server and Powershell by just interacting with the

Thursday, February 01, 2007 2:39 AM by Virtual PC Guy's WebLog

# Examining Virtual Hard Disks through Powershell

OK, today I would like to try making a script to display virtual hard disk information under Powershell.

Tuesday, April 03, 2007 5:19 PM by Il blog di Giulio Vian

# PowerShell: Verdi praterie

Una caratteristica di PS è la vastità di aree Windows che copre. In pratica se un framework, una applicazione

# Lead, Follow, or Move &raquo; Blog Archive &raquo; Get-PSUGUK July Meeting

Tuesday, July 24, 2007 5:55 AM by Fredrik Wall [Mycket skript blir det]

# Intressanta PowerShell postningar (l&#228;nkar)

När man som jag håller på med PowerShell välldigt, välldigt mycket så hamnar man ofta på många olika...

# markwilson.it [formerly known as Mark's (we)Blog] &raquo; Controlling Virtual Server 2005 R2 using Windows PowerShell

# TO-Tech Blog &raquo; Blog Archive &raquo; VSPlus for Microsoft Virtual Server 2005

Wednesday, October 31, 2007 3:49 AM by FreewareVille

# FreewareVille

Tuesday, November 13, 2007 6:33 PM by VSPlus for Microsoft Virtual Server 2005 - The Tech Blog

# VSPlus for Microsoft Virtual Server 2005 - The Tech Blog

# Desktop Computers &raquo; Virtual PC Guy&#8217;s WebLog : Controlling Virtual Server through PowerShell

# Virtual PC Guys WebLog : Controlling Virtual Server through PowerShell &laquo; Catnippet

# .NET and the Virtual Server 2005 COM API | Jose Aguilar's Blog

New Comments to this post are disabled
 
Page view tracker