skip to Main Content

I want to create a VM without nsg. In the New-AzVm cmdlet, I specify $null in the PublicIP parameter – and it works (i.e. it does not create a Public IP). But this does not work with nsg. Here is my code:

$Date = Get-Date -Format "MMddyymm"
$user = "Sitecore"
$password = ConvertTo-SecureString "Qwerty123456" -AsPlainText -Force
$credential = New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList $user, $password
$RGroup = 'test'
$Location = "Westus3"
$VMName = "vmtest$date"
$image = "vmtest-image-20220805165444"
$vnet = "vnet1"
$NSG = "nsg$date"
$SizeVM = "Standard_b4ms"
$NIC = "$VMName$date"
$Subnet = "default"
$tags = @{'xxx' = 'yyy'}

New-AzVM -ResourceGroupName $RGroup `
    -Name $VMName `
    -Image $image `
    -Location $Location `
    -VirtualNetworkName $vnet `
    -SubnetName $Subnet `
    -SecurityGroupName $null `
    -PublicIpAddressName $null `
    -Credential $credential `
    -Size $SizeVM 

2

Answers


  1. Chosen as BEST ANSWER

    When we use new-azvm cmdlet, it doesn't work. Instead, create a VM without NSG. Only the Azure CLI can do this:

    az vm create --nsg '""' 
    

  2. First of all, you should know, that it is highly not recommended using no NSG. But if you need to do so, the only way to do it with Azure Powershell is (like @SaiSakethDuduru already said) with -nsg "". So need to use the stop-parsing affects variables.

    For additional information on parsing take a look in here:
    MS DOC about Parsing

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search