Add a PowerShell script to Azure Marketplace Offer - azure

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

Related

Delete an Azure Virtual Machine automatically after deployment

To deploy my infrastructure I need to deploy a VM with a custom script extension. The only purpose of the VM, is to execute the script. After the execution of the script the VM should be deleted automatically.
How can this be done?
Additional information:
This is an azure resource manager deployment
the deletion should work in the azure marketplace environment as well.
this probably means you are doing something wrong, you can use Azure Container Instance to run the script and shutdown. it should work with marketplace as well (as far as I know you can have custom container in marketplace offerings).
Marketplace only allows you to use arm templates to deploy stuff, so you cannot really do what you are asking with an arm template. well, you might be able to hack something like that with nested deployments and complete mode, but I doubt that will pass moderation in marketplace.
technically, you can make vm delete itself as a part of the script. again, not something I would advise.

How to run powershell script in azure functions in schedule

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!!

Provision a VM as part of an Azure Devops build pipeline

I have a build pipeline that is working pretty well currently in Azure DevOps. As part of the pipline/build process, I create an artifact, which is published and reachable. After that, I'd like to do the following:
Create/Start Up a new VM (Windows)
Grab the now published artifact, unzip it and run the executable within
Run the integration tests
Close the VM
I've looked around the Azure documentation but cannot find much that discusses this sort of solution. Please help!
There is nothing built-in (like a readymade task create a vm), so you can use any way to create a VM in Azure. Azure powershell, Azure Cli, ARM Templates, SDK calls. whatever works for you.
You would need to open ssh\winrm to talk to that vm to deploy stuff to it. thats about it. You can find lots of examples on how to create a VM online. VSTS got tasks for Azure Powershell\Cli\ARM Tempaltes so you dont need to handle auth.
You can create a VM using ARM templates with the task 'Azure Resource Group Deployment'
With a separate task 'Powershell on target machine' you can run a powershell script on the target VM, if you put the downloading, unzipping and running of this exe in this script you should be able to perform the tasks you need.
You could also look into the 'invoke-azurermvmruncommand' powershell command, this allows you to run a powershell script in the vm. https://learn.microsoft.com/en-us/powershell/module/azurerm.compute/invoke-azurermvmruncommand?view=azurermps-6.11.0

Run a script on VM before it's deleted in Azure

Is there a way to run bash script on a VM before it's deleted from Azure? I know you can use CustomScriptExtension for running script when VM is created or started but I couldn't find any information about launching cleanup scripts before VM is deleted.
No, at the time of writing Azure does not support actions like this.
Depending on what you want to do, you may find it useful to subscribe to VM alerts, which can include when it is deleted. You'll have the option to call a webhook that may give you the flexibility to do what you need. Try working within the Azure portal Monitoring Blade to create your own Alert to handle this.

Start and Stop Azure Virtual Machine

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.

Resources