I'm new to Powershell scripting and trying to write a script that will go through printer queues on a print server, get the print queue's port address, then compare the print queues port address to the DHCP reservation associated with the print queue port address on the server. If the port address of the print queue compares, it should then set the description of the DHCP reservation to reflect the print queue name and location. It should then also set the hostname option to be the print queue's name.
#Set DHCP reservation description and host value on server
#DHCP Server for site
$DHCPSvr = "Server1"
#DHCP Server Scope ID
$DHCPSvrScope = "10.224.148.0"
#Get Printer Management list of shared available printers
$PrinterObjects = Get-Printer -ComputerName Server1 * | Select-Object Name, PortName, Shared, Location |
Where-Object { ($_.Name -match "^.{6}$") -and ($_.PortName -match "IP*" -or $_.PortName -match '^10*') -and ($_.Shared -EQ $True)} |
Sort-Object -Property PortName -Descending
#Get DHCP Reservation list from server
$DHCPSvrReservations = Get-DhcpServerv4Reservation -ComputerName $DHCPSvr -ScopeId $DHCPSvrScope
foreach ($DHCPSvrReservation in $DHCPSvrReservations){
foreach ($PrinterObject in $PrinterObjects)
{
if ($DHCPSvrReservation.IPAddress -eq $PrinterObjects.PortName)
{
{Set-DhcpServerv4Reservation -ComputerName $DHCPSvr -IPAddress $PrinterObjects.PortName -Description "$PrinterObjects.Name & $PrinterObjects.Location"}
{Set-DhcpServerv4OptionValue -ScopeId $DHCPSvrScope -OptionId 12 $PrinterObjects.Name}
}
}
}
I'm a bit unsure of the following code within the IF statement, is this the right way to have the loop process the two commands?
{Set-DhcpServerv4Reservation -ComputerName $DHCPSvr -IPAddress $PrinterObjects.PortName -Description "$PrinterObjects.Name & $PrinterObjects.Location"}
{Set-DhcpServerv4OptionValue -ScopeId $DHCPSvrScope -OptionId 12 $PrinterObjects.Name}
Anyway, it would be good if someone could go over what I have at the moment and point out any issues with formatting and so on.