I'm developing a test project in ASP.NET MVC, where Im using System.Management.Automation to execute some PowerShell cmdlets. Some of the cmdlets are from the Azure Powershell module.

The code is working fine when I run it from Visual Studio. But when I Published the website to IIS the some cmdlets and these scripts don't work.

Example, see the comments before:

    var shell = PowerShell.Create();
    var script1 = "Get-AzureSubscription | Out-String"; // cant execute the cmdlet 
    var script2 = "C:\\inetpub\\wwwroot\\App1\\Scripts\\test.ps1"; //Cant execute the script.
    var script3 = "Get-Date"; //Work fine
    try
    {
     shell.Commands.AddScript(script); // here insert the script.
     Collection<PSObject> results = shell.Invoke();
     //Search for errors, if some error is found redirect to an Error page.
        if (shell.Streams.Error.Count > 0)
        {
         foreach (ErrorRecord err in shell.Streams.Error)
         {
          string error = err.ToString();
          TempData["pserror"] = error;
          return RedirectToAction("Powershellerror");                
         }
        }
         else 
          if (results.Count > 0)
          {
           foreach (var psObject in results)
           {
            string result2 = psObject.ToString();
            TempData["psoutput"] = result2;
            return RedirectToAction("PowershellOutput");
           }
          }

Both, script1 and script2 give this error:

The term 'C:\inetpub\wwwroot\App1\Scripts\test.ps1' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again.

and

The term 'Get-AzureSubscription' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again.

What could be??? Something is missing in the IIS setup?

share|improve this question

You probably need to import the Azure module, try as suggested here:

http://stackoverflow.com/a/6267517/1183475

var ps = PowerShell.Create(myRS);
ps.Commands.AddCommand("Import-Module").AddArgument(@"g:\...\PowerDbg.psm1")
ps.Invoke()

I don't have the Azure PS tools installed on this machine, but the path should be: "C:\Program Files (x86)\Microsoft SDKs\Windows Azure\PowerShell\Azure\Azure.psd1

share|improve this answer
    
Thanks for the reply, but I'm still wondering why I cant execute a script file (.ps1), even is the script have a cmdlet like Get-Date, that works fine is is added explicitly (shell.Commands.AddScript(Get-Date);) but not inside the script, even is the script is empty does not work. Why is no recognizing the script???? The Error: The term 'C:\inetpub\wwwroot\App1\Scripts\test.ps1' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again. – TassadarCRG May 15 '15 at 16:15
    
That happens only when is deployed to IIS, running the app in visual studio works fine. – TassadarCRG May 15 '15 at 16:16
    
Running the app in visual studio means using IIS express ? Or is it a console app that you use for testing? – marius-O May 15 '15 at 20:45
    
Also, to make sure it's not a permissions issue or anything funny, can you load the content of the script with a StreamReader and add it via .AddCommand ? – marius-O May 15 '15 at 20:58

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.