-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGet-LocalIp.ps1
More file actions
45 lines (40 loc) · 1.68 KB
/
Get-LocalIp.ps1
File metadata and controls
45 lines (40 loc) · 1.68 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
function Get-LocalIp {
[CmdletBinding()]
param()
begin {
Write-Verbose -Message ((Get-ResStr 'STARTING_FUNCTION') -f $myInvocation.Mycommand)
New-Variable -Name 'gatewayIp' -Scope 'Private' -Value ([string]'')
New-Variable -Name 'gatewayIpObj' -Scope 'Private' -Value ($null)
New-Variable -Name 'ip' -Scope 'Private' -Value ([string]'')
New-Variable -Name 'ipObj' -Scope 'Private' -Value ($null)
New-Variable -Name 'localIps' -Scope 'Private' -Value ([string[]]@())
New-Variable -Name 'subnetMask' -Scope 'Private' -Value ([string]'')
New-Variable -Name 'subnetMaskObj' -Scope 'Private' -Value ($null)
New-Variable -Name 'result' -Scope 'Private' -Value ([string]'')
$initialVariables = Get-CurrentVariables -Debug:$DebugPreference
}
process {
try {
$gatewayIp= Get-GatewayIp
$gatewayIpObj= [IPAddress]::Parse($gatewayIp)
[string[]]$localIps= (Get-NetIPAddress -AddressFamily IPV4 -PrefixOrigin DHCP,MANUAL).IpAddress
foreach ($ip in $localIps) {
$ipObj = [IPAddress]::Parse($ip)
$subnetMask = Get-Subnet -localIp $ip
$subnetMaskObj = [IPAddress]::Parse($subnetMask)
if (($ipObj.Address -band $subnetMaskObj.Address) -eq ($gatewayIpObj.Address -band $subnetMaskObj.Address)) {
$result = $ip
break
}
}
}
catch {
$result= '0.0.0.0'
}
}
end {
Get-CurrentVariables -InitialVariables $initialVariables -Debug:$DebugPreference
Return $result
}
# Test: Get-LocalIp
}