PowerShell in Azure with example to create a VM
- Install Latest powershell from Microsoft website
- On PowerShell CLI
- Check Version
$PSVersionTable.PSVersion
Get-InstalledModule -Name Az -AllVersions | select Name,Version
- Connect to account
- Connect-AzAccount – it will prompt to enter code in URL to have you logged in CLI.
- Change Subscription
Get-AzSubscription
- Change to another one
$context = Get-AzSubscription -SubscriptionID xxxxxxx-xxxxx-xxxx-8c74-xxxxxxx
Set-AzContext $context
Creating a VM In powershell
Get-AzResourceGroup - list of existing ResourceGroups
New-AzResourceGroup -Name resGRP0405 -Location EastUS
New-AzVM -ResourceGroupName "resGRP0405" -Name "vm0405" -Location "EastUS" -VirtualNetworkName "vnet0405" -SubnetName "default" -SecurityGroupName "secGRP0405" -PublicIpAddressName "myipaddr" -OpenPorts 80,443,3389
It will ask for a password for the VM which you want to set, provide the same.
Stop-AzVM -ResourceGroupName "resGRP0405" -Name "vm0405"
Start-AzVM -ResourceGroupName "resGRP0405" -Name "vm0405"