简体   繁体   中英

How Execute Azure Powershell cmdlets from a ASP app hosted in IIS

Im working on an ASP MVC app that automates some Azure services, like create a VM. In order to do that im using Azure Powershell cmdlets.

The app will be hosted in an Azure VM with IIS and SQL Server, I already install the Azure cmdlets module to manage de Azure services, I already tested the cmdlets using my Azure subscription, they worked well in the VM.

When im debugging the app on localhost using localDB and IIS EXPRESS the cmdlets are successfully recognized and work well, but when I deploy the application to the IIS the CMDlets are not recognized.

"Error message: 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 Im missing? what i have to setup in order to run the cmdlets from my the deployed app on the IIS?

Uopdate: This is an example of one function, the function populate a dropdownlist with the result, in localhost it wor fine, in IIS.

public IEnumerable<SelectListItem> getImageFamily()
    {
        var shell = PowerShell.Create();
        shell.Commands.AddScript("C:\\inetpub\\wwwroot\\appName\Scripts\\test.ps1");
        Collection<PSObject> results = shell.Invoke();

        List<SelectListItem> items = new List<SelectListItem>();
        foreach (var psObject in results)
        {
            string image = psObject.BaseObject.ToString();
            items.Add(new SelectListItem { Text = image, Value = image });
        }
        return items;
    }

Then this is the simple script

(Get-AzureVMImage).ImageFamily | sort -Unique

I also tried

Import-Module -Name "${env:ProgramFiles(x86)}\Microsoft SDKs\Azure\PowerShell\ServiceManagement\Azure"

(Get-AzureVMImage).ImageFamily | sort -Unique

And also tried (I cut some code...)

public IEnumerable<SelectListItem> getImageFamily()
    {
        ...
        shell.Commands.AddScript("(Get-AzureVMImage).ImageFamily | sort -Unique");
        Collection<PSObject> results = shell.Invoke();
        ...
    }

When I run the instruction inside a script file I got:

The term 'C:\\inetpub\\wwwroot\\appName\\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.

:(

Iv'e encountered this problem while trying to auto deploy into azure aswell

the solution in my case was loading the modules inside the script

Write-Verbose "Improt Azure Powershell cmdlets"
Import-Module -Name "${env:ProgramFiles(x86)}\Microsoft SDKs\Azure\PowerShell\ServiceManagement\Azure"
Import-Module -Name "${env:ProgramFiles(x86)}\Microsoft SDKs\Azure\PowerShell\ResourceManager\AzureResourceManager"

PS make sure the paths are the same in your VM's

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM