PowerShell: Add printers to DNS
I realized today that most of our printers at work did not have DNS entries. This isn’t a big problem for users because they’re on our print server and they get their printers through Group Policy. The Print server has each printer set up to the IPs. We just implemented a management system today, and it lists the printers by IP and because the DNS entries were “Unknown” for most of them, it wasn’t easy to determine what printer we were looking at.
I found several PowerShell examples on the web for interacting with Microsoft DNS servers, and took a bit from each to come up with this script. It takes the shared printer name and publishes that as the DNS name. This does cause a problem if you have spaces in a share name, but our environment doesn’t, so I didn’t program for that issue.
Here’s the script:
Function Add-ARecord
{
Write-Host -ForegroundColor DarkCyan “Importing Printer information”
# Imports all printers from Print Server
$printers = (get-WmiObject -class $Class -computername $printSvr)
$Arecord = [WmiClass]“\\$dnsSvr\root\MicrosoftDNS:MicrosoftDNS_AType”
$class = 1
$ttl = 3600 # Time-To-Live in seconds
$printers | ForEach-Object{
Get the name of the Shared Printer
$name = $_.ShareName
# Get IP information from TCP/IP port
$address = $_.portname.Replace(“IP_”, “”) # output variables for testing
#write-host -ForegroundColor Yellow $server, $zone, $name, $class, $ttl, $address
# create DNS A Records
$Arecord.CreateInstanceFromPropertydata($server, $zone, $name, $class, $ttl, $address)
}
}
$Class = “win32_printer” # WMI class for printers
# Change to suit your environment
$printSvr = “printsvr” # Print server
$dnsSvr = “dnssvr” # DNS Server
$zone = “dnszone” # DNS Zone
Add-ARecord
[edit] A couple people left comments that this didn’t work for them, and I was unable to get it to work for me again. I swear it worked the day I needed it. If anyone can tell me why it doesn’t work now, I’d appreciate it.
$server is not defined. I guess it should be $dnsSvr
Furthermore I tested the script to add a dns entry and it does not work.
I guess you changed the script or did you not use it?
Best regards,
Andi
same here, not working