-
Notifications
You must be signed in to change notification settings - Fork 0
/
Get_IP_Subnet_Of_Ethernet.ps1
34 lines (27 loc) · 1.02 KB
/
Get_IP_Subnet_Of_Ethernet.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
# Script is return the subnet mask in xxx.xxx.xxx.xxx format.
# https://github.com/VSKUMBHANI/PowerShell
Function Get-NetworkIPv4 {
param(
[string]$ipAddress,
[int]$cidr
)
$parsedIpAddress = [System.Net.IPAddress]::Parse($ipAddress)
$shift = 64 - $cidr
[System.Net.IPAddress]$subnet = 0
if ($cidr -ne 0) {
$subnet = [System.Net.IPAddress]::HostToNetworkOrder([int64]::MaxValue -shl $shift)
}
[System.Net.IPAddress]$network = $parsedIpAddress.Address -band $subnet.Address
return [PSCustomObject]@{
Network = $network
SubnetMask = $subnet
}
}
# Get the IP address assigned to the network adapter 'ethernet'. Replace it with your adapter alias.
$ipAlias = Get-NetIPAddress | Where-Object {$_.InterfaceAlias -eq "ethernet"}
$NetipAddress = $ipAlias.IPAddress
$subnetPrefix = $ipAlias.PrefixLength
$ipDetails = Get-NetworkIPv4 -ipAddress $NetipAddress -cidr $subnetPrefix
# Get Subnet string.
$subnetmask = $ipDetails.SubnetMask.IPAddressToString
$subnetmask