《Windows Azure Platform 系列文章目录

 

  刚刚在帮助一个合作伙伴研究需求,他们的虚拟机全面的网络安全组(Network Security Group, NSG)会经常变动,如果手动修改非常麻烦。

  看看是否有批量设置的方法。我仔细研究了一下,记一下笔记。

 

  注意:这里介绍的是Azure ARM模式下的,虚拟机的Azure NSG网络安全组。

 

  前提需求:

  1.安装Azure PowerShell

  2.Azure ARM模式下的资源

 

  具体运行的Azure PowerShell

Add-AzureRmAccount -EnvironmentName AzureChinaCloud
#在弹出的界面中,输入用户名和密码,则登陆通过
#不过关闭PowerShell以后,下次要重新进行身份验证

#选择相应的订阅名称:
Select-AzureRmSubscription -SubscriptionName 'Training'| Select-AzureRmSubscription

#请注意下面的NSG必须之前是已经存在的
$resourceGroupName='LeiLinuxRG'
$nsgName= 'LeiCentOS68-nsg'

#创建允许外部对3306端口访问,源IP必须是192.168.1.0/24
$ruleName1= 'Inbound3306-rule'
$sourceIP= '192.168.1.0/24'
$destinationPort = 3306

$nsg = Get-AzureRmNetworkSecurityGroup -Name $nsgName -ResourceGroupName $resourceGroupName

$nsg | Add-AzureRmNetworkSecurityRuleConfig -Name $ruleName1 -Description "Allow 3306" -Access `
    Allow -Protocol Tcp -Direction Inbound -Priority 100 -SourceAddressPrefix $sourceIP `
    -SourcePortRange * -DestinationAddressPrefix * -DestinationPortRange $destinationPort

#创建允许外部对3389端口访问,源IP必须是192.168.1.0/24
$ruleName2= 'Inbound3389-rule'
$destinationPort = 3389

$nsg | Add-AzureRmNetworkSecurityRuleConfig -Name $ruleName2 -Description "Allow RDP" -Access `
    Allow -Protocol Tcp -Direction Inbound -Priority 110 -SourceAddressPrefix $sourceIP `
    -SourcePortRange * -DestinationAddressPrefix * -DestinationPortRange $destinationPort

$nsg | Set-AzureRmNetworkSecurityGroup

 

相关文章:

  • 2021-10-03
  • 2021-09-16
  • 2021-06-15
  • 2022-02-15
  • 2021-10-27
  • 2021-11-02
  • 2021-11-27
  • 2021-12-08
猜你喜欢
  • 2021-12-24
  • 2021-05-25
  • 2021-12-26
  • 2022-02-13
  • 2021-10-01
  • 2021-11-19
  • 2022-02-26
相关资源
相似解决方案