I'm trying to remove Azure Load Balancer Backend Pool setting out of NIC Ipconfig in PowerShell but not succeeding without destroying whole Ipconfig. Either it removes entire config like
Remove-AzNetworkInterfaceIpConfig -NetworkInterface $NIC
Or when I was trying to set Ipconfig while naming other properties, LB pool config persists
Set-AzNetworkInterfaceIpConfig -NetworkInterface $NIC -Name "ipconfig1" -PrivateIpAddress $DIP -Subnet $backendSubnet -PublicIpAddress $PIP -ApplicationSecurityGroup $NSG
How can I only remove LB Backend Pool and keep the other properties? Thanks
If you want just remove or add specific NIC you can use this one. Just pass required variables and Action name "remove" or "add". It removes only NIC from Backend Load Balancer Pool, but didn't destroys whole Backend Pool configuration.
$ResourceGroup = "YourResourceGroupName"
$Loadbalancer = "YourLoadBalancerName"
$NICName = "YourVirtualMachineNetworkInterfaceName"
$Action = "remove" #add
if ($Action -eq 'add')
{
Write-Host ("Adding to LB")
$nic = Get-AzNetworkInterface | Where-Object { ($_.ResourceGroupName -eq "$ResourceGroup") -and ($_.Name -eq "$NICName") }
$lb = Get-AzLoadBalancer | Where-Object { ($_.ResourceGroupName -eq "$ResourceGroup") -and ($_.Name -eq "$Loadbalancer") }
$nic.IpConfigurations[0].LoadBalancerBackendAddressPools = $lb.BackendAddressPools[0]
Set-AzNetworkInterface -NetworkInterface $nic
}
if ($Action -eq 'remove')
{
Write-Host ("Removing from LB")
$nic = Get-AzNetworkInterface | Where-Object { ($_.ResourceGroupName -eq "$ResourceGroup") -and ($_.Name -eq "$NICName") }
$nic.Ipconfigurations[0].LoadBalancerBackendAddressPools = $null
Set-AzNetworkInterface -NetworkInterface $nic
}
What you need to do is not remove the Ipconfig of the NIC, but remove the nat rule config and backend pool config from the Load Balancer that associated with the Ipconfig of the NIC. And the example code here:
$resourceGrouName = "xxxxx"
$loabBalancerName = "xxxxx"
$inboundRuleConfigName = "xxxxx"
$backendpoolconfigName = "xxxxx"
$lb = Get-AzLoadBalancer -ResourceGroupName $resourceGrouName -Name $loabBalancerName
Remove-AzLoadBalancerInboundNatRuleConfig -LoadBalancer $lb -Name $inboundRuleConfigName
Remove-AzLoadBalancerBackendAddressPoolConfig -LoadBalancer $lb -Name $backendpoolconfigName
Set-AzLoadBalancer -LoadBalancer $lb
And you need to change the variables into yours. Then you can find the info about the Load Balancer that was removed from the Ipconfig of the NIC.
turns that you don't have to remove entire backend pool config but just modify NIC Ipconfig. I've tried
$DeactivateNIC = Get-AzNetworkInterface -ResourceGroupName $RG -Name $DeactivateNICName
$DeactivateNIC.IpConfigurations[0].LoadBalancerBackendAddressPools = $null
Set-AzNetworkInterface -NetworkInterface $DeactivateNIC
that works and LB pool stays. For adding I just add the Ipconfig property like:
$ActivateNIC = Get-AzNetworkInterface -ResourceGroupName $RG -Name $ActivateNICName
$lb = Get-AzLoadBalancer -ResourceGroupName $RG -Name "LB"
$lbPoolConfig = Get-AzLoadBalancerBackendAddressPoolConfig -LoadBalancer $lb
$ActivateNIC.IpConfigurations[0].LoadBalancerBackendAddressPools=$lbPoolConfig
Set-AzNetworkInterface -NetworkInterface $ActivateNIC
Related
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--
i have the following script:
ForEach ($lista in $listas) {
$RG = $lista.rg
$VM = $lista.vm
$NIC = $lista.nic
Stop-AzVM -ResourceGroupName $RG -Name $VM -Force
$nic = Get-AzNetworkInterface -ResourceGroupName $RG -Name $NIC
$nic.EnableAcceleratedNetworking = $false
$nic | Set-AzNetworkInterface
Start-AzVM -ResourceGroupName $RG -Name $VM
}
which i can disable on azure vm accellerated network. It works fine but i would like to know if is possible to parallelize it becouse i have to do it on 20-30 vm.
Is possible to do that?
Thanks
Try this, i havnt tested it but it should hopefully work.
$ScriptBlock = {
param($RG,$VM,$NIC)
Stop-AzVM -ResourceGroupName $RG -Name $VM -Force
$nic = Get-AzNetworkInterface -ResourceGroupName $RG -Name $NIC
$nic.EnableAcceleratedNetworking = $false
$nic | Set-AzNetworkInterface
Start-AzVM -ResourceGroupName $RG -Name $VM
}
foreach($lista in $listas) {
# Execute the jobs in parallel
Start-Job $ScriptBlock -ArgumentList $lista.rg, $lista.vm, $lista.nic
}
# Wait for all to complete
While (Get-Job -State "Running") { Start-Sleep 5 }
# Display output from all jobs
$res += (Get-Job | Receive-Job)
# Cleanup
Remove-Job *
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
With the new Az module for Azure, does anyone have the syntax for getting the public IP address of an Azure VM using the name?
The commandlet Get-AzPublicIpAddress has no argument for the VM name, only the IP object name
This works, but I'm not using the machine name here, it's the name of the IP object itself:
$CurrentIp = (Get-AzPublicIpAddress -ResourceGroupName 'RG1' -Name 'MyVMname-ip').IpAddress
I can't figure out how to just get it from the VM object i.e. this doesn't work:
Get-AzVM -ResourceGroupName 'RG1' -Name 'MyVMname' | Get-AzPublicIpAddress
As I know, it's impossible to get the VM public IP through just one PowerShell Get-AzPublicIpAddress with the VM name. And the public IP in Azure is an individual resource associated with the network interface, not the VM.
As you see, there no parameter for VM name to get the public IP in that document. But you can get the public IP through a PowerShell script just with the VM name and resource group name. The script shows below:
$vm = Get-AzureRmVM -ResourceGroupName yourRG -Name vmNamme
$nic = $vm.NetworkProfile.NetworkInterfaces[0].Id.Split('/') | select -Last 1
$publicIpName = (Get-AzureRmNetworkInterface -ResourceGroupName yourRG -Name $nic).IpConfigurations.PublicIpAddress.Id.Split('/') | select -Last 1
$publicIpAddress = (Get-AzureRmPublicIpAddress -ResourceGroupName yourRG -Name $publicIpName).IpAddress
Write-Output $vmName $publicIpAddress
Or just one CLI command to get the public IP like this:
az vm show -d -g yourRG -n vmName --query publicIps
I think this is a more thorough answer, as this uses PowerShell Az as the original question intended to use. In addition, it leverages Generic.List[psobject] which is useful for later playing with the data.
$rg = 'RgName'
$Ips = Get-AzNetworkInterface -ResourceGroupName $rg
$vmDetails = New-Object "System.Collections.Generic.List[psobject]"
foreach ($instance in $Ips){
$Vm = ($instance.VirtualMachine).Id.Split('/') | select -Last 1
$PrivateIp = $instance.IpConfigurations.PrivateIpAddress
$PublicIp = (Get-AzPublicIpAddress -ResourceGroupName $rg -Name ($instance.IpConfigurations.publicIpAddress.Id.Split('/') | select -Last 1)).IpAddress
$obj = New-Object psobject -Property #{
ResourceGroupName = $rg
VmName = $vm
PrivateIp = $PrivateIp
PublicIp = $PublicIp
}
$vmDetails.Add($obj)
}
Write-Output $vmDetails
This isn't as straight forward as Az CLI unfortunately but a good script to have regardless for Az modules.
Here's my take on Andrew Harris' answer, it filters out network interfaces not attached to machines and accounts for VMs that don't have a public IP:
function Get-VmIP {
<#
.SYNOPSIS
Returns the IP addresses for all VMs in the current subscription.
#>
[cmdletbinding()]
param()
$Interfaces = Get-AzNetworkInterface
foreach ($Interface in $Interfaces) {
if ($Interface.VirtualMachine) {
$VMName = $Interface.VirtualMachine.Id.split('/')[-1]
$PrivateIP = $Interface.IpConfigurations.PrivateIpAddress
$PublicIP = if ($Interface.IpConfigurations.publicIpAddress) {
Get-AzPublicIpAddress -Name ($instance.IpConfigurations.publicIpAddress.Id.Split('/')[-1]).IpAddress
}
[PSCustomObject]#{
VMName = $VMName
RGName = $Interface.ResourceGroupName
PrivateIP = $PrivateIP
PublicIP = $PublicIP
}
}
}
}
This is a corrected version of the Mark Wragg's script earlier in this thread:
function Get-VmIP {
<#
.SYNOPSIS
Returns the IP addresses for all VMs in the current subscription.
#>
[cmdletbinding()]
param()
$Interfaces = Get-AzNetworkInterface
foreach ($Interface in $Interfaces) {
if ($Interface.VirtualMachine) {
$VMName = $Interface.VirtualMachine.Id.split('/')[-1]
$PrivateIP = $Interface.IpConfigurations.PrivateIpAddress
$PublicIpAddressConfig = $Interface.IpConfigurations.publicIpAddress
$PublicIP = $null
$pconfigname = $null
if ($PublicIpAddressConfig) {
$pconfigname = $PublicIpAddressConfig.Id.Split('/')[-1]
$PublicIP = (Get-AzPublicIpAddress -Name $pconfigname).IpAddress
}
[PSCustomObject]#{
VMName = $VMName
RGName = $Interface.ResourceGroupName
PrivateIP = $PrivateIP
PublicIP = $PublicIP
}
}
}
}
The accepted answer uses AzureRM PowerShell module which is now obsoleted by Az module:
$VM = Get-AzVM -ResourceGroupName $ResourceGroupName -VMName $VMName
$NetworkInterfaceName = $VM.NetworkProfile.NetworkInterfaces[0].Id.Split('/')[-1]
$NetworkInterface = Get-AzNetworkInterface -ResourceGroupName $VM.ResourceGroupName -Name $NetworkInterfaceName
$PublicIpAddressName = $NetworkInterface.IpConfigurations.PublicIpAddress.Id.Split('/')[-1]
$PublicIpAddress = Get-AzPublicIpAddress -ResourceGroupName $VM.ResourceGroupName -Name $PublicIpAddressName
Write-Host "IP: $($PublicIpAddress.IpAddress), FQDN: $($PublicIpAddress.DnsSettings.Fqdn)"
Scope of the script is within an Azure subscription.
Below is the one-liner script which returns Name, PublicIpAllocaitonMethod(It's basically the type of the IP address whether it's a Static or Public IP) and the IpAddress properties of all the Network interfaces in a subscription.
(Get-AzNetworkInterface ).IpConfigurations.PublicIpAddress.Id | Foreach-Object -Process {$_.Split('/')| select -Last 1} | Foreach-Object -Process {Get-AzPublicIpAddress -Name $_} | Format-List Name, PublicIpAllocationMethod,IpAddress
If we remove the last statement Format-List it will display all the properties of the network interfaces that are having public IP addresses.
I have coded a powershell script to set an existing subnet to function as a service endpoint for multiple services. However, when I run the command line in the script, it doesn't add a new service endpoint, it just changes the existing one.
I am trying to parameterise this through Jenkins as well, which may be an added complication. I think if I can get the base syntax right then that shouldn't be a problem.
Syntax I am using is:
#Get vnet
$virtualnetwork = Get-AzureRmVirtualNetwork -Name $VN -ResourceGroupName $RG
#Configure service endpoint
Add-AzureRmVirtualNetworkSubnetConfig -Name $SN -AddressPrefix $SAP -
VirtualNetwork $virtualnetwork -ServiceEndpoint $EP
#Set configuration
$virtualnetwork | Set-AzureRmVirtualNetwork
You can use something like this to add as many endpoints as required:
$rgname = "amgar-dtl"
$vnName = "Dtlamgar-dtl"
$sname = "Dtlamgar-dtlSubnet"
$subnetPrefix = "10.0.0.0/20"
#Get vnet
$VirtualNetwork = Get-AzureRmVirtualNetwork -ResourceGroupName $rgname -Name $vnName | Get-AzureRmVirtualNetworkSubnetConfig -Name $sname
#Get existing service endpoints
$ServiceEndPoint = New-Object 'System.Collections.Generic.List[String]'
$VirtualNetwork.ServiceEndpoints | ForEach-Object { $ServiceEndPoint.Add($_.service) }
#Add new service endpoint
Get-AzureRmVirtualNetwork -ResourceGroupName $rgname -Name $vnName | Set-AzureRmVirtualNetworkSubnetConfig -Name $sname -AddressPrefix $subnetPrefix -ServiceEndpoint $ServiceEndPoint.Add("Microsoft.KeyVault") | Set-AzureRmVirtualNetwork
Hope this helps!
Successful syntax is:
#Vnet
$VN = "$ENV:VNET_NAME"
#Resource Group
$RG = "$ENV:RESOURCEGROUP_NAME"
#Subnet
$SN = "$ENV:SUBNET_NAME"
#Subnet Address Prexifx
$SAP = "$ENV:ADDRESS_PREFIX"
#ServiceEndpoint
$EP = "$ENV:SERVICE_ENDPOINT"
Write-Host "Importing the AzureRM module into the PowerShell session"
Import-Module AzureRM
Write-Host "Connect service principle account to Azure RM"
Connect-AzureRmAccount -ServicePrincipal -Credential $CREDS -TenantId $TID -Subscription $SID
#Get vnet
$VirtualNetwork = Get-AzureRmVirtualNetwork -ResourceGroupName $RG -Name $VN | Get-AzureRmVirtualNetworkSubnetConfig -Name $SN
#Get existing service endpoints
$ServiceEndPoint = New-Object 'System.Collections.Generic.List[String]'
$VirtualNetwork.ServiceEndpoints | ForEach-Object { $ServiceEndPoint.Add($_.service) }
$ServiceEndPoint.Add($EP)
#Add new service endpoint
Get-AzureRmVirtualNetwork -ResourceGroupName $RG -Name $VN | Set-AzureRmVirtualNetworkSubnetConfig -Name $SN -AddressPrefix $SAP -ServiceEndpoint $ServiceEndPoint | Set-AzureRmVirtualNetwork
Powershell does not appear to support the command $ServiceEndPoint.Add("Microsoft.KeyVault") with “|”. Once it was executed separately, the script worked.
Here is another version for those looking to process multiple subnets and to validate that the subnet doesn't already have the service endpoint enabled because it will error out if the same service is listed twice when modifying the subnet.
$subscription = "Enter Subscription ID here"
$subnets = #('my-subnet-1','my-subnet-2','my-subnet-3')
$vnetName = "MY-VNET"
$vnetRgName = "MY-VNET-RG"
$newEndpoint = "Microsoft.AzureCosmosDB"
Set-AzContext -Subscription $subscription
foreach($snet in $subnets){
Write-Host "Modifying Service Endpoints for subnet: $snet" -fore red -back white
$virtualNetwork = Get-AzVirtualNetwork -Name $vnetName -ResourceGroupName $vnetRgName | Get-AzVirtualNetworkSubnetConfig -Name $snet
$addrPrefix = $virtualNetwork.AddressPrefix
#Get existing service endpoints
$ServiceEndPoint = New-Object 'System.Collections.Generic.List[String]'
$virtualNetwork.ServiceEndpoints | ForEach-Object { $ServiceEndPoint.Add($_.service) }
if ($ServiceEndPoint -notcontains $newEndPoint){
$ServiceEndPoint.Add($newEndpoint)
}
#Add new service endpoint
Get-AzVirtualNetwork -Name $vnetName -ResourceGroupName $vnetRgName | Set-AzVirtualNetworkSubnetConfig -Name $snet -AddressPrefix $addrPrefix -ServiceEndpoint $ServiceEndPoint | Set-AzVirtualNetwork
}