Looking for a better way to do the following from the code block below. I was able to brute force it by running the code in a foreach loop
$DestAddr = $rule.destination
$DestAddrSplit = $DestAddr.split("-")
$SorAddr = $rule.source
$SorAddrSplit = $SorAddr.split("-")
$SorPort = $rule.sorport
$SorPortSplit = $SorPort.split("-")
$DesPort = $rule.desport
$DesPortSplit = $DesPort.split("-")
The Script below works however when I import-Csv I could not get it to do the split as need for the Azure Cmdlet.
script
$rg = "Test"
$loc = "EASTUS2"
$nsgname = Read-Host -Prompt "Please input NSG Name"
New-AzNetworkSecurityGroup -Name $nsgname -ResourceGroupName $rg -Location $loc
$NSG = Get-AzureRmNetworkSecurityGroup -Name $nsgname -ResourceGroupName $rg
$Rules = Import-Csv "C:\temp\PowerhShell\creatensginput.csv"
foreach($Rule in $Rules)
{
$DestAddr = $rule.destination
$DestAddrSplit = $DestAddr.split("-")
$SorAddr = $rule.source
$SorAddrSplit = $SorAddr.split("-")
$SorPort = $rule.sorport
$SorPortSplit = $SorPort.split("-")
$DesPort = $rule.desport
$DesPortSplit = $DesPort.split("-")
$NSG | Add-AzNetworkSecurityRuleConfig -Name $rule.name `</div>
<div> -Access $rule.access `
-Direction $rule.direction `</div>
<div> -Priority $rule.priority `
-SourceAddressPrefix $SorAddrSplit `</div>
<div> -SourcePortRange $SorPortSplit `
-DestinationAddressPrefix $DestAddrSplit `</div>
<div> -DestinationPortRange $DesPortSplit `
-Protocol $rule.protocol `
-Description $rule.description
}
$NSG | Set-AzureRmNetworkSecurityGroup
CSV file
name,direction,priority,access,source,destination,sorport,desport,protocol,description
AllowPing,Inbound,100,allow,VirtualNetwork,111.111.111.0/24,2,*,"Icmp","Allow ICMP Ping"
AllowRDPToJumpHosts,Inbound,110,allow,VirtualNetwork,111.111-111.224.183.5,3389-443,3389-443,Tcp,"Allow the RDP Protocol."