SkillPilotAI

PowerShell IpamServer module (2/4)

Part 2 of the IpamServer module: the Find- cmdlets that locate free IPs, ranges and subnets, Export/Import to CSV, and the read-only Get- cmdlets for the hierarchy, server settings, DHCP/DNS data and audit events. These are the commands you use every day to plan IP space safely.

252.1 Why is it used?

  • Find a free IP or subnet from data instead of guessing, so no two devices get the same address.
  • Get- cmdlets show utilization, overlaps and duplicates before they cause outages.
  • Export/Import moves IPAM data in and out as CSV for reports and bulk changes.
  • Audit events answer "who had this IP at that time?" during an investigation.
  • Module not installed on the office PC (Count 0), so syntax and outputs come from Microsoft Learn; the lab is a Linux equivalent.

252.2 How it works

Find-IpamFreeAddress on the network
  1. 1.IPAM server collects scopes + leases from DHCP (dhcp1.contoso.com)

  2. 2.Zones come from DNS, logon events from the DC / NPS

  3. 3.Data is stored as Block > Subnet > Range > Address in the WID database

  4. 4.Admin PC: Get-IpamRange | Find-IpamFreeAddress

  5. 5.IPAM server returns the free IPs, e.g. IpAddress : 10.12.3.1

252.3 Find-IpamFreeAddress, Find-IpamFreeRange, Find-IpamFreeSubnet

CmdletInput objectKey options
Find-IpamFreeAddressA range (pipe or -InputObject)-NumAddress N, -TestReachability (ping + DNS record check)
Find-IpamFreeRangeA subnet-NumberOfAddresses, -NumberOfRanges
Find-IpamFreeSubnetA block-SubnetPrefix 24, -NumberOfSubnets
PowerShell
$FreeIPs = Get-IpamRange -StartIPAddress "10.12.3.1" `  -EndIPAddress "10.12.3.254" |  Find-IpamFreeAddress -NumAddress 10 -TestReachability $Subnet = Get-IpamSubnet -NetworkId 12.1.0.0/16Find-IpamFreeRange -InputObject $Subnet -NumberOfAddresses 256 $IPAMBlock = Get-IpamBlock -NetworkId 10.1.0.0/16Find-IpamFreeSubnet -InputObject $IPAMBlock -SubnetPrefix 24
Output
PS C:\> $FreeIPs[0]IpAddress       : 10.12.3.1PingStatus      : NoReplyDnsRecordStatus : NotFoundPS C:\> $Subnet = Get-IpamSubnet -NetworkId 12.1.0.0/16PS C:\> Find-IpamFreeRange -InputObject $Subnet -NumberOfAddresses 256StartIPAddress : 12.1.0.0EndIPAddress   : 12.1.0.255NetworkId      : 12.1.0.0/16

252.4 Export-IpamSubnet and Import-IpamAddress

  • Export-IpamSubnet writes all subnets of an address family to CSV; give -Path or -PassThru.
  • -NetworkType NonVirtualized|Provider|Customer filters; -Force overwrites an old file.
  • Import-IpamAddress adds and updates addresses from CSV; -ErrorPath writes failed rows to an error file.
  • With -ManagedByService, -ServiceInstance and -NetworkId it does a full sync of one range: addresses not in the CSV are deleted.
PowerShell
Export-IpamSubnet -AddressFamily IPv4 `  -AddressSpace Default -Path "C:\Subnets_v4.csv" Import-IpamAddress -AddressFamily IPv4 `  -Path "C:\addressv4.csv" -ErrorPath "C:\" -Force # full sync of one range (InventoryImport)Import-IpamAddress -AddressFamily IPv4 `  -Path "C:\addressupdatev4.csv" `  -ManagedByService "IPAM" -ServiceInstance "Localhost" `  -NetworkId "10.10.10.0/24"

252.5 Get- cmdlets for the hierarchy

CmdletUseful optionsDocs example shows
Get-IpamAddress-AddressFamily (required), -AddressCategory Public|Private|Global, -MappingToSubnet/Range/Block, -UnmappedDuplicate 172.16.10.16: once from VMM, once from IPAM
Get-IpamAddressSpace-Name, -Type, -MappingToProviderAddressSpaceContosoMusic IPv4 about 62% utilized
Get-IpamBlock-AddressCategory, -NetworkId + Start/End, -MappingToParentBlock (child blocks)10.0.0.0/8 PercentageUtilized 30
Get-IpamAddressUtilizationThresholdNo optionsUnder 20, Over 80
Get-IpamRange-AddressFamily (required), -StartIPAddress/-EndIPAddress, -ManagedByServiceTwo overlapping ranges in 172.16.19.0/24 (VMM and IPAM)
Get-IpamSubnet-NetworkId (CIDR), -Unmapped, -MappingToBlockFilter on Overlapping
PowerShell
# duplicate addressesGet-IpamAddress -AddressFamily IPv4 -AddressSpace Default |  Where-Object {$_.duplicate -eq $true} Get-IpamAddressSpace -Name "ContosoMusic"Get-IpamBlock -NetworkId "10.0.0.0/8" `  -StartIPAddress 10.0.0.0 -EndIPAddress 10.255.255.255Get-IpamAddressUtilizationThreshold # overlapping rangesGet-IpamRange -AddressFamily IPv4 -AddressCategory Private `  -AddressSpace "Default" | where {$Overlapping -eq $True}
Output
PS C:\> Get-IpamAddressSpace -Name "ContosoMusic"Name                           : ContosoMusicType                           : CustomerAddressSpaceDescription                    : Music Mp3AssociatedProviderAddressSpace : MainDatacenterIpv4PercentageUtilized         : 62.0805369127517Ipv6PercentageUtilized         : 0PS C:\> Get-IpamBlock -NetworkId "10.0.0.0/8" -StartIPAddress 10.0.0.0 -EndIPAddress 10.255.255.255Utilization        : UnderPercentageUtilized : 30PS C:\> Get-IpamAddressUtilizationThresholdUnderUtilizationThreshold   OverUtilizationThreshold-------------------------   ------------------------20                          80
Output
PS C:\> Get-IpamRange -AddressFamily IPv4 -AddressCategory Private -AddressSpace "Default" | where {$Overlapping -eq $True}Overlapping      : TrueNetworkID        : 172.16.19.0/24StartAddress     : 172.16.19.10EndAddress       : 172.16.19.110ManagedByService : VMMOverlapping      : TrueNetworkID        : 172.16.19.0/24StartAddress     : 172.16.19.50EndAddress       : 172.16.19.150ManagedByService : IPAM

252.6 Server settings, DHCP and DNS data

  • Get-IpamServerConfiguration (docs example for Get-IpamConfiguration): version and TCP port.
  • Get-IpamDatabase: WID or SQL Server settings; Get-IpamCapability: IpAddressTracking status.
  • Get-IpamServerInventory -ServerType DC|DNS|DHCP|NPS; Get-IpamDiscoveryDomain lists discovery domains.
  • Get-IpamCustomField / Get-IpamCustomFieldAssociation read custom fields and their links.
  • DHCP: Get-IpamDhcpScope (-AddressFamily required), Get-IpamDhcpSuperscope, Get-IpamDhcpServer.
  • DNS: Get-IpamDnsServer, Get-IpamDnsZone (-ZoneType Forward|IPv4Reverse|IPv6Reverse), Get-IpamDnsResourceRecord (-ZoneName), Get-IpamDnsConditionalForwarder.
  • No option = return everything.
PowerShell
Get-IpamCapabilityGet-IpamDiscoveryDomainGet-IpamServerInventory -AddressFamily IPv4 -ServerType DHCPGet-IpamDhcpScope -AddressFamily IPv4 -ServerFqdn "dhcp.contoso.com"Get-IpamDnsZone -ZoneType Forward -ZoneName "northamerica.contoso.com"Get-IpamDnsResourceRecord -ZoneName "northamerica.contoso.com" -RecordType AAAA
Output
PS C:\> Get-IpamCapabilityName                Status----                ------IpAddressTracking   EnabledPS C:\> Get-IpamDiscoveryDomainDomainName    DiscoverDc   DiscoverDns   DiscoverDhcp----------    ----------   -----------   ------------contoso.com   True         True          Trueminings.com   False        True          True

252.7 Audit and configuration events

  • All three take -StartDate / -EndDate and return at most 10,000 results, then a warning.
  • Get-IpamConfigurationEvent: changes made in IPAM, filter by -UserName / -UserDomainName.
  • Get-IpamDhcpConfigurationEvent: changes on DHCP servers; pipe to Export-Csv.
  • Get-IpamIpAddressAuditEvent: search by -IpAddress, -ClientId, -HostName or -UserName; -CorrelateLogonEvents joins leases with logons.
