Thursday, October 7, 2021

Azure Powershell change dynamic ip to static for a VM.

 

Select-AzContext -Subscription "YourSubscriptionname"

 

get-azsubscription to get a list of all subscriptions you have access to.

 

Set-AzContext  -subscriptionname "yoursubscriptionname"

 

 Change dynamic to static for a single VM

$If = Get-AzNetworkInterface | ?{$_.virtualmachine.id -match "yourvm"}
$Nic = Get-AzNetworkInterface -ResourceGroupName $If.resourcegroupname -Name $if.name
$Nic.IpConfigurations[0].PrivateIpAllocationMethod
$Nic.IpConfigurations[0].PrivateIpAllocationMethod = "Static"
Set-AzNetworkInterface -NetworkInterface $Nic

 

View all Vm's with dynamic ip's

$DynamicIfs = Get-AzNetworkinterface | ?{$_.IpConfigurations[0].PrivateIpAllocationMethod -eq "Dynamic"}
foreach($if in $DynamicIfs)
{
$Nic = Get-AzNetworkInterface -ResourceGroupName $If.resourcegroupname -Name $if.name
Write-host $If.Virtualmachine.Id $Nic.IpConfigurations[0].PrivateIpAllocationMethod
}

 

Change dynamic to static for all VM's. 

$DynamicIfs = Get-AzNetworkinterface | ?{$_.IpConfigurations[0].PrivateIpAllocationMethod -eq "Dynamic"}
foreach($if in $DynamicIfs)
{
$Nic = Get-AzNetworkInterface -ResourceGroupName $If.resourcegroupname -Name $if.name
$Nic.IpConfigurations[0].PrivateIpAllocationMethod
$Nic.IpConfigurations[0].PrivateIpAllocationMethod = "Static"
Set-AzNetworkInterface -NetworkInterface $Nic
}



No comments:

Post a Comment