Delen via


Working with the SCVMM Templates via Powershell

Ok... this is my first blog post in quite some time. I've been doing some different coding and since my ASPNET blog posts helped people out, I thought I'd share what I've learned working with SCVMM.

 In this post I'll describe how to request and work with SCVMM template items via powershell and C#.

This post is going to assume you know how to setup C# and work with the System.Management.Automation namespace. If you don't I'll be posting a blog on how I do this next.

In this blog I'm going to show you how to request a specific template and parse the PSObject returned so we can populate a form with the template values. 

The first thing I'm going to do is create the powershell script to get a given template. I've created a function that takes in two parameters, the name of the template that I want, and the VMM Server where that template lives:

public void GetTemplate(string TemplateName, string Server)

{

string psscript = "Get-Template -VMMServer " + Server + " | where {$_.Name -eq '" + TemplateName + "'}";

      
      Collection<PSObject> results = InvokePowerShell(psscript);
}

The InvokePowerShell function will be detailed in the next blog post.

OK this is great, now we have a PSObject that contains all the members of the Template class. But how do we go and get these members so we can use them in C#? With the exception of the custom properties, you just have to know the Member name. Let's take a look:

if (results[0].Members["FullName"].Value != null)

{

tempTemplate.FullName = results[0].Members["FullName"].Value.ToString();

}

So here I am assuming that there is only one template that matches the name that we passed into our function. If there are multiple template objects, they will be returned via an array of PSObjects. This is why I just check the first item in the results array.

From there I just need to know the name of the member of the template class that I'm looking for. There are many ways to do this. You can open the SCVMM powershell window and type Get-Help Get-Template. Or easier, just open the SCVMM powershell window and type get-template. This will return all templates and all members of those templates of the localhost.

Now I did mention that the custom properties are a bit different to work with. This is because the custom properties are canacatenated together split by a space. We can create an array of custom properties to access our custom properties very easily like this:

string[] customproperties = (string[])results[0].Members["CustomProperties"].Value;

tempTemplate.CustomProperty1 = customproperties[0];

string CustomProperty2 = customproperties[1];

The last topic to cover would be setting template settings. I do this by getting the template from SCVMM and then passing the values of the members to be set via the  Set-Template cmdlet. This function will set custom property 1 of the template to the value passed into the function.

public void UpdateTemplate(string TemplateName, string myValue)

{
     string psscript = "";

     psscript += "\r\n$Template = Get-Template -VMMServer localhost | where {$_.Name -like '" + TemplateName + "'}\r\n";

     psscript += "\r\nSet-Template -Template $Template -Custom1 '" + myValue + "'";

Collection<PSObject> results = InvokePowerShell(psscript);
}

If the function succeeds it will return the name of the template in the PSObject results.

Thats it... pretty straight forward.

Enjoy!

 

Adam

Comments

  • Anonymous
    April 12, 2010
    Hi there, thanks for posting this info.  I was doing something similar, might wanna take a look:http://scorpiotek.com/blog/?p=770I got here because I was searching when I executed some commands, I got no results back.  Using your code, the same thing is happening.I'm using the exact methods as you when testing, except the GetTemplate function:public static void GetTemplate(string Server)       {           string psscript = "Get-Template -VMMServer " + Server;           Collection<PSObject> results = InvokePowerShell(psscript);       }The incoming parameter is the name of my server (SCVMM) and when I run the powershell command:Get-Template -VMMServer SCVMMI get all the listings of the templates.   Any ideas on what might be going on?Thanks!
  • Anonymous
    April 12, 2010
    Whoops, the assembly was throwing an exception and I forgot to fill in the catch part - my bad.Anyhow, I'm building with VS2010 and it seems that it does not like to mix Framework versions with the commandlet:{"Mixed mode assembly is built against version 'v2.0.50727' of the runtime and cannot be loaded in the 4.0 runtime without additional configuration information."}I change the project to compile to Framework 2.0 and your code works like a charm.Thanks again for posting this!