Skip to content

Instantly share code, notes, and snippets.

@wogho
Created May 2, 2022 09:54
Show Gist options
  • Save wogho/5511ed6a18b536704c84c2aa466a99aa to your computer and use it in GitHub Desktop.
Save wogho/5511ed6a18b536704c84c2aa466a99aa to your computer and use it in GitHub Desktop.
Azure CLI : control VM and Change Network NIC, Setting Public IP
# 머신 종료 / VM off
Stop-AzVM -ResourceGroupName "GeonjuV2.0" -Name "GeonjuV2" -Force
Start-Sleep -s 10
# NIC 랜덤 생성 / Create New NIC Ramdom
$vm = Get-AzVM -ResourceGroupName GeonjuV2.0 -Name GeonjuV2
$nicname = $vm.NetworkProfile.NetworkInterfaces[0].Id.Split('/') | select -Last 1
$name = ([int]$nicname + 1)
$nic = New-AzNetworkInterface -Name $name -ResourceGroupName "GeonjuV2.0" -Location "koreacentral" -SubnetId "/subscriptions/54827d60-3591-4014-a594-e6b75fb7deab/resourceGroups/GeonjuV2.0/providers/Microsoft.Network/virtualNetworks/GeonjuV2.0-vnet/subnets/default" -IpConfigurationName "IPConfiguration1" -DnsServer "8.8.8.8", "8.8.4.4”
Start-Sleep -s 10
# 생성한 NIC 연결 / Connect New NIC
$nicId = (Get-AzNetworkInterface -ResourceGroupName GeonjuV2.0).Id.Split('/') | Select -Last 1
az vm nic add -g GeonjuV2.0 --vm-name GeonjuV2 --nics $nicId
Start-Sleep -s 10
# 기존 NIC 분리 / Remove old NIC from VM
$nicout = (Get-AzNetworkInterface -ResourceGroupName GeonjuV2.0).Id.Split('/') | select -Index 8
az vm nic remove -g GeonjuV2.0 --vm-name GeonjuV2 --nics $nicout
Start-Sleep -s 10
# 기존 NIC 제거 / Delete old NIC
$renicId = (Get-AzNetworkInterface -ResourceGroupName GeonjuV2.0).Id.Split('/') | Select -Index 8
az network nic delete -g GeonjuV2.0 -n $renicId
Start-Sleep -s 10
# 공인 Ip 생성 / Create New Public IP
$publicname = (Get-AzPublicIpAddress -ResourceGroupName GeonjuV2.0).Id.Split('/') | select -Last 1
$pn = ($publicname + 1)
New-AzPublicIpAddress -Name $pn -ResourceGroupName GeonjuV2.0 -AllocationMethod Dynamic -Location koreacentral
Start-Sleep -s 10
# 생성한 공인Ip NIC 연결 / Connect New Public IP in New NIC
$vm = Get-AzVM -ResourceGroupName GeonjuV2.0 -Name GeonjuV2
$nicname = $vm.NetworkProfile.NetworkInterfaces[0].Id.Split('/') | select -Last 1
$newpip = (Get-AzPublicIpAddress -ResourceGroupName GeonjuV2.0).Id.Split('/') | Select -Last 1
az network nic ip-config update --name IPConfiguration1 --resource-group GeonjuV2.0 --nic-name $nicname --public-ip-address $newpip
Start-Sleep -s 10
# 기존 공인Ip 제거 / Delete old Public IP
$pip = (Get-AzPublicIpAddress -ResourceGroupName GeonjuV2.0).Id.Split('/') | Select -Index 8
Remove-AzPublicIpAddress -name $pip -ResourceGroupName GeonjuV2.0 -Force
Start-Sleep -s 10
# 머신 가동 / Start VM
Start-AzVM -ResourceGroupName "GeonjuV2.0" -Name "GeonjuV2"
Start-Sleep -s 60
# 원격 연결 / Connect VM RDP
$ip = az vm show -d -g GeonjuV2.0 -n GeonjuV2 --query publicIps -o tsv
cmdkey /generic:"$ip" /user:"jaeho" /pass:"gksrnrwldhr@12"
mstsc /v:$ip
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment