동일한 테넌트의 구독 전체에서 NSG 규칙을 복제하는 Azure Powershell 스크립트

동일한 테넌트의 구독 전체에서 NSG 규칙을 복제하는 Azure Powershell 스크립트

1 구독의 특정 NSG에서 다른 구독의 NSG로의 powershell 스크립트 복제 NSG 규칙에 대한 도움이 필요합니다. 두 NSG가 모두 동일한 구독에 있지만 다른 구독에 있는 경우에는 이를 수행하지 않는 스크립트가 있습니다. 여기에 내가 가진 것이 있습니다. 어떤 도움이라도 주시면 감사하겠습니다.

#name of NSG that you want to copy 
$nsgOrigin = ""
#name new NSG  
$nsgDestination = "" 
#Resource Group Name of source NSG 
$rgName = "" 
#Resource Group Name when you want the new NSG placed 
$rgNameDest = ""
 
$nsg = Get-AZNetworkSecurityGroup -Name $nsgOrigin -ResourceGroupName $rgName 
$nsgRules = Get-AZNetworkSecurityRuleConfig -NetworkSecurityGroup $nsg 
$newNsg = Get-AZNetworkSecurityGroup -name $nsgDestination -ResourceGroupName $rgNameDest 
foreach ($nsgRule in $nsgRules) { 

$acl = @{Name = $nsgRule.Name; Protocol = $nsgRule.Protocol; SourcePortRange= $nsgRule.SourcePortRange; DestinationPortRange = $nsgRule.DestinationPortRange;Priority = $nsgRule.Priority;Direction = $nsgRule.Direction; Access = $nsgRule.Access; SourceApplicationSecurityGroup = $nsgRule.SourceApplicationSecurityGroups }


if ( $nsgRule.DestinationAddressPrefix.count -gt 0 ) { 
$acl  += @{DestinationAddressPrefix = $nsgRule.DestinationAddressPrefix }
} 
if ( $nsgRule.SourceAddressPrefix.count -gt 0 ) {
$acl  += @{SourceAddressPrefix = $nsgRule.SourceAddressPrefix }
}
if ( $nsgRule.SourceApplicationSecurityGroups.count -gt 0 ) {
$acl  += @{SourceApplicationSecurityGroup = $nsgRule.SourceApplicationSecurityGroups }
}
if ( $nsgRule.DestinationApplicationSecurityGroups.count -gt 0 ) {
$acl  += @{DestinationApplicationSecurityGroup = $nsgRule.DestinationApplicationSecurityGroups }
}


Add-AZNetworkSecurityRuleConfig -NetworkSecurityGroup $newNsg @acl
       
} 
Set-AZNetworkSecurityGroup -NetworkSecurityGroup $newNsg

답변1

다음과 같이 원본 nsg 전후에 구독 컨텍스트를 설정하면 됩니다.

Set-AzContext -SubscriptionName 'SUBNAME'

$nsg = Get-AZNetworkSecurityGroup -Name $nsgOrigin -ResourceGroupName $rgName $nsgRules = Get-AZNetworkSecurityRuleConfig -NetworkSecurityGroup $nsg

Set-AzContext -SubscriptionName 'SUBNAME'

관련 정보