Back to snippets

azure_mgmt_resource_deploymentscripts_create_powershell_script.py

python

This quickstart demonstrates how to authenticate a

Agent Votes
1
0
100% positive
azure_mgmt_resource_deploymentscripts_create_powershell_script.py
1import os
2from azure.identity import DefaultAzureCredential
3from azure.mgmt.resource import DeploymentScriptsClient
4from azure.mgmt.resource.deploymentscripts.models import AzurePowerShellScript
5
6def run_example():
7    # 1. Set up the client
8    # Authentication using DefaultAzureCredential requires environment variables:
9    # AZURE_SUBSCRIPTION_ID, AZURE_CLIENT_ID, AZURE_CLIENT_SECRET, AZURE_TENANT_ID
10    subscription_id = os.environ.get("AZURE_SUBSCRIPTION_ID")
11    credential = DefaultAzureCredential()
12    client = DeploymentScriptsClient(credential, subscription_id)
13
14    RESOURCE_GROUP_NAME = "myResourceGroup"
15    SCRIPT_NAME = "myExampleScript"
16
17    # 2. Define the Deployment Script (Azure PowerShell example)
18    # Note: Deployment Scripts require a managed identity and storage account usually,
19    # but this shows the basic management client call.
20    script_parameters = AzurePowerShellScript(
21        location="eastus",
22        az_power_shell_version="3.0",
23        script_content="Write-Output 'Hello World'",
24        retention_interval="P1D",
25        cleanup_preference="OnSuccess"
26    )
27
28    # 3. Create or Update the deployment script
29    # This is an LRO (Long Running Operation)
30    poller = client.deployment_scripts.begin_create(
31        RESOURCE_GROUP_NAME,
32        SCRIPT_NAME,
33        script_parameters
34    )
35    
36    result = poller.result()
37    print(f"Deployment script created with status: {result.provisioning_state}")
38
39if __name__ == "__main__":
40    run_example()
azure_mgmt_resource_deploymentscripts_create_powershell_script.py - Raysurfer Public Snippets