Skip to content

Instantly share code, notes, and snippets.

@blakedrumm
Last active April 9, 2024 04:02
Show Gist options
  • Save blakedrumm/a2477ac427f080e9925d6ff412a5f812 to your computer and use it in GitHub Desktop.
Save blakedrumm/a2477ac427f080e9925d6ff412a5f812 to your computer and use it in GitHub Desktop.
This script allows you to run a command on an Azure Arc machine using the API.
# ============================================================================
# Name: Run command on Azure Arc machine
# ============================================================================
# Author: Blake Drumm (blakedrumm@microsoft.com)
# Website: https://blakedrumm.com/
# Date created: April 4th, 2024
# ============================================================================
$AzureRmContext = Connect-AzAccount
if (-not $AzureRmContext)
{
Write-Error "Failed to authenticate to Azure."
return
}
# Null out the response
$Response = $null
# Get the access token
$AccessToken = (Get-AzAccessToken -ResourceUrl https://management.azure.com).Token
# Define the subscription ID, resource group, and machine name
$SubscriptionId = "cc2ebf7f-fc13-4b4f-8a19-5f0e37f10aed"
$ResourceGroupName = "ResourceGroupName"
$MachineName = "AGENT01-2019"
$Location = "eastus" # Ensure to use the correct location for your resource
# Formulate the REST API URL for the runCommand operation on a connected machine
$RunCommandName = "RunCommandName"
$ApiVersion = "2023-10-03-preview" # Make sure to use the current API version
$Url = "https://management.azure.com/subscriptions/$SubscriptionId/resourceGroups/$ResourceGroupName/providers/Microsoft.HybridCompute/machines/$MachineName/runCommands/$RunCommandName`?api-version=$ApiVersion"
# Prepare the header with the access token
$Headers = @{
'Authorization' = "Bearer $AccessToken"
'Content-Type' = 'application/json'
}
# Constructing the body with the command
$Body = @{
location = $Location
properties = @{
source = @{
script = "echo `"`$(Get-Date) - Hello World! Running on `$env:COMPUTERNAME !`""
}
}
} | ConvertTo-Json
# Send the PUT request to execute the command
$Response = Invoke-RestMethod -Uri $Url -Method Put -Headers $Headers -Body $Body
# Output the response
$Response
# ------------------------------------------------------------------------------------
#
# Get status of command executed above
#
if (-not $AzureRmContext)
{
Write-Error "Failed to authenticate to Azure."
return
}
$x = 0
do
{
# Send the GET request to retrieve the command details
$Response = Invoke-RestMethod -Uri $Url -Method Get -Headers $Headers
#cls
# Output the response
$Response.properties
# Increase the count
$x++
Write-Output "`n`nChecked $x times"
sleep -seconds 5
}
until ($Response.Properties.provisioningState -notmatch "Creating|Updating")
<#
Copyright (c) Microsoft Corporation. MIT License
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
#>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment