How to start Azure VM(Deallocated) use REST API - azure

I found a very strange problem here:
In Azure powershell, we can use
Start-AzureVM -ServiceName "mypc" -Name "mypc"
for both VM state= stop or stop(Deallocated).
But for Azure Mangement API
We can use start role only for VM state= stop
VM state=stop(deallocated) can't use that API..
How can I use REST API to start VM with State=Stop(deallocated)?
Thanks.

The Windows Azure PowerShell cmdlets use the Service Management REST API - but it uses an undocumented 2013-06-01 version. It is possible that this operation is available only in the undocumented version of the Service Management REST API.
You can see what the cmdlets actually do by using Fiddler to proxy the request - this gives you access to the operation invoked (URL) as well as the payload sent and received. Alternatively, you can look at the PowerShell cmdlets source which is available on GitHub.

POST https://management.core.windows.net/<subscription-id>/services/hostedservices/<service-name>/deployments/<deployment-name>/roleinstances/<role-name>/Operations
**x-ms-version: 2013-06-01**
<StartRoleOperation xmlns="http://schemas.microsoft.com/windowsazure" xmlns:i="http://www.w3.org/2001/XMLSchema-instance"><OperationType>StartRoleOperation</OperationType></StartRoleOperation>

public Task<ComputeLongRunningOperationResponse> StartVirtualMachineAsync(string subscriptionId, string name, string resource_group)
{
TokenCloudCredentials tokenCloudCredential = new TokenCloudCredentials(subscriptionId, token);
ComputeManagementClient computeManagementClient = new ComputeManagementClient(tokenCloudCredential);
return computeManagementClient.VirtualMachines.StartAsync(resource_group, name);
}

Related

Is there a REST API alternative to Set-AzSqlInstanceDatabaseBackupShortTermRetentionPolicy?

I am currently working on automating the setting of retention policies of databases within an Azure managed instance. At the moment I am using the Azure Set-AzSqlInstanceDatabaseBackupShortTermRetentionPolicy PowerShell Cmdlet. It would be preferable to use REST API for my automation workflow, is there an equivalent?
The retention policy is not part of the Managed Instanced - Update API. There is an equivalent API for single instance databases.
Any help in pointing me to an API would be appreciated.
this would be the api call:
/subscriptions/xxx/resourceGroups/yyy/providers/Microsoft.Sql/managedInstances/zzz/databases/uuu/backupShortTermRetentionPolicies/default?api-version=2017-03-01-preview
somehow I dont see this in the API reference. But you can always do something like this to figure it out:
Set-AzSqlInstanceDatabaseBackupShortTermRetentionPolicy -ResourceGroupName resourcegroup01 -InstanceName server01 -DatabaseName database01 -RetentionDays 35 -Debug
and just at the debug output, it will contain REST call url

Can custom console application be executed in Azure (PowerShell) Runbook?

Can I use my own/custom console application in Azure Runbook (PowerShell or any other)?
When I try to run my own console application (after retrieving it from Azure Storage), it fails.
When I try to execute it simply by:
.\myapp.exe
I get:
Program 'myapp.exe' failed to run: The operation was canceled by the user
When I use System.Diagnostics.Process:
$process = New-Object System.Diagnostics.Process
$process.StartInfo.FileName = $path
$process.StartInfo.UseShellExecute = $False
$process.Start()
I get a more meaningful error:
Exception calling "Start" with "0" argument(s): "This program is blocked by group policy. For more information, contact
your system administrator"
Is there any settings in Azure Automation, I can toggle, to allow executing custom applications? Or is it simply not possible in the restricted Runbook environment?
Thanks for reaching out! Unfortunately your ask of running .exe inside an Azure Automation runbook is currently not supported. And yes, you can go with Azure Web Job(s). One other customer has recently reached out with similar ask and solved their issue by leveraging Azure Web Job(s). For clarification, you may refer this MSDN thread. Hope this helps you.
Cheers!
Unfortunately , it is not supported by Azure Automation Runbook for now.Here is a feedback replied by Automation PG team , there is no update on it.
You can however
Run console app as Azure WebJob on App Service and call it remotely via SCM endpoint or,
Compile console application as PowerShell cmdlet
using System.Management.Automation;
namespace MyApp
{
[Cmdlet(VerbsCommunications.Send, "RunApplication")]
public class RunApplicationCommand : Cmdlet
{
protected override void ProcessRecord()
{
// App Code goes here
}
}
}
Attach compiled DLL to PowerShell module
Deploy PowerShell modle to Automation account using Modules > Import
Call cmdlet from runbook

