-
Notifications
You must be signed in to change notification settings - Fork 0
/
Remove-SCVPNConnectionNetworkRoutes.ps1
94 lines (77 loc) · 2.46 KB
/
Remove-SCVPNConnectionNetworkRoutes.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
##
# remove network route(s) in network routes list from VM network gateway of VM network
#
#
##
function Import-ModuleIfNotAlreadyImported {
param ([String]$Name)
$isImported = Get-Module | Where-Object {$_.Name -eq $Name}
if (!$isImported) {
Import-Module $Name
}
}
function Remove-SCVPNConnectionNetworkRoutes {
param(
[String]$VMnetworkName,
[String]$VPNConnectionName,
[String[]]$RoutingSubnets,
[String]$Protocol="L3"
)
if (!$PSCmdlet.$Protocol) {
Write-Warning "`n`n`n$Protocol variable not defined, using L3 as default value..."
}
$VmNetworkObjectRef = Get-SCVMNetwork -Name $VmNetworkName
$vmNetworkGatewayObjectRef = Get-SCVMNetworkGateway -VMNetwork $VmNetworkObjectRef
$vpnConnection = Get-SCVPNConnection -Name $VPNConnectionName -VMNetworkGateway $vmNetworkGatewayObjectRef | Where-Object {$_.Protocol -eq $Protocol}
# Remove network routes from VM network gateway...
try
{
foreach($route in $RoutingSubnets)
{
$networkRouteObjectRef = Get-SCNetworkRoute -VPNConnection $vpnConnection | Where-Object {$_.IPSubnet -eq $route}
if ($networkRouteObjectRef) {
Write-Host "`nRemoving network route $route from the routing table...`n`n"
$routeRemoveResult = Remove-SCNetworkRoute -NetworkRoute $networkRouteObjectRef
if ($routeRemoveResult) {
Write-Host "`nNetwork route was removed...`n`n"
}
} else {
Write-Host "`n`nNo network route was found..."
}
}
}
catch
{
$PSItem.Exception.InnerExceptionMessage
}
Write-Host "`n`n`nFollowing network routes exists for $VPNConnectionName $Protocol connection of $VMnetworkName VM network..."
Get-SCNetworkRoute -VPNConnection $vpnConnection
}
##
# module name
$moduleName = "virtualmachinemanager"
#
#
# VM network name
$VmNetworkName = "<VM network name here>"
#
#
# VPN connection name
$VPNConnectionName = "<VPN connection name>"
#
#
# Connection protocol
$protocol = "L3"
#
#
# Network routes
$routingSubnets = @(
"192.168.10.0/29",
"192.168.20.0/29"
)
#
#
#
##
Import-ModuleIfNotAlreadyImported -Name $moduleName
Remove-SCVPNConnectionNetworkRoutes -VMnetworkName $VmNetworkName -VPNConnectionName $VPNConnectionName -RoutingSubnets $routingSubnets