-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathRestAPI-RunCommand-Example.ps1
More file actions
51 lines (38 loc) · 1.93 KB
/
RestAPI-RunCommand-Example.ps1
File metadata and controls
51 lines (38 loc) · 1.93 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
#Author gevivesh@microsoft.com
# How to Run commands and check the output on Azure Virtual Machine
# Example using the Azure REST API
# Example as-is please run this as your own risk
# I Used POST and GET to REST API to run commands
# https://learn.microsoft.com/en-us/rest/api/compute/virtual-machines/run-command?tabs=HTTP
# Login to Azure
Connect-AzAccount
#declare the variables
$VMNAME = "VMNAME"
$RGNAME = "RGNAME"
$COMMANDNAME = "COMMANDNAME"
$SCRIPT = "ipconfig" # this is the command that we are going to run https://learn.microsoft.com/en-us/windows-server/administration/windows-commands/ipconfig
$token = (Get-AzAccessToken).Token # We are using Azure Powershell module to get the Token you can get it from another place ;)
$location = "eastus2" # please add the region of where the VM is located
$subid = "ffff-xxxx-ffff-1111-subid" #add the sub id
#creating the header
$authHeader = @{
'Authorization' = 'Bearer ' + $token
'Content-Type' = 'application/json'
}
# SENDING THE COMMAND (AZURE RUN COMMAND)
$restUri = "https://management.azure.com/subscriptions/" + $subid + '/resourceGroups/' + $RGNAME + '/providers/Microsoft.Compute/virtualMachines/' + $VMNAME + '/runCommands/' + $COMMANDNAME + '?api-version=2021-07-01'
$requestbody = @{
"location" = $location
"properties" = @{
"source" = @{
"script" = $SCRIPT
}
}
}
$body = ($requestbody | ConvertTo-Json)
$response = Invoke-RestMethod -Uri $restUri -Method Put -Headers $authHeader -Body $body
# QUERY THE RESPONSE
$restUri = 'https://management.azure.com/subscriptions/' + $subid + '/resourceGroups/' + $RGNAME + '/providers/Microsoft.Compute/virtualMachines/' + $VMNAME + '/runCommands/' + $COMMANDNAME + '?$expand=instanceView&api-version=2022-08-01'
$response = Invoke-RestMethod -Uri $restUri -Method Get -Headers $authHeader
$response = $response | ConvertFrom-Json
$response.properties.instanceView