UseCases/UpdateUniFiVpnPeerIP.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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
#requires -Version 3.0 -Modules UniFiTooling

<#
      .SYNOPSIS
      Update a VPN PeerIp for a given UniFi Network
  
      .DESCRIPTION
      Update a VPN PeerIp for a given UniFi Network via the API of the UniFi Controller
  
      .PARAMETER VPN2Update
      Name of the UniFi VPN Network
  
      .PARAMETER NewPeerIp
      New IP address of the VPN peer (Remote)
      IPv4 address onyl, the Controller rejects any other format.
  
      .EXAMPLE
      PS C:\> .\UpdateUniFiVpnPeerIP.ps1 -VPN2Update 'JoshAtHomeVpn' -NewPeerIp 10.10.10.10
 
      Update the Peer IP of the Network 'JoshAtHomeVpn' to 10.10.10.10
  
      .NOTES
      Just a use case demo
       
      Requires the UniFiTooling module version 1.0.3, or later
#>

[CmdletBinding(ConfirmImpact = 'None')]
[OutputType([bool])]
param
(
   [Parameter(Mandatory,
         ValueFromPipeline,
         ValueFromPipelineByPropertyName,
         Position = 1,
   HelpMessage = 'Name of the UniFi VPN Network')]
   [ValidateNotNullOrEmpty()]
   [string]
   $VPN2Update,
   [Parameter(Mandatory,
         HelpMessage = 'New IP address of the VPN peer (Remote)',
         ValueFromPipeline,
         ValueFromPipelineByPropertyName,
   Position = 2)]
   [ValidateNotNullOrEmpty()]
   [ipaddress]
   $NewPeerIp
)

begin
{
   # Login
   $null = (Invoke-UniFiApiLogin)
   
   # Put the input to a new variable
   # I use [ipaddress] above and let PowerShell do the input checks, because the Controlelr is a bit picky with the format
   $NewPeerIp2 = $NewPeerIp.IPAddressToString
   
   # Safe ProgressPreference and Setup SilentlyContinue for the function
   $ExistingProgressPreference = ($ProgressPreference)
   $ProgressPreference = 'SilentlyContinue'
}

process
{
   # Get all UniFi Networks and filter based on the input above
   $UnifiNetwork = (((Get-UnifiNetworkList) | Where-Object -FilterScript {
            ($_.Name -eq $VPN2Update)
   })._id)
    
   # Get the details of the network we found
   $UnifiNetworkDetails = (Get-UnifiNetworkDetails -UnifiNetwork $UnifiNetwork)

   # Replace the Peer IP in the object
   $UnifiNetworkDetails.ipsec_peer_ip = $NewPeerIp2

   # Create a new Request Body
   $paramConvertToJson = @{
      InputObject   = $UnifiNetworkDetails
      Depth         = 5
      ErrorAction   = 'Stop'
      WarningAction = 'SilentlyContinue'
   }
   $UnifiNetworkDetailsJson = (ConvertTo-Json @paramConvertToJson)

   # Update the VPN network
   $null = (Set-UnifiNetworkDetails -UnifiNetwork $UnifiNetwork -UniFiBody $UnifiNetworkDetailsJson)
    
   # Get the details of the network again
   $NewPeerIpUniFi = ((Get-UnifiNetworkDetails -UnifiNetwork $UnifiNetwork).ipsec_peer_ip)
    
   # Compare the input with the latest info on the UniFi Controller
   if ($NewPeerIpUniFi -ne $NewPeerIp2)
   {
      Write-Warning -Message ('Peer address is {0} but it should be {1}' -f $NewPeerIpUniFi, $NewPeerIp2)

      $false
   }
   else
   {
      $true
   }
}

end
{
   # Logoff
   $null = (Invoke-UniFiApiLogout)
   
   # Restore ProgressPreference
   $ProgressPreference = $ExistingProgressPreference
}