Hi Sheetal Jain,
Could you please deploy this entire script by directly copying and pasting it into PowerShell and running it first for testing purposes? If everything is deployed as expected, you can delete the resource immediately. Later, you can customize the naming convention according to your preferences.
$resourceGroupName = "Akshay"
$location = "East US"
$vmName = "TestVM"
$vmSize = "Standard_DS2_v2"
$adminUsername = "Akshay"
$adminPassword = ConvertTo-SecureString -String "Akshaykumar@123456" -AsPlainText -Force
$availabilitySetName = "dcAvailabilitySet"
New-AzResourceGroup -Name $resourceGroupName -Location $location
$exSubnet = New-AzVirtualNetworkSubnetConfig -Name EXSrvrSubnet -AddressPrefix "10.0.0.0/24"
$vnet = New-AzVirtualNetwork -ResourceGroupName $resourceGroupName -Location $location -Name "EXSrvrVnet" -AddressPrefix "10.0.0.0/16" -Subnet $exSubnet -DNSServer "10.0.0.4"
$rule1 = New-AzNetworkSecurityRuleConfig -Name "RDPTraffic" -Description "Allow RDP to all VMs on the subnet" -Access Allow -Protocol Tcp -Direction Inbound -Priority 100 -SourceAddressPrefix Internet -SourcePortRange * -DestinationAddressPrefix * -DestinationPortRange 3389
$rule2 = New-AzNetworkSecurityRuleConfig -Name "ExchangeSecureWebTraffic" -Description "Allow HTTPS to the Exchange server" -Access Allow -Protocol Tcp -Direction Inbound -Priority 101 -SourceAddressPrefix Internet -SourcePortRange * -DestinationAddressPrefix "10.0.0.5/32" -DestinationPortRange 443
$nsg = New-AzNetworkSecurityGroup -Name "EXSrvrSubnetNSG" -ResourceGroupName $resourceGroupName -Location $location -SecurityRules $rule1, $rule2
$vnet = Get-AzVirtualNetwork -ResourceGroupName $resourceGroupName -Name "EXSrvrVnet"
Set-AzVirtualNetworkSubnetConfig -VirtualNetwork $vnet -Name "EXSrvrSubnet" -AddressPrefix "10.0.0.0/24" -NetworkSecurityGroup $nsg
$vnet | Set-AzVirtualNetwork
$publicIp = New-AzPublicIpAddress -ResourceGroupName $resourceGroupName -Location $location -Name "$vmName-ip" -AllocationMethod Static
$nic = New-AzNetworkInterface -ResourceGroupName $resourceGroupName -Location $location -Name "$vmName-nic" -SubnetId $vnet.Subnets[0].Id -PublicIpAddressId $publicIp.Id
$avSet = New-AzAvailabilitySet -ResourceGroupName $resourceGroupName -Location $location -Name $availabilitySetName -Sku Aligned -PlatformUpdateDomainCount 5 -PlatformFaultDomainCount 2
$vmConfig = New-AzVMConfig -VMName $vmName -VMSize $vmSize -AvailabilitySetId $avSet.Id
$vmConfig = Set-AzVMOperatingSystem -VM $vmConfig -Windows -ComputerName $vmName -Credential (New-Object System.Management.Automation.PSCredential($adminUsername, $adminPassword)) -ProvisionVMAgent -EnableAutoUpdate
$vmConfig = Set-AzVMSourceImage -VM $vmConfig -PublisherName "MicrosoftWindowsServer" -Offer "WindowsServer" -Skus "2019-Datacenter" -Version "latest"
$vmConfig = Add-AzVMNetworkInterface -VM $vmConfig -Id $nic.Id
$vm = New-AzVM -ResourceGroupName $resourceGroupName -Location $location -VM $vmConfig
Screenshot for you reference
If you still get any errors in this script tag me comment will assist you how to address the issue
If it is working for you and if it was helpful, could you please click an accept and answer and upvote for other community reference