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
- Step 1
1.IPAM server collects scopes + leases from DHCP (dhcp1.contoso.com)
- Step 2
2.Zones come from DNS, logon events from the DC / NPS
- Step 3
3.Data is stored as Block > Subnet > Range > Address in the WID database
- Step 4
4.Admin PC: Get-IpamRange | Find-IpamFreeAddress
- Step 5
5.IPAM server returns the free IPs, e.g. IpAddress : 10.12.3.1
252.3 Find-IpamFreeAddress, Find-IpamFreeRange, Find-IpamFreeSubnet
| Cmdlet | Input object | Key options |
|---|---|---|
| Find-IpamFreeAddress | A range (pipe or -InputObject) | -NumAddress N, -TestReachability (ping + DNS record check) |
| Find-IpamFreeRange | A subnet | -NumberOfAddresses, -NumberOfRanges |
| Find-IpamFreeSubnet | A block | -SubnetPrefix 24, -NumberOfSubnets |
$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 24PS 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/16252.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.
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
| Cmdlet | Useful options | Docs example shows |
|---|---|---|
| Get-IpamAddress | -AddressFamily (required), -AddressCategory Public|Private|Global, -MappingToSubnet/Range/Block, -Unmapped | Duplicate 172.16.10.16: once from VMM, once from IPAM |
| Get-IpamAddressSpace | -Name, -Type, -MappingToProviderAddressSpace | ContosoMusic IPv4 about 62% utilized |
| Get-IpamBlock | -AddressCategory, -NetworkId + Start/End, -MappingToParentBlock (child blocks) | 10.0.0.0/8 PercentageUtilized 30 |
| Get-IpamAddressUtilizationThreshold | No options | Under 20, Over 80 |
| Get-IpamRange | -AddressFamily (required), -StartIPAddress/-EndIPAddress, -ManagedByService | Two overlapping ranges in 172.16.19.0/24 (VMM and IPAM) |
| Get-IpamSubnet | -NetworkId (CIDR), -Unmapped, -MappingToBlock | Filter on Overlapping |
# 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}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 80PS 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 : IPAM252.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.
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 AAAAPS C:\> Get-IpamCapabilityName Status---- ------IpAddressTracking EnabledPS C:\> Get-IpamDiscoveryDomainDomainName DiscoverDc DiscoverDns DiscoverDhcp---------- ---------- ----------- ------------contoso.com True True Trueminings.com False True True252.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.
- Step 1
1.client1 gets a lease for 10.10.1.1 from DHCP
- Step 2
2.The user logs on at the DC
- Step 3
3.IPAM collects DHCP lease events and DC logon events
- Step 4
4.Admin runs Get-IpamIpAddressAuditEvent -IpAddress 10.10.1.1 -CorrelateLogonEvents
- Step 5
5.IPAM answers: 10.10.1.1 = client1 + user
$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 -CorrelateLogonEvents252.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.
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.txtNmap 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%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[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 free252.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%.