Can we run powershell script via hosted web api in azure? - powershell

want to run powershell scripts from a web API hosted in azure. But I am getting Execution Policy error while trying to import modules and when I try to change the execution policy using below code
ps.AddCommand("Set-ExecutionPolicy").AddArgument("Unrestricted")
.AddParameter("Scope", "CurrentUser");
It says Unauthorized access on C:/Powershell.
This works fine in localhost, but not on deployed web api.
Is there a workaround on this or can we not run powershell scritps via hosted api at all?

You can package your PowerShell Scripts into Azure Functions and expose them as Web APIs.
This gives you the added advantage of leveraging bindings with your functions if required, and other platform benefits of Azure Functions like auto scaling.

Related

Run Custom Script Extension Against Azure App Service

I am able to execute Custom Script Extensions against VMs using Set-AzureRmVMCustomScriptExtension and noticed that Azure App Services have Extensions listed as one of their options:
Extensions add functionality to your App Service. Click add to see the
list of available extensions.
What I would like to achieve is running some PowerShell against the VM hosting an App Service, but can't locate a Resource Manager cmdlet like Set-AzureRmVMCustomScriptExtension. An example is modifying the security of some of the directories of IIS hosting my website to disable anonymous access, and adding a few files to directories of my website. I am able to achieve this using a script against an IaaS Azure VM and am wondering if this will work against an App Service, and if so, how to target the VM of the App Service.
Key actions I take as part of this PowerShell is:
Disable anonymous access to certain directories in IIS
Limit access to certain file types
Edit existing XML files
Add new files to a directory (downloaded via CDN)
Logging into the Kudu interface (Mysite.scm.azure...) I am able to execute some PowerShell via the debug Console option, so I am thinking there has to be a way to run PowerShell via Azure Automation beyond the AzureRm.Websites cmdlets as with the Kudu PowerShell interface I was able to create directories with a plain old create-item cmdlet.
App Service is very different from VM hosting. In App Service, you never get direct access to the VM, and there is generally no way that you can run any script that affects the VM at a global level.
Instead, you are only able to affect the files that make up the Web App, and everything runs under a sandbox.

Deploy asp.net web app on local or remote IIS using PowerShell DSC

I am unable to find any help on how to use PowerShell for DSC and deploy asp.net web app on local or remote on premises IIS.
Can you please help me with the steps or any resources?
What's your preferred method of deploying code prior to using DSC? xRobocopy was created with this scenario in mind, but if you are packaging your solution using NuGet, you could also use the PackageManagement resource.
I started working on a NuGet based config script recently but it isn't ready to publish in the gallery yet:
https://github.com/Microsoft/NuGetIISAppServerConfig/blob/master/NuGetIISAppServerConfig.ps1

Create azure website programatically in C#

I want to create/update the websites/cloud services in Azure in C#. My objective is to deploy the website/cloud service in Azure without any user intervention.
Can anyone please help me to resolve below queries?
Can we manage Azure websites/cloud services using C# code? If yes then how (any library/api/nuget package)?
If it is not possible in C#, then what are other options to achieve this? I read WebDeploy(MsDeploy), powershell can do this work but I am not sure which one is best in this scenario and how to use them.
This completely depends on your scenario. If you have got a system to run your powershell script from, this might be a good option (https://azure.microsoft.com/en-us/documentation/articles/powershell-install-configure/) You could also use the cross platform command line tools to script your deployment / web app creation. There are different other options, especially for continous deployment to a web app. You can for example connect your github repo to an existing web app and deploy from that repository.
The C# library you were looking for should be this one:
https://github.com/Azure/azure-sdk-for-net/tree/master/src/ResourceManagement/WebSite

Running PowerShell scripts programmatically in Azure Web Sites

I want to run PS scripts in C# exactly along the lines of Sample
This works on a Cloud Service (Web Role), on a standard server (with execution policy disabled), but not in a Azure Web Site.
Anyone know why this is so?
I don't know of any official documentation on the issue but there are multiple answers on the related forums where Microsoft employees talk about the limitations of not being able/allowed to launch external processes in Windows Azure Web Sites. See
PHP - Warning: exec(): Unable to fork
PDF Generator failing on Azure Website but not development machine
Recommended HTML to PDF Generator that works on Azure Website Reserved mode with Full Trust
You are probably trying to do that with your powershell script.
You could verify if that is in fact your issue by catching and looking at the exception(s) which should relate to security policies/permissions.

csmanage or powershell commandlets, which one to choose?

After looking around for some information about managing my azure app i found csmanage and powershell commandlets. What are the difference between these applications and which one to choose?
The basis of Windows Azure Services management is REST API and beauty of using REST API is that you can wrap REST in any programming language as longs as there is networking and security infrastructure available. Most of the tools you mentioned above are using REST API underneath and using Windows Azure Service Management API. CSMANAGE and PowerShell Cmdlets are using Windows Azure Service Management API to manager Windows Azure subscriptions. You can learn more about Service Management API here.
There is also another tool call WAPMMC (Windows Azure Platform Management Tool (MMC) which is also built upon Service Management API.
Besides it there are 3rd party tools which are built using Service management API i.e. Cerebrata Powershell cmdlets.
Comparing all of above, I would say Windows Azure Powershell Cmdlets is the most powerful tool among all of these and it has been updated with bug fixes and many more features added frequently. To use it in your machine you just need to download powershell cmdlets and Windows Azure SDK in your machine as these are required.
http://wappowershell.codeplex.com/
Recently I also found a Java based tool called Burp to use REST API directly from its shell to manage Windows Azure subscription. There is no need for .net framework or Azure SDK. You just need a machine with Java runtime and Burp. I have added a blog on this regard here.
I think it depends on what you're more comfortable with. I don't think the CSManage tool is being updated that much any longer (maybe at all - unsure). The PowerShell cmdlets get a lot of attention - both from Microsoft and partners like Cerebrata.
My vote - learn PowerShell and go with the PowerShell cmdlets for working with your Windows Azure apps. Small learning curve and well worth it.