How to run a remote command (powershell/bash) against an existing Azure VM in Azure Data Factory V2?

I've been trying to find a way to run a simple command against one of my existing Azure VMs using Azure Data Factory V2.
Options so far:
Custom Activity/Azure Batch won't let me add existing VMs to the pool
Azure Functions - I have not played with this but I have not found any documentation on this using AZ Functions.
Azure Cloud Shell - I've tried this using the browser UI and it works, however I cannot find a way of doing this via ADF V2
The use case is the following:
There are a few tasks that are running locally (Azure VM) in task scheduler that I'd like to orchestrate using ADF as everything else is in ADF, these tasks are usually python applications that restore a SQL Backup and or purge some folders.
i.e. sqdb-restore -r myDatabase
where sqldb-restore is a command that is recognized locally after installing my local python library. Unfortunately the python app needs to live locally in the VM.
Any suggestions? Thanks.
Thanks to #martin-esteban-zurita, his answer helped me to get to what I needed and this was a beautiful and fun experiment.
It is important to understand that Azure Automation is used for many things regarding resource orchestration in Azure (VMs, Services, DevOps), this automation can be done with Powershell and/or Python.
In this particular case I did not need to modify/maintain/orchestrate any Azure resource, I needed to actually run a Bash/Powershell command remotely into one of my existing VMs where I have multiple Powershell/Bash commands running recurrently in "Task Scheduler".
"Task Scheduler" was adding unnecessary overhead to my data pipelines because it was unable to talk to ADF.
In addition, Azure Automation natively only runs Powershell/Python commands in Azure Cloud Shell which is very useful to orchestrate resources like turning on/off Azure VMs, adding/removing permissions from other Azure services, running maintenance or purge processes, etc, but I was still unable to run commands locally in an existing VM. This is where the Hybrid Runbook Worker came into to picture. A Hybrid worker group
These are the steps to accomplish this use case.
1. Create an Azure Automation Account
2. Install the Windows Hybrid Worker in my existing VM . In my case it was tricky because my proxy was giving me some errors. I ended up downloading the Nuget Package and manually installing it.
.\New-OnPremiseHybridWorker.ps1 -AutomationAccountName <NameofAutomationAccount> -AAResourceGroupName <NameofResourceGroup>
-OMSResourceGroupName <NameofOResourceGroup> -HybridGroupName <NameofHRWGroup>
-SubscriptionId <AzureSubscriptionId> -WorkspaceName <NameOfLogAnalyticsWorkspace>
Keep in mind that in the above code, you will need to find your own parameter values, the only parameter that does not have to be found and will be created is HybridGroupName this will define the name of the Hybrid Group
3. Create a PowerShell Runbook
[CmdletBinding()]
Param
([object]$WebhookData) #this parameter name needs to be called WebHookData otherwise the webhook does not work as expected.
$VerbosePreference = 'continue'
#region Verify if Runbook is started from Webhook.
# If runbook was called from Webhook, WebhookData will not be null.
if ($WebHookData){
# Collect properties of WebhookData
$WebhookName = $WebHookData.WebhookName
# $WebhookHeaders = $WebHookData.RequestHeader
$WebhookBody = $WebHookData.RequestBody
# Collect individual headers. Input converted from JSON.
$Input = (ConvertFrom-Json -InputObject $WebhookBody)
# Write-Verbose "WebhookBody: $Input"
#Write-Output -InputObject ('Runbook started from webhook {0} by {1}.' -f $WebhookName, $From)
}
else
{
Write-Error -Message 'Runbook was not started from Webhook' -ErrorAction stop
}
#endregion
# This is where I run the commands that were in task scheduler
$callBackUri = $Input.callBackUri
# This is extremely important for ADF
Invoke-WebRequest -Uri $callBackUri -Method POST
4. Create a Runbook Webhook pointing to the Hybrid Worker's VM
4. Create a webhook activity in ADF where the above PowerShell runbook script will be called via a POST Method
Important Note: When I created the webhook activity it was timing out after 10 minutes (default), so I noticed in the Azure Automation Account that I was actually getting INPUT data (WEBHOOKDATA) that contained a JSON structure with the following elements:
WebhookName
RequestBody (This one contains whatever you add in the Body plus a default element called callBackUri)
All I had to do was to invoke the callBackUri from Azure Automation. And this is why in the PowerShell runbook code I added Invoke-WebRequest -Uri $callBackUri -Method POST. With this, ADF was succeeding/failing instead of timing out.
There are many other details that I struggled with when installing the hybrid worker in my VM but those are more specific to your environment/company.
This looks like a use case that is supported with Azure Automation, using a hybrid worker. Try reading here: https://learn.microsoft.com/en-us/azure/automation/automation-hybrid-runbook-worker
You can call runbooks with webhooks in ADFv2, using the web activity.
Hope this helped!