IP address tracking with Get-IpamIpAddressAuditEvent
  1. 1.client1 gets a lease for 10.10.1.1 from DHCP

  2. 2.The user logs on at the DC

  3. 3.IPAM collects DHCP lease events and DC logon events

  4. 4.Admin runs Get-IpamIpAddressAuditEvent -IpAddress 10.10.1.1 -CorrelateLogonEvents

  5. 5.IPAM answers: 10.10.1.1 = client1 + user

PowerShell
$Today = Get-Date$LastMonth = $Today.AddDays(-30)Get-IpamConfigurationEvent -StartDate $LastMonth -EndDate $Today `  -UserName "Administrator" -UserDomainName "Contoso.com"Get-IpamDhcpConfigurationEvent -EndDate $Today -StartDate $StartDate |  Export-Csv "C:\IpamEvents.csv"Get-IpamIpAddressAuditEvent -StartDate $LastMonth -EndDate $Today `  -IpAddress 10.10.1.1 -CorrelateLogonEvents

252.8 Lab: free IPs and free subnets on Linux

  • Lab network n252-lan 192.168.252.0/24: ipam .10, web1 .21, db1 .22, app1 .23; scan only this lab.
  • nmap -sn = ARP ping only; used IPs go to /tmp/used.txt (.1 is the Docker gateway).
  • seq + awk = Find-IpamFreeAddress idea; sipcalc -s 24 + routes = Find-IpamFreeSubnet idea; column -t = Export-IpamSubnet idea.
Terminal
nmap -sn -n 192.168.252.0/24nmap -sn -n -oG - 192.168.252.0/24 |  awk '/Up$/{print $2}' | sort -t. -k4,4n > /tmp/used.txt # free = .1-.254 minus used, first 5seq 1 254 | awk 'NR==FNR{split($1,a,"."); used[a[4]]=1; next}  !($1 in used){print "192.168.252."$1}' /tmp/used.txt - | head -5awk 'END{printf "used %d of 254 = %.2f%%\n", NR, NR*100/254}' /tmp/used.txt
Output
Nmap done: 256 IP addresses (5 hosts up) scanned in 2.31 seconds[ipam]$ cat /tmp/used.txt192.168.252.1192.168.252.10192.168.252.21192.168.252.22192.168.252.23 192.168.252.2192.168.252.3192.168.252.4192.168.252.5192.168.252.6[ipam]$ awk 'END{printf ...}' /tmp/used.txtused 5 of 254 = 1.97%
Terminal
ip -4 route | awk '{print $1}' > /tmp/routes.txtsipcalc 192.168.252.0/22 -s 24 |  awk '/^Network/{print $3"/24"}' |  while read n; do    grep -qx $n /tmp/routes.txt && s=used || s=free    echo "$n $s"; done | tee /tmp/subnets.txt { echo NetworkId,Status; tr ' ' , < /tmp/subnets.txt; } > /tmp/subnets.csvcolumn -t -s, /tmp/subnets.csv
Output
[ipam]$ awk '$2=="used"{u++} END{...}' /tmp/subnets.txtblock 192.168.252.0/22: 2 of 4 /24 used = 50%[ipam]$ column -t -s, /tmp/subnets.csvNetworkId         Status192.168.252.0/24  used192.168.253.0/24  used192.168.254.0/24  free192.168.255.0/24  free

252.9 Common mistakes

  • Trusting "free in IPAM" blindly: IPAM does not know a manual static IP; use -TestReachability (ping + DNS) or a real scan.
  • Running an Import-IpamAddress full sync with the wrong CSV: addresses missing from the file get deleted.
  • Forgetting -AddressFamily on Get-IpamAddress / Get-IpamRange / Get-IpamDhcpScope (required).
  • Expecting more than 10,000 audit results in one query; narrow the date range.

252.10 Interview tips

  • 10 free unused IPs: Get-IpamRange | Find-IpamFreeAddress -NumAddress 10 -TestReachability.
  • Planning a new subnet: Get-IpamBlock (PercentageUtilized) + threshold -> Find-IpamFreeSubnet -> Find-IpamFreeRange -> Export-IpamSubnet to share.
  • "Who had this IP?": Get-IpamIpAddressAuditEvent -CorrelateLogonEvents.
  • Default thresholds in the docs: under 20%, over 80%.