I need to run a powershell script on a schedule using azure functions, I tried to execute the file using node.js but it's not working and i can't figure out why.
While my original answer was correct at the time, Azure has been updated to support Azure Functions utilizing Powershell: https://learn.microsoft.com/en-us/azure/azure-functions/functions-reference-powershell
Original Answer:
To me Azure Automation (https://learn.microsoft.com/en-us/azure/automation/) seems like a better model for running a Powershell script on a schedule in Azure since it really is a native capability of that tool.
If you really want to do it in an Azure Function it does look like there are some resources you could try (I have not tested them):
https://blogs.msdn.microsoft.com/powershell/2017/02/24/using-powershell-modules-in-azure-functions/
https://github.com/Azure/azure-functions-powershell-worker
Best of luck!!
Related
Is it possible to change the publish method for an azure function without deleting and recreating it? Currently I want to swap the publish method from code to docker containers for a couple of azure functions apps.
I am not able to find anything useful for this matter under the deployment center or under the Microsoft's Azure CLI documentation.
I've seen that it's possible to do that for webapps but didn't found much about azure functions apps.
I highly appreciate any kind of help!
Thank you!
Here is the suitable MS Doc for your requirement of changing the Publish Method of Azure Functions to the Docker Container.
This documentation contains different ways of achieving the above requirement such as using the Azure Functions core tools commands, CLI and also PowerShell Commands.
The actual way of deploying/publishing the Azure Functions is creating the Azure functions along with the docker image initialization. But, when publishing the Azure Functions code manually to docker, you need the docker file in your Azure Functions Project.
You can initialize the docker file using the command func init --worker-runtime -docker which will prompts for the langauge runtime such as Java, dotnet, node, python, etc.
Refer to the SoundCode article given by #MarkHeath and Educba article for more information on adding the docker file in Azure Functions project and publishing to docker.
I want to run a python script in Azure without using flask or jango. I'm thinking about azure functions or something like this.
Thank you!
Sure you can use Azure functions in this case. Alternatively, you can also use Azure web jobs where you can upload it through the web job blade by creating your app service in the VS code zipping it.
REFERENCES:
function in Azure with Python using Visual Studio Code - MSFT Docs
WebJobs - github wiki
I would like to perform the following steps on schedule (presumably using Azure Automation):
Provision a VM in Azure
Run a powershell script on that VM
Deprovision VM
Actually I have more steps but left only 3 for simplicity.
I am new to IaC and appreciate your general guidance and advice.
Is it scope of Azure Automation or I need something else?
I would like to code everything in text format and put in Git and update automatically via Pull Requests
Should I use Runbooks or DSC?
Regarding step 2, I cannot figure out how I can upload my powersehll script into newly created VM and run it locally. The script downloads some files and updates some remote resources.
Thanks,
Ruslan
there are a lot of options and tools to achieve your goal.
If you will be working strictly in the Azure cloud, The following tools are most commonly used for building an environment.
Azure-powershell
Azure-CLI
ARM-templates
each of them very similar but all a little different with their own benefits to them, but they are all tools for building your virtual infrastructure. For configuring your resources there are other tools. Like you mentioned yourself, DSC is a tool to configure virtual machines.
if you are planning to use github to push your code, i would recommend using ARM-templates. You can very easily use your own or other templates by referencing in your code. However this might be the 'hardest' solution to learn and understand the syntax in comparison to the cli and powershell. But also the most frequently used.
It is possible to build your environment and configure it in the same script using the Azure-CLI, Azure-Powershell or an other opensource solution like Terraform, But this is not best practice.
A lot of starter scripts are publicly available on github and in the Microsoft docs.
if you have any specific questions you can always send me a message, i am currently working on azure automation myself.
I would like to publish an Azure Managed Application to the Azure Marketplace. Is it possible to add to the "app.zip" an own PowerShell Script, which executes some additional deployment steps besides the Azure Resource Manager Template?
The Script would invoke the arm template and handle some outputs of the Template
The way to think about these is that you can only do tasks that can be done in a template. Today, there's no way to run an arbitrary script in an ARM template.
That help?
After some research and contacting the MS Support I found two possible solutions:
Using a VM with a Custom Script Extension. Downside: VM needs long to startup and is expensive if we do not delete it afterwards.
Using a Azure Container Instance to run the script. Starts up in about 45 seconds and doesn't cost anything if we don't use it. -> Tutorial
I need to start / stop a Azure virtual machine, but I don't know how I can do this. I don't know anything about Azure or Azure scripts.
You have several options.
The first one is by using the Azure Portal. Click on your VM and at the bottom you should see a button to stop the virtual machine. Starting it again works the same way.
The second option is to use PowerShell. John McCabe has posted a simple script on his blog to do this. The most important functions are Start-AzureVM and Stop-AzureVM.
If you want to automate this from a developer perspective (which needs a little bit more work than the PowerShell solution) is by using the Azure Management Libraries. Brady Gaster wrote a great post about it.
The fourth option is to use Microsoft Azure Automation. There you create runbooks for common tasks. In the samples are scripts already included which can start and stop a VM.
You can also create a Timer-triggered Azure Function to start/stop your VM. I've provided the steps here:
Azure Function role like permissions to Stop Azure Virtual Machines
Instead of creating HTTP-triggered Functions, just use Timer-triggered functions instead and edit the PowerShell script content to hardcode the vmname and resourcegroup.
within the Azure portal (I would recommend using the new portal), go into the VM you need, on the dashboard you will see a Stop and Start button, or you can choose to Restart the VM. Hope this helps.