Azure Powershell-将多个服务终结点应用于子网

时间:2019-02-07 17:46:52

标签: azure powershell

我已经编码了一个powershell脚本来设置一个现有的子网,以充当多个服务的服务端点。但是,当我在脚本中运行命令行时,它不会添加新的服务端点,而只会更改现有的服务端点。

我也在尝试通过Jenkins对此进行参数化,这可能会增加复杂性。我认为,如果我能正确使用基本语法,那应该不是问题。

我正在使用的语法是:

#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    

2 个答案:

答案 0 :(得分:1)

您可以使用类似的方法添加所需数量的端点:

$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

希望这会有所帮助!

答案 1 :(得分:0)

成功的语法是:

#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似乎不支持带有“ |”的命令$ ServiceEndPoint.Add(“ Microsoft.KeyVault”)。脚本单独执行后,便可以正常工作。