-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGet-NextIp.ps1
More file actions
51 lines (47 loc) · 1.97 KB
/
Get-NextIp.ps1
File metadata and controls
51 lines (47 loc) · 1.97 KB
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
function Get-NextIp {
[CmdletBinding()]
param(
[Parameter(Mandatory = $false)]
[string]$ip = $(Throw ((Get-ResStr 'PARAM_MANDATORY_MISSED') -f 'ip', $myInvocation.Mycommand))
,
[Parameter(Mandatory = $false)]
[int]$inc = 1
)
begin {
Write-Verbose -Message ((Get-ResStr 'STARTING_FUNCTION') -f $myInvocation.Mycommand)
New-Variable -Name 'ipBytes' -Scope 'Private' -Value ($null)
New-Variable -Name 'ipInt' -Scope 'Private' -Value ([uint32]0)
New-Variable -Name 'ipObj' -Scope 'Private' -Value ($null)
New-Variable -Name 'newIp' -Scope 'Private' -Value ($null)
New-Variable -Name 'nextIpBytes' -Scope 'Private' -Value ($null)
New-Variable -Name 'nextIpInt' -Scope 'Private' -Value ([uint32]0)
New-Variable -Name 'result' -Scope 'Private' -Value ([string]'')
$initialVariables = Get-CurrentVariables -Debug:$DebugPreference
}
process {
try {
$ipObj = [IPAddress]$ip
$ipBytes = $ipObj.GetAddressBytes()
[System.Array]::Reverse($ipBytes)
$ipInt = [System.BitConverter]::ToUInt32($ipBytes, 0)
$nextIpInt = $ipInt + $inc
$nextIpBytes = [System.BitConverter]::GetBytes($nextIpInt)
[System.Array]::Reverse($nextIpBytes)
if ([IntPtr]::Size -eq 4) { # x86
$newIp = New-Object System.Net.IPAddress ([System.Array]::CreateInstance([byte], 4))
[System.Buffer]::BlockCopy($nextIpBytes, 0, $nextIp.GetAddressBytes(), 0, 4)
} else { # x64
$newIp = New-Object System.Net.IPAddress -ArgumentList (,$nextIpBytes)
}
$result = $newIp.IPAddressToString
}
catch {
$result = '0.0.0.0'
}
}
end {
Get-CurrentVariables -InitialVariables $initialVariables -Debug:$DebugPreference
Return $result
}
# Test: Get-NextIp -ip '192.168.178.20' -inc 5
}