SCVMM 2008 : C# and the SDK
All i wanted to do was to run SCVMM cmdlets from within my C# Application, here's my example on how to get this done,
=============================================
using System;
using System.Collections.ObjectModel;
using System.Collections.Generic;
using System.Text;
using System.Management;
using System.Management.Automation;
using System.Management.Automation.Runspaces;
namespace blogsvcmm
{
class Program
{
static void Main(string[] args)
{
// supplyin SCVMM Server Name as Parameter
string vmserver = "imanlab2";
// This is two line script
// we could provision this to be read from DB or a file
string scriptText = "$vmserver=Get-VMMServer " +vmserver +"\n";
scriptText = scriptText + "echo $vmserver";
RunspaceConfiguration runSpaceConfig = RunspaceConfiguration.Create();
PSSnapInException snapInException = null;
PSSnapInInfo info = runSpaceConfig.AddPSSnapIn("Microsoft.SystemCenter.VirtualMachineManager", out snapInException);
Runspace rspace = RunspaceFactory.CreateRunspace(runSpaceConfig);
rspace.Open();
Pipeline pipeline = rspace.CreatePipeline();
pipeline.Commands.AddScript(scriptText);
pipeline.Commands.Add("Out-String");
Collection<PSObject> results = null;
try
{
results = pipeline.Invoke();
}
catch (Exception p)
{
Console.WriteLine(p.Message);
}
rspace.Close();
StringBuilder stringBuilder = new StringBuilder();
foreach (PSObject obj in results)
{
stringBuilder.AppendLine(obj.ToString());
}
Console.WriteLine(stringBuilder.ToString());
Console.WriteLine("\n Press Any key to exit");
Console.ReadLine();
}
}
}
=============
Sample Output
=============
Name : <scvmmserver>
IsConnected : True
ServerInterfaceVersion : 2.0.0
Profile : Administrator
FullyQualifiedDomainName : <scvmmserver fqdn>
FQDN : <scvmmserver fqdn>
Channel : Microsoft.VirtualManager.Remoting.IVirtualManager
Service
ObjectCache : \Microsoft.SystemCenter.VirtualMachineManager.Clie
ntCache
MOMReportingEnabled : False
OpsMgrReportingEnabled : False
MOMReportingServerURL :
OpsMgrReportingServerURL :
OpsMgrServer :
SelfServiceContactEmail :
PlacementGoal : LoadBalance
MemoryPriority : 8
DiskIOPriority : 2
CPUPriority : 8
NetworkPriority : 2
CEIPOptIn : True
VMRCAccessAccount :
VMRCDefaultPort : 5900
VMConnectDefaultPort : 2179
MinimumSupportedAgentVersion : 2.0.3444.0
LibraryRefresherEnabled : True
LibraryRefresherFrequency : 1
PROMonitoringLevel : Off
PROAutomationLevel : Off
PhysicalAddressRangeStart : 00:1D:D8:B7:1C:00
PhysicalAddressRangeEnd : 00:1D:D8:F4:1F:FF
DatabaseServerName : <scvmmserver fqdn>
DatabaseInstanceName : MICROSOFT$VMM$
DatabaseName : VirtualManagerDB
UserName : Windows User
CompanyName :
ProductVersion : 2.0.3444.0
ProductID : 00683-030-3201177-82230
IsEvaluationVersion : False
IsWorkgroupEdition : False
EvaluationDaysLeft : 0
Hope you find this post useful, Cheers !!!
Jeevan Bisht : Support Escalation Engineer