Quantcast
Channel: Virtual Machine Manager – General forum
Viewing all articles
Browse latest Browse all 2770

How to run VMM cmdlets from C# code on a remote system using PS remoting to the SC-VMM server?

$
0
0

I have a SC-VMM server which I connect from a remote machine to use VMM cmdlets to manage the VMs.

I want use the same (PS remoting and run SCVMM cmdlets) using C# code. Can someone advice can this work & how? ... I have added the code which does PS remote and try to Import VMM module and say get all host names? But after successful remoting  

But I dont get any output for get-SCVMHost cmdlet? No error or exception, nothing. 

int iRemotePort = 5985;
string strShellURI = @"http://schemas.microsoft.com/powershell/Microsoft.PowerShell";
string strAppName = @"/wsman";
AuthenticationMechanism auth = AuthenticationMechanism.Negotiate;
WSManConnectionInfo ci = new WSManConnectionInfo(
    false,
    sRemote,
    iRemotePort,
    strAppName,
    strShellURI,
    creds);
ci.AuthenticationMechanism = auth;

Runspace runspace = RunspaceFactory.CreateRunspace(ci);
runspace.Open();
PowerShell psh = PowerShell.Create();
psh.Runspace = runspace;

//Import VMM Module, NOTE: Also tried AddSnapin method, but that didnt work either..
string psCmds = "Import-Module VirtualMachineManager";
psh.AddScript(psCmds);
psh.Invoke();
foreach (ErrorRecord current in psh.Streams.Error)
{
    retcode += 1;
    returnMsg.Append("\nException: \n" + current.Exception.ToString());
    returnMsg.Append("\nInner Exception: \n" + current.Exception.InnerException);
}
//Get HostList
psh.Commands.Clear();
psCmds = "Get-SCVMHost | Format-List Name";
psh.AddScript(psCmds);Pipeline pipeline2 = runspace.CreatePipeline();
pipeline2.Commands.AddScript(psCmds);
Collection<PSObject> myresults = pipeline2.Invoke();foreach (PSObject pso in myresults)
{
    Console.WriteLine(pso.Members["Name"].Value);
}



Viewing all articles
Browse latest Browse all 2770

Trending Articles