Azure PowerShell (13) 批量设置Azure ARM Network Security Group (NSG)

简介:

 《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
复制代码

 


本文转自Azure Lei Zhang博客园博客,原文链接:http://www.cnblogs.com/threestone/p/6869513.html,如需转载请自行联系原作者


目录
相关文章
|
8天前
|
存储 网络协议 网络安全
【Azure 环境】部署ARM Linked Template时候 Blob SAS Token不能正常工作
Unable to retrieve url https://<stroage account name>.blob.core.chinacloudapi.cn/arm/azuredeploy.json?sp=r 'st' is not recognized as an internal or external command, operable program or batch file. 'se' is not recognized as an internal or external command, operable program or batch file. 'spr' is no
|
3月前
【Azure App Service】PowerShell脚本批量添加IP地址到Web App允许访问IP列表中
Web App取消公网访问后,只允许特定IP能访问Web App。需要写一下段PowerShell脚本,批量添加IP到Web App的允许访问IP列表里!
|
4月前
|
数据安全/隐私保护
【Azure Entra ID】使用PowerShell脚本导出Entra ID中指定应用下的所有用户信息
在Azure Entra ID中,需要导出一个Application 下的用户信息, 包含User的创建时间。
|
6月前
|
存储 Windows
在 PowerShell 中获取代理设置
【8月更文挑战第27天】
239 6
|
6月前
【Azure Durable Function】PowerShell Activity 函数遇见 Newtonsoft.Json.JsonReaderException: The reader's MaxDepth of 64 has been exceeded.
【Azure Durable Function】PowerShell Activity 函数遇见 Newtonsoft.Json.JsonReaderException: The reader's MaxDepth of 64 has been exceeded.
|
6月前
【Azure Web Job】Azure Web Job执行Powershell脚本报错 The term 'Select-AzContext' is not recognized as the name
【Azure Web Job】Azure Web Job执行Powershell脚本报错 The term 'Select-AzContext' is not recognized as the name
|
6月前
|
存储 C# Python
【Azure Storage Account】Azure 存储服务计算Blob的数量和大小的PowerShell代码
【Azure Storage Account】Azure 存储服务计算Blob的数量和大小的PowerShell代码
|
6月前
|
Ubuntu Linux 测试技术
【Azure Function App】Python Function调用Powershell脚本在Azure上执行失败的案例
【Azure Function App】Python Function调用Powershell脚本在Azure上执行失败的案例
|
5月前
|
Windows
Powershell 重新排列去重 Windows环境变量
【9月更文挑战第13天】本文介绍如何使用PowerShell对Windows环境变量进行重新排列和去重。首先通过`$env:`访问环境变量,接着使用`-split`命令分割路径,再利用`Select-Object -Unique`去除重复项。之后可根据需要对路径进行排序,最后将处理后的路径组合并更新环境变量。注意修改环境变量前应备份重要数据并了解潜在影响。
194 10
|
4月前
|
监控 关系型数据库 MySQL
PowerShell 脚本编写 :自动化Windows 开发工作流程
PowerShell 脚本编写 :自动化Windows 开发工作流程
197 0