0
votes

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?

1

1 Answers

1
votes

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

https://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