Azure PowerShell Examples

Explore practical examples of using Azure PowerShell to manage your cloud resources. These examples cover common scenarios and demonstrate the power and flexibility of Azure automation.

Getting Started

Before you begin, ensure you have the Azure PowerShell module installed and are logged in to your Azure account using Connect-AzAccount.

# Connect to your Azure account Connect-AzAccount # Set your subscription context if you have multiple # Set-AzContext -SubscriptionId "YOUR_SUBSCRIPTION_ID"

Virtual Machine Creation

Create a new Windows Virtual Machine in a specified resource group and location.

# Define variables $resourceGroupName = "MyResourceGroup" $location = "East US" $vmName = "MyWebAppVM" $adminUsername = "azureuser" $adminPassword = "P@sswOrd12345!" # Create a resource group if it doesn't exist New-AzResourceGroup -Name $resourceGroupName -Location $location # Create a virtual machine New-AzVM -ResourceGroupName $resourceGroupName ` -Name $vmName ` -Location $location ` -VirtualNetworkNetworkName "MyVNet" ` -SubnetName "MySubnet" ` -SecurityGroupName "MyNetworkSecurityGroup" ` -PublicIpAddressName "MyPublicIp" ` -ImageName "Win2019Datacenter" ` -Credential (Get-Credential $adminUsername $adminPassword) ` -OpenPorts 3389 # RDP port

Storage Account Management

Create a general-purpose v2 storage account.

# Define variables $storageResourceGroup = "MyStorageRG" $storageLocation = "West US 2" $storageAccountName = "mystorageaccount" + (Get-Random -Maximum 99999) # Unique name $skuName = "Standard_LRS" # Locally Redundant Storage # Create storage account New-AzStorageAccount -ResourceGroupName $storageResourceGroup ` -Name $storageAccountName ` -Location $storageLocation ` -SkuName $skuName ` -Kind StorageV2 # Get storage account key $storageKey = Get-AzStorageAccountKey -ResourceGroupName $storageResourceGroup -Name $storageAccountName $storageKeyName = $storageKey[0].KeyName # Get connection string $connectionString = "DefaultEndpointsProtocol=https;AccountName=$storageAccountName;AccountKey=$($storageKey[0].Value);EndpointSuffix=core.windows.net" Write-Host "Storage Account Name: $($storageAccountName)" Write-Host "Storage Account Key: $($storageKey[0].Value)" Write-Host "Connection String: $($connectionString)"

Web App Deployment

Create an App Service Plan and a Web App, then deploy code from a local path.

# Define variables $webAppResourceGroup = "MyWebAppRG" $webAppLocation = "North Europe" $appServicePlanName = "MyWebAppPlan" $webAppName = "myuniquewebapp" + (Get-Random -Maximum 99999) $sourcePath = "C:\path\to\your\web\app\files" # Path to your web app files # Create resource group New-AzResourceGroup -Name $webAppResourceGroup -Location $webAppLocation # Create App Service Plan New-AzAppServicePlan -ResourceGroupName $webAppResourceGroup ` -Name $appServicePlanName ` -Location $webAppLocation ` -Tier Free ` -WorkerSize Small # Create Web App New-AzWebApp -ResourceGroupName $webAppResourceGroup ` -Name $webAppName ` -Location $webAppLocation ` -AppServicePlan $appServicePlanName # Deploy code (using Zip Deploy) Publish-AzWebApp -ResourceGroupName $webAppResourceGroup ` -Name $webAppName ` -PackagePath $sourcePath

Network Security Group

Create a Network Security Group (NSG) and associate it with a subnet.

# Define variables $nsgResourceGroup = "MyNetRG" $nsgLocation = "Southeast Asia" $nsgName = "MyWebAppNSG" $vNetName = "MyVNet" $subnetName = "MySubnet" # Create resource group New-AzResourceGroup -Name $nsgResourceGroup -Location $nsgLocation # Create a virtual network and subnet $vnet = New-AzVirtualNetwork -Name $vNetName ` -ResourceGroupName $nsgResourceGroup ` -Location $nsgLocation ` -AddressPrefix "10.0.0.0/16" $subnet = Add-AzVirtualNetworkSubnetConfig -Name $subnetName ` -VirtualNetwork $vnet ` -AddressPrefix "10.0.0.0/24" Set-AzVirtualNetwork -VirtualNetwork $vnet # Create NSG $nsg = New-AzNetworkSecurityGroup -Name $nsgName ` -ResourceGroupName $nsgResourceGroup ` -Location $nsgLocation # Associate NSG with subnet Set-AzVirtualNetworkSubnetConfig -Name $subnetName ` -VirtualNetwork $vnet ` -AddressPrefix "10.0.0.0/24" ` -NetworkSecurityGroup $nsg Set-AzVirtualNetwork -VirtualNetwork $vnet Write-Host "NSG '$nsgName' created and associated with subnet '$subnetName'."

Azure SQL Database

Create an Azure SQL server and a database.

# Define variables $sqlResourceGroup = "MySqlRG" $sqlLocation = "Central India" $sqlServerName = "myazuresqlserver" + (Get-Random -Maximum 99999) $sqlAdminName = "sqladmin" $sqlAdminPassword = "ComplexP@sswOrd987!" $databaseName = "MySampleDatabase" $location = "Central India" # Create resource group New-AzResourceGroup -Name $sqlResourceGroup -Location $sqlLocation # Create SQL server New-AzSqlServer -ServerName $sqlServerName ` -ResourceGroupName $sqlResourceGroup ` -Location $sqlLocation ` -ServerAdminLogin $sqlAdminName ` -ServerAdminPassword $sqlAdminPassword # Create SQL Database New-AzSqlDatabase -ResourceGroupName $sqlResourceGroup ` -ServerName $sqlServerName ` -DatabaseName $databaseName ` -Edition Basic ` -RequestedServiceObjectiveName Basic Write-Host "Azure SQL Server '$sqlServerName' and database '$databaseName' created successfully."

Monitoring and Diagnostics

Enable diagnostics settings for a resource to send logs and metrics to a Log Analytics workspace.

# Define variables $diagResourceGroup = "MyMonitoringRG" $diagResourceName = "MyWebAppVM" # The resource to monitor $logAnalyticsWorkspaceName = "MyLogAnalyticsWorkspace" $workspaceResourceGroup = "MyMonitoringRG" # Workspace can be in the same RG # Get the resource to configure $vm = Get-AzVM -ResourceGroupName $diagResourceGroup -Name $diagResourceName # Get or create Log Analytics Workspace $workspace = Get-AzOperationalInsightsWorkspace -Name $logAnalyticsWorkspaceName -ResourceGroupName $workspaceResourceGroup -ErrorAction SilentlyContinue if (-not $workspace) { $workspace = New-AzOperationalInsightsWorkspace -Name $logAnalyticsWorkspaceName -Location $diagResourceGroup ` -ResourceGroupName $workspaceResourceGroup } # Enable diagnostics Set-AzDiagnosticSettings -ResourceId $vm.Id ` -WorkspaceId $workspace.ResourceId ` -Enabled $true ` -Categories "Administrative", "Performance", "Write" # Example categories Write-Host "Diagnostics enabled for '$diagResourceName' sending to '$logAnalyticsWorkspaceName'."