How to create multiple Subnets in pre-existing Vnet Powershell - azure

I have the following code which works good for adding a few subnets but I would like for this script to leverage a CSV file to import from and add the subnets into a pre-existing Vnet?
$appssubnet = New-AzVirtualNetworkSubnetConfig -Name servers -AddressPrefix "172.16.1.0/24" -NetworkSecurityGroupId "/subscriptions/xxxxxxxxxx-xxxx-xxxx-xxxxx-xxxxxxxxxxxx/resourceGroups/powershell-grp/providers/Microsoft.Network/networkSecurityGroups/app-nsg1"`
-RouteTableId "/subscriptions/xxxxxxxxxx-xxxx-xxxx-xxxxx-xxxxxxxxxxxx/resourceGroups/powershell-grp/providers/Microsoft.Network/routeTables/powershell-rt"
$serversubnet = New-AzVirtualNetworkSubnetConfig -Name apps -AddressPrefix "172.16.2.0/24" -RouteTableId "/subscriptions/xxxxxxxxxx-xxxx-xxxx-xxxxx-xxxxxxxxxxxx/resourceGroups/powershell-grp/providers/Microsoft.Network/routeTables/powershell-rt"
$dmz = New-AzVirtualNetworkSubnetConfig -Name dmz -AddressPrefix "172.16.3.0/24"
$updatedvnet = New-AzVirtualNetwork -Name "testsubnet" -ResourceGroupName "powershell-grp" -Location "North Europe" -AddressPrefix "172.16.0.0/16" -Subnet $serversubnet, $dmz, $appssubnet -Force:$true
$updatedvnet | Set-AzVirtualNetwork

You can use the below powershell script.
$subnets1 = Import-Csv "C:\Users\v-XXXsXX18\Documents\TestCount.csv"
#$subnets1.subnetName
foreach ($subnet in $subnets1){
$dmz = New-AzVirtualNetworkSubnetConfig -Name $subnet.SubnetName -AddressPrefix $subnet.AddressPrefix
$vnet=Get-AzVirtualNetwork -Name "MyVirtualNetworkTes" -ResourceGroupName "v-raXXXXndtree"
$updatedvnet=Add-AzVirtualNetworkSubnetConfig -Name $dmz.Name -VirtualNetwork $vnet -AddressPrefix $dmz.AddressPrefix
$updatedvnet | Set-AzVirtualNetwork
}
Output--

Related

argument is null or empty - start-job

I am getting an argument is null error while trying to execute below command. Is there a way to can call the variable inside start-job
$RG = "xx"
$Location1 = "xx"
$VNET1="xx"
$PublicIP1="xx"
$VNGW1 = "xx"
Start-Job {
New-AzVirtualNetworkGateway -Name $VNGW1 -ResourceGroupName $RG -Location $Location1 -GatewayType Vpn -VpnType RouteBased -GatewaySku VpnGw1 -Asn 65511 -IpConfigurations $gwipconf1 -EnableBgp $True
}
Cannot validate argument on parameter 'Name'. The argument is null or empty. Provide an argument that is not null or empty, and then try the command again.
Cannot validate argument on parameter 'Name'. The argument is null or
empty. Provide an argument that is not null or empty, and then try the
command again.
Without the Argument list in Start-Job cmdlet, we cannot use the variables.
We have created a PowerShell script & tested the below scripts in our environment which is working fine. We can create Virtual Network Gateway using start-job in below two ways:
Using Variables as Argument List
Using Script in Start-Job
Using Variables as Argument List:
Firstly, We have created all the dependency resources for Virtual Network Gateway using below cmdlets:
$subnet = New-AzVirtualNetworkSubnetConfig -Name 'gatewaysubnet' -AddressPrefix '10.254.0.0/27'
$ngwpip = New-AzPublicIpAddress -Name ngwpip -ResourceGroupName "vnet-gateway" -Location "UK West" -AllocationMethod Dynamic
$vnet = New-AzVirtualNetwork -AddressPrefix "10.254.0.0/27" -Location "UK West" -Name vnet-gateway -ResourceGroupName "vnet-gateway" -Subnet $subnet
$subnet = Get-AzVirtualNetworkSubnetConfig -name 'gatewaysubnet' -VirtualNetwork $vnet
$ngwipconfig = New-AzVirtualNetworkGatewayIpConfig -Name ngwipconfig -SubnetId $subnet.Id -PublicIpAddressId $ngwpip.Id
Once the dependencies are created, we used the below Start-Job cmdlet for the creation of Virtual Network Gateway:
$RG = "xx"
$Location = "xx"
$gwipconf = "xx"
$VNGW = "xx"
Start-Job {
New-AzVirtualNetworkGateway -Name $args[3] -ResourceGroupName $args[0] -Location $ args[1] -GatewayType Vpn -VpnType RouteBased -GatewaySku VpnGw1 -Asn 65511 -IpConfigurations $ args[2] -EnableBgp $True
} -ArgumentList #($RG, $Location, $gwipconf, $VNGW)
Using Script in Start-Job:
Here, we have created a PowerShell file (gateway.ps1) & stored in local machine which contains the dependent resources creation followed by virtual network gateway creation(using the start-job).
New-AzResourceGroup -Location "UK West" -Name "vnet-gateway"
$subnet = New-AzVirtualNetworkSubnetConfig -Name 'gatewaysubnet' -AddressPrefix '10.254.0.0/27'
$ngwpip = New-AzPublicIpAddress -Name ngwpip -ResourceGroupName "vnet-gateway" -Location "UK West" -AllocationMethod Dynamic
$vnet = New-AzVirtualNetwork -AddressPrefix "10.254.0.0/27" -Location "UK West" -Name vnet-gateway -ResourceGroupName "vnet-gateway" -Subnet $subnet
$subnet = Get-AzVirtualNetworkSubnetConfig -name 'gatewaysubnet' -VirtualNetwork $vnet
$ngwipconfig = New-AzVirtualNetworkGatewayIpConfig -Name ngwipconfig -SubnetId $subnet.Id -PublicIpAddressId $ngwpip.Id
New-AzVirtualNetworkGateway -Name myNGW -ResourceGroupName vnet-gateway -Location "UK West" -IpConfigurations $ngwIpConfig -GatewayType "Vpn" -VpnType "RouteBased" -GatewaySku "Basic" -CustomRoute 192.168.0.0/24
Now, We have passed the above powershell script file path as an argument to the Start-job to create VPN gateway:
start-job -filepath \<pathofPowershellScript>
Here is the sample output for the reference :

Can't create a Databricks from a powershell script

My issue
Can't create a databricks from script. Always getting this error message:
Private and public subnets should be unique for the Virtual Network
'/subscriptions/XXXX/resourceGroups/rgtest64/providers/Microsoft.Network/virtualNetworks/vnet64'.
Public subnet name: 'privsub64', Private subnet name:
| 'privsub64'
How to reproduce
Just running this script:
$location="westeurope"
$rg = "rgtest64"
$vnet = "vnet64"
New-AzResourceGroup -name $rg -Location westeurope
$dlg = New-AzDelegation -Name dbrdl -ServiceName "Microsoft.Databricks/workspaces"
$rdpRule = New-AzNetworkSecurityRuleConfig -Name rdp-rule -Description "Allow RDP" -Access Allow `
-Protocol Tcp -Direction Inbound -Priority 100 -SourceAddressPrefix Internet `
-SourcePortRange * -DestinationAddressPrefix * -DestinationPortRange 3389
$networkSecurityGroup = New-AzNetworkSecurityGroup -ResourceGroupName $rg `
-Location $location -Name nsg-test -SecurityRules $rdpRule
$privSubnet = New-AzVirtualNetworkSubnetConfig -Name privsub64 -AddressPrefix "10.0.1.0/24" `
-NetworkSecurityGroup $networkSecurityGroup -Delegation $dlg
$pubSubnet = New-AzVirtualNetworkSubnetConfig -Name pubsub64 -AddressPrefix "10.0.2.0/24" `
-NetworkSecurityGroup $networkSecurityGroup -Delegation $dlg
$testVN = New-AzVirtualNetwork -Name $vnet -ResourceGroupName $rg `
-Location $location -AddressPrefix "10.0.0.0/16" -Subnet $privSubnet,$pubSubnet
New-AzDatabricksWorkspace -Name databricks-test-with-custom-vn -ResourceGroupName $rg -Location $location `
-VirtualNetworkId $testVN.Id -PrivateSubnetName $privSubnet.Name -PublicSubnetName `
$privSubnet.Name -Sku standard
PSVersion: 7.1.3
PSEdition: Core
What I tested
I tested the script on several subscriptions.
I also tried without creating the subnets, same result.
I tested from an ARM template, same result.
It is working if I don't reference any VNET, but for internal reasons I need to impose a specific VNET.
What I need
Understanding what happen, and what to do
Everything was working until a few days ago. I don't understand
thanks
According to the error, your public subnet,s name and your private subnet name are the same. Whew we deploy Databricks workspace into vnet, the two subnets should be different. Please update your script as
New-AzDatabricksWorkspace -Name databricks-test-with-custom-vn -ResourceGroupName $rg -Location $location `
-VirtualNetworkId $testVN.Id -PrivateSubnetName $privSubnet.Name -PublicSubnetName `
$pubSubnet.Name -Sku standard
.
The whole script is as below
$location="westeurope"
$rg = "rgtest64"
$vnet = "vnet64"
New-AzResourceGroup -name $rg -Location westeurope
$dlg = New-AzDelegation -Name dbrdl -ServiceName "Microsoft.Databricks/workspaces"
$rdpRule = New-AzNetworkSecurityRuleConfig -Name rdp-rule -Description "Allow RDP" -Access Allow `
-Protocol Tcp -Direction Inbound -Priority 100 -SourceAddressPrefix Internet `
-SourcePortRange * -DestinationAddressPrefix * -DestinationPortRange 3389
$networkSecurityGroup = New-AzNetworkSecurityGroup -ResourceGroupName $rg `
-Location $location -Name nsg-test -SecurityRules $rdpRule
$privSubnet = New-AzVirtualNetworkSubnetConfig -Name privsub64 -AddressPrefix "10.0.1.0/24" `
-NetworkSecurityGroup $networkSecurityGroup -Delegation $dlg
$pubSubnet = New-AzVirtualNetworkSubnetConfig -Name pubsub64 -AddressPrefix "10.0.2.0/24" `
-NetworkSecurityGroup $networkSecurityGroup -Delegation $dlg
$testVN = New-AzVirtualNetwork -Name $vnet -ResourceGroupName $rg `
-Location $location -AddressPrefix "10.0.0.0/16" -Subnet $privSubnet,$pubSubnet
New-AzDatabricksWorkspace -Name databricks-test-with-custom-vn -ResourceGroupName $rg -Location $location `
-VirtualNetworkId $testVN.Id -PrivateSubnetName $privSubnet.Name -PublicSubnetName `
$pubSubnet.Name -Sku standard

How to add add additonal ip to a Network Interface Card in azure

I have a set of Private Ip address that i want to add to a Nic in Azure. I am running into error on adding the IP's to Nic.
set-AzContext -subscription "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
$vnet = Get-AzVirtualNetwork -Name "rosvnet" -ResourceGroupName "sample"
$subnet = Get-AzVirtualNetworkSubnetConfig -Name "default" -VirtualNetwork $vnet
$Nic = Get-AzNetworkInterface -ResourceGroupName "sample" -Name "closer-nic"
$Nic | New-AzNetworkInterfaceIpConfig -Name "ipconfig1" -PrivateIpAddress "10.1.0.11" -Subnet $subnet
$Nic | New-AzNetworkInterfaceIpConfig -Name "ipconfig2" -PrivateIpAddress "10.1.0.13" -Subnet $subnet
$Nic | New-AzNetworkInterfaceIpConfig -Name "ipconfig2" -PrivateIpAddress "10.1.0.14" -Subnet $subnet
Set-AzNetworkInterface -NetworkInterface $Nic
error is:
New-AzNetworkInterfaceIpConfig : The input object cannot be bound to any parameters for the command either because the command does not take pipeline input or the input and its properties do not
match any of the parameters that take pipeline input.
you can do like this
set-AzContext -subscription "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
$vnet = Get-AzVirtualNetwork -Name "rosvnet" -ResourceGroupName "sample"
$subnet = Get-AzVirtualNetworkSubnetConfig -Name "default" -VirtualNetwork $vnet
$Nic1 = New-AzNetworkInterfaceIpConfig -Name "ipconfig1" -PrivateIpAddress "10.1.0.11" -Primary -Subnet $subnet
$Nic2 = New-AzNetworkInterfaceIpConfig -Name "ipconfig2" -PrivateIpAddress "10.1.0.13" -Subnet $subnet
$Nic3 = New-AzNetworkInterfaceIpConfig -Name "ipconfig2" -PrivateIpAddress "10.1.0.14" -Subnet $subnet
$NIC = New-AzNetworkInterface `
-Name MyNIC `
-ResourceGroupName `
-Location `
-NetworkSecurityGroupId `
-IpConfiguration $Nic1,$Nic2,$Nic3

