none
Executing SCVMM PowerShell scripts via C# conditionally works depending on application type RRS feed

  • Question

  • I suspect that this is the wrong forum but I could not find one that was appropriate.

    The environment is SCVMM 2012 R2. I have a series of PowerShell scripts that are executed via C# code from a variety of applications -- MVC, WCF, console, unit tests. It seems that successful connection to the VMM server is dependent on the type of application being used. For example, console apps and WCF apps can connect successfully but the same code running in a unit test or standard MVC app throw the following exception:

      

    {You cannot access VMM management server SC-01. (Error ID: 1604)

    Contact the Virtual Machine Manager administrator to verify that your account is a member of a valid user role and then try the operation again.}
        CategoryInfo: {ReadError: (:) [Get-SCVirtualMachine], CarmineException}
        ErrorDetails: {You cannot access VMM management server SC-01. (Error ID: 1604)

    Contact the Virtual Machine Manager administrator to verify that your account is a member of a valid user role and then try the operation again.}
        Exception: {"You cannot access VMM management server SC-01.\r\nContact the Virtual Machine Manager administrator to verify that your account is a member of a valid user role and then try the operation again."}
        FullyQualifiedErrorId: "1604,Microsoft.SystemCenter.VirtualMachineManager.Cmdlets.GetVMCmdlet"
        InvocationInfo: Command = {Get-SCVirtualMachine}
        PipelineIterationInfo: Count = 0
        ScriptStackTrace: "at <ScriptBlock>, <No file>: line 1"
        TargetObject: null

    I suspect that somehow, each app is running under different credentials but I cannot see how that is possible. Each app exhibits the stated behavior whether running within Visual Studio (w/ IIS Express) [running under my own account] or published to the server running under a specific specified account that definitely has permissions to the VMM environment.

    This behavior is easily reproducible with the following code

    -- PS Script as embedded resource

    Get-SCVirtualMachine -VMMServer "SC-01"

    -- C# code to load and execute script

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.Management.Automation;
    
    namespace LabManagement {
    
    	public class HyperVVirtualMachineManager {
    
    		public IEnumerable<Models.VirtualMachine> GetVirtualMachines() {
    			var vms = new List<Models.VirtualMachine>();
    
    			try {
    				using (var rs = System.Management.Automation.Runspaces.RunspaceFactory.CreateRunspace()) {
    					rs.Open();
    
    					using (var ps = PowerShell.Create()) {
    						ps.Runspace = rs;
    
    						var assembly = System.Reflection.Assembly.GetExecutingAssembly();
    						var scriptName = "LabManagement.Scripts.GetAllVirtualMachines.ps1";
    
    						using (var s = assembly.GetManifestResourceStream(scriptName)) {
    							using (var reader = new System.IO.StreamReader(s)) {
    								var script = reader.ReadToEnd();
    
    								ps.AddScript(script);
    							}
    						}
    
    						var output = ps.Invoke();
    
    						if (ps.Streams.Error.Count > 0) {
    							foreach (var item in ps.Streams.Error) {
    								// do something
    							}
    						}
    
    						if (output.Count > 0) {
    							foreach (var o in output) {
    								// do something
    							}
    						}
    
    					}
    
    					rs.Close();
    				}
    			}
    			catch (RuntimeException ex) {
    				// do something
    			}
    
    			return vms;
    		}
    
    	}
    
    }
    

    -- Console app (Success)

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    
    namespace VirtualLabManagement.ConsoleTests {
    
    	class Program {
    
    		static void Main(string[] args) {
    			// Arrange
    			var svc = new LabManagement.HyperVVirtualMachineManager();
    
    			// Act
    			var hosts = svc.GetVirtualMachines();
    			
    			// Assert
    		}
    
    	}
    
    }
    

    -- Unit Test (Fails)

    		[TestMethod]
    		public void TestMethod1() {
    			// Arrange
    			var svc = new LabManagement.HyperVVirtualMachineManager();
    
    			// Act
    			var hosts = svc.GetVirtualMachines();
    
    			// Assert
    			Assert.IsNull(hosts);
    		}

    As you can see, the code is exactly the same between the different executions but the ability to connect differs.

    Tuesday, February 17, 2015 10:53 PM

All replies

  • Seems that this issue has not been resolved since it still happens... with IIS Express now. I can run a script that connects to SCVMM in a console app launched from Visual Studio but within a ASP.NET MVC app (IIS Express) it does not.
    Thursday, January 12, 2017 1:13 AM