Azure Monitoring Service API - Deployment Name parameter

I'm using Azure Monitoring Service API and need to pass DEPLOYTMENT NAME as a parameter to BuildVirtualMachineResourceId API method.
At the moment its not clear to me where/how to locate this piece of information so it can be passed to the method. Both cloud service name and vm name are easily available.
String vmResourceId = ResourceIdBuilder.BuildVirtualMachineResourceId(
CLOUD_SERVICE_NAME, DEPLOYMENT_NAME, VM_NAME);
Try Deployment ID... should be available on your Azure portal under the "Dashboard" screen
Use Get-AzureDeployment Cmdlet that gives you the deployment name. More details: http://msdn.microsoft.com/en-us/library/azure/dn495146.aspx

Trying to Create a Virtual Machine Deployment in Azure via Service Management API

Trying to Create a Virtual Machine Deployment in Azure via Service Management API.
But getting the 404 http error code , though the uri is correct.I am correctly setting the values in the below format,
https://management.core.windows.net//services/hostedservices//deployments/
Anything else is missing ? Also am setting the certificates for SSL authentication.All other services seems to work but this alone is throwing a 404 http error
you mentioned you are using this url:
https://management.core.windows.net//services/hostedservices//deployments/
are you inputing your subscription-id and hosted service on which you wish to deploy?
the url should look like this:
https://management.core.windows.net/<subscription-id>/services/hostedservices/<service-name>/deployments/
taken from http://msdn.microsoft.com/en-us/library/windowsazure/jj157194
though when using the above URL i was also getting 404 responses, i needed to remove the trailing slash at the end. (".../deployments" , not ".../deployments/")
after this i started getting BadRequest Responses but this is probably just because of the XML body not being generated properly.
hope it helps
I think you are asking about Windows Azure Virtual Machines. In that case you might not be using the correct newer Powershell cmdlets to create Azure Virtual Machines because when you make new Virtual Machine cmdlets you don't need to use the management URL. It is all done during very first call when you configure connection using PublishSettings file and then set your subscription. The steps to create a new Azure Virtual Machine using Powershell cmdlets are as below:
Get-AzurePublishSettingsFile (documentation is here)
Import-AzurePublishSettingsFile azuresettings.publishsettings
New-AzureVM ** (documentation/sample script is here)
If you are using PaaS Windows Azure Web/Worker Role then you will use:
New-AzureDeployment (documented here)
A list of all new Windows Azure Powershell CMDlets is documented here.
Finally it worked.There are some errors in MSDN documentation.
uri:https://management.core.windows.net/subscriptionID/services/hostedservices/servicename/deployments
No slash at the end, but MSDN documentation gives a slash at the end.

Resources