Trying to add a second IP adress to a network adapter but getting Resource {0} not found on Set-AzureRmNetworkInterface

I am trying to add a second IP address on a network adapter on a Azure Virtual machine scale set using the AzureRm cmdlet using Powershell.
My code looks like this:
Add-AzureRmAccount
Select-AzureRmSubscription -SubscriptionId "XXXXXXXXXXXXXXXXXXXXXX"
$vnetname = "confvnet"
$loc = "West Europe"
$backendSubnetName = "conf-jira-interlink"
$backendSubnetConfig = New-AzureRmVirtualNetworkSubnetConfig -Name $backendSubnetName -AddressPrefix "10.0.4.0/24"
echo "backendSubnetConfig: "$backendSubnetConfig
$vnet = Get-AzureRmVirtualNetwork -Name $vnetname -ResourceGroupName "resourcegroup-confluence-jira-datacenter"
echo "vnet: "$vnet
Add-AzureRmVirtualNetworkSubnetConfig -Name $backendSubnetName -VirtualNetwork $vnet -AddressPrefix "10.0.4.0/24" | Set-AzureRmVirtualNetwork
$subnetId = (Get-AzureRmVirtualNetworkSubnetConfig -Name $backendSubnetName -VirtualNetwork $vnet).Id
echo "subnetId: "$subnetId
$ipCfg = New-AzureRmVmssIPConfig -Name 'eth1' -SubnetId $subnetId -PrivateIPAddressVersion 'IPv4' -Primary $false
echo "ipCfg: "$ipCfg
$backendSubnet = Get-AzureRmVirtualNetworkSubnetConfig -Name $backendSubnetName -VirtualNetwork $vnet
echo "backendSubnet: "$backendSubnet
$vmss = Get-AzureRmVmss -ResourceGroupName resourcegroup-confluence-jira-datacenter -VMScaleSetName confcluster
echo "vmss: "$vmss
Add-AzureRmVmssNetworkInterfaceConfiguration -Name $backendSubnet -Primary $false -IPConfiguration $ipCfg -VirtualMachineScaleSet $vmss
$vmss = Get-AzureRmVmss -ResourceGroupName resourcegroup-confluence-jira-datacenter -VMScaleSetName confcluster
$Nic = Get-AzureRmNetworkInterface -ResourceGroupName "resourcegroup-confluence-jira-datacenter" -VirtualMachineScaleSetName 'confcluster'
echo "Nic: "$Nic
$ipconfig_interlink = New-AzureRmNetworkInterfaceIpConfig -Name "ipconfig-conf-jira-interlink" -Subnet $backendSubnetConfig -PrivateIpAddress "10.0.4.20"
Add-AzureRmNetworkInterfaceIpConfig -Name $ipconfig_interlink.Name -Subnet $backendSubnetConfig -NetworkInterface $Nic
Set-AzureRmNetworkInterfaceIpConfig -Name $ipconfig_interlink.Name -PrivateIpAddress 10.0.4.20 -Subnet $backendSubnetConfig -NetworkInterface $Nic
$Nic = Get-AzureRmNetworkInterface -ResourceGroupName "resourcegroup-confluence-jira-datacenter" -VirtualMachineScaleSetName 'confcluster'
echo "Nic: "$Nic
Set-AzureRmNetworkInterface -NetworkInterface $Nic
Update-AzureRmVmss -ResourceGroupName "resourcegroup-confluence-jira-datacenter" -VMScaleSetName "confcluster" -VirtualMachineScaleSet $vmss
But I'm getting the error
Set-AzureRmNetworkInterface : Resource '{0}' not found In
X:\JIRA_Confluence_Migration\PowerShell\network-interfaces-azure-same-vnet.ps1:67
Zeichen:1
+ Set-AzureRmNetworkInterface -NetworkInterface $Nic
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : CloseError: (:) [Set-AzureRmNetworkInterface], ArgumentException
+ FullyQualifiedErrorId : Microsoft.Azure.Commands.Network.SetAzureNetworkInterfaceCommand
on Set-AzureRmNetworkInterface.
Does anyone know what the problem is?
For your issue, you just need to change the command like below:
$Nic | Set-AzureRmNetworkInterface
Then it will work without the error.
But as I see in your script, you just get the network interface then set it without any change. If so, the command does not affect anything. You can take a look at the example of the command Set-AzureRmNetworkInterface.
Update
According to another test, there is a point should be focused on. Azure VM Scale Set instances are not the same as the VM. You can not find the network interfaces of the instances in the group which the VMSS in. All the instances are created from the same configuration, so do the network interfaces. As the error shows, the resource does not found.
You can take a look at the question that How do I specify a range of private IP addresses to use for static private IP address allocation. So I think that it's a wrong way that you want to set the NIC of the instance through the command Set-AzureRmNetworkInterface.
Just as your previous question, you can set the network configuration of the VMSS, it's the right way for VMSS.
I suspect it's looking for the identifier of the NIC itself, and not an object. Try:
Set-AzureRmNetworkInterface -NetworkInterface $Nic.Id

