Skip to content

Instantly share code, notes, and snippets.

@ndamulelonemakh
Last active October 4, 2023 07:27
Show Gist options
  • Save ndamulelonemakh/96b69dc69ca6fb4f010bdae1f974c356 to your computer and use it in GitHub Desktop.
Save ndamulelonemakh/96b69dc69ca6fb4f010bdae1f974c356 to your computer and use it in GitHub Desktop.
Azure automation templates
#!/usr/bin/env python3
from azure.mgmt.containerinstance import ContainerInstanceManagementClient
from azure.identity import DefaultAzureCredential
def start_container_instance(resource_group_name,
container_group_name,
subscription_id):
print(f'Attempting to start {container_group_name=} in {resource_group_name=}')
# Authenticate with Azure
credentials = DefaultAzureCredential()
aci_client = ContainerInstanceManagementClient(credential=credentials, subscription_id=subscription_id)
# Get the container group
container_group = aci_client.container_groups.get(resource_group_name, container_group_name)
# Check if the container group is already running
if container_group.containers[0].instance_view.current_state.state == 'Running':
print(f'Container group {container_group_name} is already running, no action')
else:
# Start the container group
poller = aci_client.container_groups.begin_start(resource_group_name, container_group_name)
print(f'Starting container group {container_group_name}..')
result = poller.result()
print("Start sequenece done, result =", result)
SUBSCRIPTION_ID=""
RESOURCE_GROUP=""
CONTAINER_INSTANCE=""
start_container_instance(RESOURCE_GROUP, CONTAINER_INSTANCE, SUBSCRIPTION_ID)
try
{
"Logging in to Azure..."
Connect-AzAccount -Identity
}
catch {
Write-Error -Message $_.Exception
throw $_.Exception
}
#Get all ARM resources from all resource groups
$ResourceGroups = Get-AzResourceGroup
foreach ($ResourceGroup in $ResourceGroups)
{
Write-Output ("Showing resources in resource group " + $ResourceGroup.ResourceGroupName)
$Resources = Get-AzResource -ResourceGroupName $ResourceGroup.ResourceGroupName
foreach ($Resource in $Resources)
{
Write-Output ($Resource.Name + " of type " + $Resource.ResourceType)
}
Write-Output ("")
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment