PowerShell IpamServer module (3/4)
Part 3 of the IpamServer module: bulk Import of ranges and subnets from CSV, provisioning the IPAM server and its GPOs, moving the IPAM database from WID to SQL Server, and the Remove-/Rename- cmdlets that clean up data. The lab rebuilds the same ideas with SQLite and ipcalc.
253.1 Why is it used?
- Import loads hundreds of ranges/subnets from a CSV instead of typing them one by one.
- Provisioning gives the IPAM server access to the managed DHCP/DNS servers through GPOs.
- Move-IpamDatabase is needed when WID is too small and you want SQL Server.
- Remove- cmdlets clean stale records and old events so reports stay correct.
- Office PC: module not installed (Count 0); syntax/output from Microsoft Learn, practical on Linux.
253.2 Import-IpamRange and Import-IpamSubnet
- Step 1
1.Admin PC sends subnets.csv (5 rows) to the IPAM server
- Step 2
2.IPAM server validates every row
- Step 3
3.3 valid subnets are added/updated in the WID database
- Step 4
4.2 failed rows go back as errors.csv (-ErrorPath)
- Import-IpamRange: -Path and -AddressFamily required; default mode adds new ranges and updates existing ones.
- -ErrorPath = folder for the CSV of failed rows; without it the file goes to Documents.
- -ManagedByService + -ServiceInstance = update mode: ranges not in the CSV are DELETED (-DeleteMappedAddresses deletes their addresses too).
- -CreateSubnetIfNotFound creates the parent subnet.
- Import-IpamSubnet: if there is no NetworkType column, all subnets go to the Default address space.
Import-IpamRange -AddressFamily IPv4 -Path "C:\Rangev4.csv" -Force # update mode: ranges not in the CSV are deletedImport-IpamRange -AddressFamily IPv4 -Path "C:\Rangev4.csv" ` -ManagedByService "IPAM" -ServiceInstance "localhost" -ErrorPath "C:\" Import-IpamSubnet -AddressFamily IPv4 -Path "C:\subnetsv4.csv" -Force253.3 Provisioning: Invoke-IpamGpoProvisioning and Invoke-IpamServerProvisioning
- Step 1
1.Admin on the IPAM server runs Invoke-IpamGpoProvisioning
- Step 2
2.Request goes to the domain controller (PDC emulator by default)
- Step 3
3.DC creates and links IPAM1_DHCP, IPAM1_DNS, IPAM1_DC_NPS
- Step 4
4.GPOs give the IPAMUG group access on managed DHCP and DNS servers
A real domain change: never run on the office PC
| Option | Meaning |
|---|---|
| -Domain, -GpoPrefixName | Required for GPO provisioning |
| -IpamServerFqdn | Default: local computer name |
| -DomainController | Default: PDC emulator |
| -DelegatedGpoUser / -DelegatedGpoGroup | Let non domain admins edit the GPOs |
| Invoke-IpamServerProvisioning (no params) | WID database, manual access to managed servers |
| -ProvisioningMethod Automatic -GpoPrefix IPAM1 | GPO-based access |
| -DatabaseServer -DatabaseName -DatabasePort | Use SQL Server instead of WID (-DatabaseAuthType Windows|SQL) |
Invoke-IpamGpoProvisioning -Domain "Ds2-infra.Contoso.com" ` -GpoPrefixName "IPAM1" -PassThru -Force Invoke-IpamServerProvisioning -ProvisioningMethod Automatic -GpoPrefix IPAM1Invoke-IpamServerProvisioning -DatabaseServer "Db1.Contoso.com" ` -DatabaseName "Ipamdb" -DatabasePort 1433PS C:\> Invoke-IpamGpoProvisioning -Domain "Ds2-infra.Contoso.com" -GpoPrefixName "IPAM1" -PassThru -ForceDisplayName : IPAM1_DNSDomainName : ds2-infra.contoso.comGpoStatus : AllSettingsEnabledDisplayName : IPAM1_DC_NPSDomainName : ds2-infra.contoso.comGpoStatus : AllSettingsEnabledDisplayName : IPAM1_DHCPDomainName : ds2-infra.contoso.comGpoStatus : AllSettingsEnabled253.4 Move-IpamDatabase
- Step 1
1.IPAM data is in the Windows Internal Database (WID)
- Step 2
2.Move-IpamDatabase creates a new schema on SQL Server (ContosoDB22, port 1433)
- Step 3
3.All IPAM data is copied
- Step 4
4.IPAM settings now point to the new DB: DatabaseType MSSQL
- Step 5
5.Old WID files are renamed with a timestamp
- Required: -DatabaseServer, -DatabaseName, -DatabasePort, -DatabaseAuthType (Windows|SQL).
- Source can be WID or SQL; destination is SQL Server.
- Docs advice: shut down IPAM clients first, keep the same collation on both sides.
Get-IpamDatabase # before: DatabaseType WIDMove-IpamDatabase -DatabaseServer "ContosoDB22" ` -DatabaseName "IpamDB1" -DatabasePort 1433 ` -DatabaseAuthType WindowsGet-IpamDatabase # afterPS C:\> Move-IpamDatabase -DatabaseServer "ContosoDB22" -DatabaseName "IpamDB1" -DatabasePort 1433 -DatabaseAuthType Windows[Y] Yes [N] No [S] Suspend [?] Help (default is "Y"): YPS C:\> Get-IpamDatabaseDatabaseType : MSSQLWidSchemaPath :DatabaseServer : ContosoDB22DatabaseName : IpamDB1DatabasePort : 1433DatabaseAuthType : Windows253.5 Remove- cmdlets for addresses, ranges, subnets, blocks
| Cmdlet | Default behaviour | Option to go further |
|---|---|---|
| Remove-IpamAddress | Deletes the IPAM record only (not the DHCP reservation or DNS record) | -InputObject / pipeline (e.g. expired addresses) |
| Remove-IpamRange | Addresses move to the unmapped address space | -DeleteMappedAddresses |
| Remove-IpamSubnet | Error if ranges are mapped (safe) | -DeleteAssociatedRanges, -DeleteAssociatedAddresses |
| Remove-IpamBlock | Subnets remapped, ranges unmapped | -DeleteAssociatedSubBlocks |
| Remove-IpamAddressSpace | Not removed while IPs/blocks/subnets are linked | -Force, -PassThru |
Get-IpamAddress -AddressFamily IPv4 | Where-Object { $_.ExpiryStatus -eq "Expired"} | Remove-IpamAddress Get-IpamRange -StartIPAddress 10.12.3.1 -EndIPAddress 10.12.3.254 | Remove-IpamRange Remove-IpamSubnet -NetworkId 10.12.0.0/16 -NetworkType NonVirtualizedPS C:\> Remove-IpamSubnet -NetworkId 10.12.0.0/16 -NetworkType NonVirtualizedConfirmDeleting the non-virtualized subnet Contoso_2 with network 10.12.0.0/16.[Y] Yes [N] No [S] Suspend [?] Help (default is "Y"): yRemove-IpamSubnet : There are ranges associated with this subnet.+ FullyQualifiedErrorId : WIN32 8,Remove-IpamSubnet253.6 Events, inventory, custom fields and access scope
- Remove-IpamConfigurationEvent / Remove-IpamDhcpConfigurationEvent / Remove-IpamIpAddressAuditEvent / Remove-IpamUtilizationData: -EndDate required, delete history up to that date.
- Do not give today's date for config events: the next collection brings today's events back; use yesterday.
- DHCP events stay on the DHCP server; only the IPAM DB copy is deleted.
- Remove-IpamDiscoveryDomain: error if the domain is not configured. Remove-IpamServerInventory: IPAM stops managing the server, its IPAM data is deleted, the server config itself does not change.
- Remove-IpamCustomField fails while the field is assigned or in an association; remove the association first. Built-in fields cannot be removed or renamed.
- Rename-IpamCustomField / Rename-IpamCustomValue keep associations and assignments valid.
- Set-IpamAccessScope: decides which admin manages which object; Global is created at install, sub scopes like \Global\Europe.
$Today = Get-DateRemove-IpamDhcpConfigurationEvent -EndDate $Today.AddDays(-1)Remove-IpamServerInventory -Name "Dhcp01" Remove-IpamCustomFieldAssociation -CustomFieldOne "VmmLogicalNetwork" ` -CustomFieldTwo "NetworkSite"Rename-IpamCustomField -Name "Building" -newName "Building_Main" -PassThru $Zone = Get-IpamDnsZone -ZoneType Forward -ZoneName "dublin.contoso.com"Set-IpamAccessScope -IpamDnsZone -InputObject $Zone ` -AccessScopePath "\Global\Europe" -PassThruPS C:\> Remove-IpamServerInventory -Name "Dhcp01"ConfirmDeleting server Dhcp01.contoso.com from IPAM server inventory.Do you want to continue?[Y]Yes [N] No [S] Suspend [?] Help (default is "Y"):253.7 Lab: SQLite IPAM (import, delete, move)
- Linux equivalent, not IpamServer: container ipam1 keeps the inventory in /var/lib/ipam/ipam.db.
- import-subnet.sh validates each row with busybox ipcalc -n; bad rows go to an error CSV (= -ErrorPath).
- A foreign key (PRAGMA foreign_keys=ON) blocks deleting a subnet that still has a range, like Remove-IpamSubnet.
- .backup + copy to ipam2 + matching sha256 + integrity_check ok = Move-IpamDatabase idea.
#!/bin/sh# n253-import-subnet.sh FILE.csvDB=/var/lib/ipam/ipam.dbERR=/var/lib/ipam/errors/subnet-errors.csvtail -n +2 "$1" |while IFS=, read -r net name site; do out=$(ipcalc -n "$net" 2>&1) && [ "$out" = "NETWORK=${net%/*}" ] || { echo "REJECT $net: $out" echo "$net,$name,$site,$out" >> "$ERR" continue; } sqlite3 "$DB" "INSERT OR REPLACE INTO subnet VALUES('$net','$name','$site');" echo "OK $net $name"done$ cd /root && ./import-subnet.sh subnets.csvOK 192.168.73.0/26 ServersOK 192.168.73.64/26 UsersOK 192.168.73.128/26 LabREJECT 192.168.73.300/26: ipcalc: bad IP address: 192.168.73.300REJECT 192.168.73.200/26: NETWORK=192.168.73.192$ cd /root && ./import-range.sh ranges.csvOK 192.168.73.10-192.168.73.50 (IPAM, localhost)OK 192.168.73.70-192.168.73.120 (IPAM, localhost)REJECT 192.168.73.130-192.168.73.200: not inside 192.168.73.128/26sqlite3 /var/lib/ipam/ipam.db \ "DELETE FROM address WHERE ip='192.168.73.11'; SELECT changes() || ' row deleted';"sqlite3 -cmd 'PRAGMA foreign_keys=ON' /var/lib/ipam/ipam.db \ "DELETE FROM subnet WHERE network_id='192.168.73.0/26';" # move: consistent backup, copy to ipam2, verifysqlite3 /var/lib/ipam/ipam.db '.backup /srv/ipamdb/ipam.db'docker exec n253-ipam1 cat /srv/ipamdb/ipam.db | docker exec -i n253-ipam2 sh -c 'cat > /srv/ipam.db'sha256sum /srv/ipam.db | cut -c1-16sqlite3 /srv/ipam.db 'PRAGMA integrity_check; PRAGMA database_list;'1 row deletedError: stepping, FOREIGN KEY constraint failed (19)(exit 19)[ipam1]$ sha256sum /srv/ipamdb/ipam.db | cut -c1-1677b56b85fa58c7d2[ipam2]$ sha256sum /srv/ipam.db | cut -c1-1677b56b85fa58c7d2[ipam2]$ sqlite3 /srv/ipam.db 'PRAGMA integrity_check; PRAGMA database_list;'ok0|main|/srv/ipam.db1|temp|253.8 Common mistakes
- Thinking Remove-IpamAddress also removes the DHCP reservation and DNS record: it only deletes the IPAM record (use the DhcpServer / DnsServer modules).
- Running Import-IpamRange in update mode with a partial CSV: CSV has 2 ranges, IPAM had 5 -> only 2 remain.
- Moving the database while IPAM clients are open or with a different collation.
- Running GPO provisioning without understanding it changes the domain (needs domain admin rights).
253.9 Interview tips
- WID to SQL: Get-IpamDatabase (type + path) -> SQL DB with same collation -> shut down clients -> Move-IpamDatabase -> verify DatabaseType MSSQL.
- Explain the update mode of Import-IpamRange: not in the CSV = deleted.
- Remove-IpamSubnet is safe by default: it errors if ranges are mapped.
- GPO provisioning creates three GPOs: <prefix>_DHCP, <prefix>_DNS, <prefix>_DC_NPS.