Associate NSG to Subnets in foreach loop Powershell

I've created a small script which creates subnets, I can then pass them through a loop and create network security groups (nsg) with the name of the subnet + -nsg, but when I try to associate the nsg to the subnet it fails saying the nsg doesn't exist. So i created two small scripts and split the create nsg from the associate nsg, but again same error.
Error:
Set-AzureNetworkSecurityGroupToSubnet : ResourceNotFound : The Network Security Group nsg-test-nsg does not exist.
But this does definitely exist.
The code I have is:
$resource = "rg-subnets"
$vnetName = "vnet-bmg"
$loc = "West Europe"
$vnet = Get-AzureRmVirtualNetwork -ResourceGroupName $resource -Name $vnetName
$sub = (Get-AzureRmVirtualNetworkSubnetConfig -VirtualNetwork $vnet).name
foreach ($subnet in $sub){
New-AzureRmNetworkSecurityGroup -Name $subnet-nsg -ResourceGroupName $resource -Location $loc -Force
Set-AzureNetworkSecurityGroupToSubnet -Name $subnet-nsg -VirtualNetworkName $vnetName -SubnetName $subnet
}
Help! :)
This worked for me,
$resource = "rg-subnets"
$vnetName = "vnet-bmg"
$loc = "West Europe"
$vnet = Get-AzureRmVirtualNetwork -ResourceGroupName $resource -Name $vnetName
$sub = (Get-AzureRmVirtualNetworkSubnetConfig -VirtualNetwork $vnet).name
foreach ($subnet in $sub){
New-AzureRmNetworkSecurityGroup -Name $subnet-nsg -ResourceGroupName $resource -Location $loc -Force
$subnetName = Get-AzureRmVirtualNetworkSubnetConfig -VirtualNetwork $vnet -Name $subnet
$nsg = Get-AzureRmNetworkSecurityGroup -ResourceGroupName $resource -Name "$subnet-nsg"
$subnetName.NetworkSecurityGroup = $nsg
Set-AzureRmVirtualNetwork -VirtualNetwork $vnet -Verbose
}

Resources