diff --git a/GetNetworkManagerScope.json b/GetNetworkManagerScope.json new file mode 100644 index 0000000..ddff58e --- /dev/null +++ b/GetNetworkManagerScope.json @@ -0,0 +1,7 @@ +{ + "subsid": "", + "resourceGroupName": "", + "networkManagerName": "", + "outputPath": "./scope.json" +} + \ No newline at end of file diff --git a/GetNetworkManagerScope.ps1 b/GetNetworkManagerScope.ps1 new file mode 100644 index 0000000..e04218c --- /dev/null +++ b/GetNetworkManagerScope.ps1 @@ -0,0 +1,40 @@ + #// Copyright (c) Microsoft Corporation. + #// Licensed under the MIT license. + + # Setup + $configPath = '.\GetNetworkManagerScope.json' + + try + { + $Configs = Get-Content -Path $configPath | ConvertFrom-Json -AsHashtable -ErrorAction SilentlyContinue + $subId = $configs["subsid"] + Write-Host "Subscription Id: " $subId + $rgname = $configs["resourceGroupName"] + Write-Host "ResourceGroup Id: " $rgname + $networkManagerName = $configs["networkManagerName"] + Write-Host "NetworkManager Name: " $NetworkManagerName + $outputPath = $configs["outputPath"] + Write-Host "OutputPath: " $outputPath + } + catch + { + Write-Host "Config file is in incorrect json format, please format it correctly" -ForegroundColor Red + return + } + + if ($null -eq $configs) + { + Write-Host "Config file is in incorrect json format, please format it correctly" -ForegroundColor Red + return + } + + Connect-AzAccount -Subscription $subId + + $networkManager = Get-AzNetworkManager -ResourceGroupName $rgname -Name $networkManagerName + + $scope = $networkManager.NetworkManagerScopes | Select-Object * -ExcludeProperty "CrossTenantScopes", "ManagementGroupsText", "SubscriptionsText", "CrossTenantScopesText" + $scope | ConvertTo-Json | Out-File -FilePath $outputPath + + Write-Host "Get Scope Completed In " $outputPath -ForegroundColor Green + + \ No newline at end of file diff --git a/ScopeValidation.json b/ScopeValidation.json index 4f430c4..511d87c 100644 --- a/ScopeValidation.json +++ b/ScopeValidation.json @@ -2,11 +2,10 @@ "subsid": "", "resourceGroupName": "", "networkManagerName": "", - "subscriptions": [""], - "managementGroups": [""] + "subscriptions": [], + "managementGroups": [] + "scopePath": "./scope.json", + "isDryRun": "false" } - - - \ No newline at end of file diff --git a/ScopeValidation.ps1 b/ScopeValidation.ps1 index 99e79f2..626d929 100644 --- a/ScopeValidation.ps1 +++ b/ScopeValidation.ps1 @@ -3,18 +3,34 @@ # Setup $configPath = '.\ScopeValidation.json' - + try { $Configs = Get-Content -Path $configPath | ConvertFrom-Json -AsHashtable -ErrorAction SilentlyContinue $subId = $configs["subsid"] - Write-Host "Subscription Id: " $SubId + Write-Host "Subscription Id: " $subId $rgname = $configs["resourceGroupName"] Write-Host "ResourceGroup Id: " $rgname $networkManagerName = $configs["networkManagerName"] Write-Host "NetworkManager Name: " $NetworkManagerName - $subscriptions = $configs["subscriptions"] - $managementGroups = $configs["managementGroups"] + $scopePath = $configs["scopePath"] + + if($scopePath.Length -ne 0) + { + Write-Host "Get Scope From ScopePath: " $scopePath + $updatedScope = Get-Content -Path $scopePath | ConvertFrom-Json -AsHashtable -ErrorAction SilentlyContinue + $subscriptions = $updatedScope["Subscriptions"] + $managementGroups = $updatedScope["ManagementGroups"] + } + else{ + $subscriptions = $configs["subscriptions"] + $managementGroups = $configs["managementGroups"] + } + + + + $isDryRun = $configs["isDryRun"] + Write-Host "IsDryRun: " $isDryRun } catch { @@ -164,7 +180,24 @@ return } } + + + } + + if($isDryRun -eq "False" || $isDryRun -eq "false") + { + # Update + return + $scope = New-AzNetworkManagerScope -Subscription $subscriptions -ManagementGroup $managementGroups + $networkManager.NetworkManagerScopes = $scope; + $newNetworkManager = Set-AzNetworkManager -InputObject $networkManager + Write-Host "Removed Scopes Do Not Contain Deployed Resources, Network Manager Scope is Updated" -ForegroundColor Green } + else + { + Write-Host "Removed Scopes Do Not Contain Deployed Resources, Safe To Remove" -ForegroundColor Green + } + + - Write-Host "Removed Scopes Do Not Contain Deployed Resources, Safe To Remove" -ForegroundColor Green \ No newline at end of file