Prerequisites
- Azure Subscription: Ensure you have an active Azure subscription.
- PowerShell Installed: Make sure you have PowerShell installed on your local machine.
- Azure PowerShell Module: Install the Azure PowerShell module if you haven’t already. You can install it using the following command:
Step 1: Sign in to Your Azure Account
Before you can deploy resources, you need to authenticate to your Azure account. Use the following command.
Connect-AzAccount
This command will prompt you to enter your Azure credentials.
Step 2: Create a Resource Group
A resource group is a container that holds related resources for an Azure solution. You can create a resource group using the following command:
New-AzResourceGroup -Name "MyResourceGroup" -Location "EastUS"
Replace “MyResourceGroup” with your desired resource group name and “EastUS” with your preferred Azure region.
Step 3: Deploy Azure Resources
You can deploy various Azure resources, such as Virtual Machines, Storage Accounts, or Databases. Below are examples of how to deploy different resources.
3.1: Create a Virtual Machine
To create a Virtual Machine, you can use the following script
Set up a virtual network and a subnet for your VM:
$vnet = New-AzVirtualNetwork -ResourceGroupName "MyResourceGroup" -Location "EastUS" -Name "MyVNet" -AddressPrefix "10.0.0.0/16" $subnet = Add-AzVirtualNetworkSubnetConfig -Name "MySubnet" -AddressPrefix "10.0.0.0/24" -VirtualNetwork $vnet $vnet | Set-AzVirtualNetwork
Step 3.2: Create a Public IP Address
Now, create a public IP address for the VM:
$publicIp = New-AzPublicIpAddress -ResourceGroupName "MyResourceGroup" -Name "MyPublicIP" -Location "EastUS" -AllocationMethod Dynamic
Step 3.3: Create a Network Security Group (NSG)
Create a network security group to control inbound and outbound traffic:
$nsg = New-AzNetworkSecurityGroup -ResourceGroupName "MyResourceGroup" -Location "EastUS" -Name "MyNSG"
Step 3.4: Create a Network Interface
Create a network interface that connects the VM to the virtual network:
$nic = New-AzNetworkInterface -ResourceGroupName "MyResourceGroup" -Location "EastUS" -Name "MyNic" -PublicIpAddress $publicIp -NetworkSecurityGroup $nsg
Step 3.5: Define the Virtual Machine Configuration
Define the configuration for your new virtual machine:
$vmConfig = New-AzVMConfig -VMName "MyVM" -VMSize "Standard_DS1_v2" -ResourceGroupName "MyResourceGroup" -Location "EastUS" $vmConfig = Set-AzVMOperatingSystem -VM $vmConfig -Windows -ComputerName "MyVM" -Credential (Get-Credential) -ProvisionVMAgent -EnableAutoUpdate $vmConfig = Set-AzVMSourceImage -VM $vmConfig -PublisherName "MicrosoftWindowsServer" -Offer "WindowsServer" -Skus "2019-Datacenter" -Version "latest" $vmConfig = Add-AzVMNetworkInterface -VM $vmConfig -Id $nic.Id
Step 3.6: Create the Virtual Machine
Finally, create the virtual machine with the specified configuration:
New-AzVM -ResourceGroupName "MyResourceGroup" -Location "EastUS" -VM $vmConfig
Step 3.7: Verify the Deployment
After executing the command, you can verify that your VM has been created successfully:
Get-AzVM -ResourceGroupName "MyResourceGroup"
Example 2: Create a Storage Account
To create a Storage Account, you can use the following command:
$storageAccountName = "mystorageaccount2024" New-AzStorageAccount -ResourceGroupName $resourceGroupName -Name $storageAccountName -SkuName "Standard_LRS" -Location $location -Kind "StorageV2"
Step 4: Verify Deployment
After running your deployment script, you can verify that your resources have been created successfully:
Get-AzVM -ResourceGroupName $resourceGroupName Get-AzStorageAccount -ResourceGroupName $resourceGroupName
Step 5: Clean Up Resources
When you’re finished with your resources, you can remove them to avoid unnecessary charges:
Remove-AzResourceGroup -Name $resourceGroupName -Force