pixel0815
Goto Top

TCP IP Druckerport auf Win 7 Client von IP ändern auf DNS Hostname

Moin,

ich suche nach einer einfachen Lösung den Druckerport zu ändern.
Stelle es mir so vor, dass ein Skript auf dem Client ausgeführt wird und es gibt eine CSV Datei die aus IP;Hostname besteht.
Wenn nun ein TCP/IP Port entdeckt wird der mit dem Inhalt der CSV passt, dann soll der Port für den Drucker auf den DNS Namen umgestellt werden. Wenn der Hostname schon im Port hinterlegt ist, dann soll natürlich nichts unernommen werden, nur der Druckername soll sich ändern und alles muss protokolliert werden zur etwaigen Fehlersuche.

Ebenfalls soll der Druckername identisch sein mit dem Hostnamen nur ohne FQDN.
Ausgeführt wird das Skript per Softwareverteilungsjob aus den Clients.

Liebe Grüße
Pixel

Update:

$ports = Get-ChildItem -Path "hklm:\SYSTEM\CurrentControlSet\Control\Print\Monitors\Standard TCP/IP Port\Ports"  
foreach ($portPath in ($ports.Name -replace "HKEY_LOCAL_MACHINE","HKLM:"))  
{
    $hostName = (Get-ItemProperty -Path $portPath -Name HostName).HostName
    if ($hostName)
    {
        $resolvedName = (Resolve-DnsName -Name $hostName -DnsOnly -NoRecursion -Type PTR -ErrorAction SilentlyContinue).NameHost
        if ($resolvedName)
        {
            Write-Verbose "$hostName resolved to $resolvedName"  
            Set-ItemProperty -path $portPath -Name HostName -Value $resolvedName
        } else {
            Write-Verbose "Unable to resolve $hostname"  
        }
    }
}

Damit kann ich nun wunderbar aus dem DNS den Hostnamen abfragen und benutzen.
Wenn man nun noch den Druckernamen und den Portnamen ändern könnte wäre das perfekt. Ich komme wohl nicht drumherum einen neuen Port zu erstellen dann und den alten zu löschen und den neuen dann zuweisen oder? Prinzipiell möchte ich aber eigentlich mehr mit CSV arbeiten damit ich volle kontrolle habe.

Content-Key: 310174

Url: https://administrator.de/contentid/310174

Ausgedruckt am: 19.03.2024 um 04:03 Uhr

Mitglied: 129813
129813 18.07.2016 um 14:34:37 Uhr
Goto Top
Changing printerport HostAddress:
gwmi win32_TCPIPPrinterPort | ?{$_.HostAddress -like '\\HOST_OLD\*'} | %{$_.HostAddress = '\\HOST_NEW\xxxxx'; $_.put()}  
Regards
Mitglied: pixel0815
pixel0815 18.07.2016 um 14:39:27 Uhr
Goto Top
nice solution. i want to rename this

ByteCount               : 
Caption                 : 
CreationClassName       : Win32_TCPIPPrinterPort
Description             : Standard-TCP/IP-Port
HostAddress             : printer4762.test.loc
InstallDate             : 
Name                    : 10.12.12.13
PortNumber              : 9100
Protocol                : 1
Queue                   : 
SNMPCommunity           : public
SNMPDevIndex            : 1

The Name Property Name face-sad
Mitglied: 129813
129813 18.07.2016 aktualisiert um 14:56:02 Uhr
Goto Top
Delete the printer first, then the port, then recreate port with the new name and reattach printer.

How to create a printer port ? Look:
$wmi = ([WMICLASS]"\\$printserver\ROOT\cimv2:Win32_TCPIPPrinterPort")  
$wmi.psbase.scope.options.enablePrivileges = $true
$port = $wmi.CreateInstance()
$port.Name = "Your new Name"  
$port.Protocol = 1 
$port.Portnumber = "XXXXX"  
$port.HostAddress = "XXXXXX"  
$port.put()
Mitglied: pixel0815
pixel0815 18.07.2016 um 14:57:49 Uhr
Goto Top
Like this?

$ports = Get-ChildItem -Path "hklm:\SYSTEM\CurrentControlSet\Control\Print\Monitors\Standard TCP/IP Port\Ports"  

Function PortInstall {
param ($PortName,$PrinterIP,$servername)
            
$PPrinter=([WMIClass]"\\.\ROOT\cimv2:Win32_TcpIpPrinterPort").CreateInstance()  
$PPrinter.name           = $Portname
$PPrinter.Protocol       = 1
$PPrinter.HostAddress    = $PrinterIP
$PPrinter.PortNumber     = 9100 
$PPrinter.SNMPCommunity  = "public"  
$PPrinter.SNMPEnabled  = 1
$PPrinter.PortMonMibPortIndex = 1
$PPrinter.Put()

}

foreach ($portPath in ($ports.Name -replace "HKEY_LOCAL_MACHINE","HKLM:"))  
{
    $hostName = (Get-ItemProperty -Path $portPath -Name HostName).HostName
    if ($hostName)
    {
        $resolvedName = (Resolve-DnsName -Name $hostName -DnsOnly -NoRecursion -Type PTR -ErrorAction SilentlyContinue).NameHost
        if ($resolvedName)
        {
            Write-Host "$hostName resolved to $resolvedName"  
            Set-ItemProperty -path $portPath -Name HostName -Value $resolvedName
            PortInstall -PortName "$resolvedname" -PrinterIP "$resolvedname"  
        } else {
            Write-Host "Unable to resolve $hostname"  
        }
    }
}

.. How can i delete the printer and set this printer again with the correct driver? we have 34 different printer models.
there is no solution to create a new printerport and attach this to the old printer?
Mitglied: 129813
129813 18.07.2016 aktualisiert um 15:00:44 Uhr
Goto Top
Mitglied: 129813
129813 18.07.2016 aktualisiert um 15:03:30 Uhr
Goto Top
there is no solution to create a new printerport and attach this to the old printer?
create port, then get the Printer again via WMI Class WIN32_Printer and change it's port to the new one.

Also have a look at
Migration von Netzwerkdruckern auf Arbeitsplatzsystemen mit Hilfe von Powershell

You already find a lot here face-smile
Mitglied: pixel0815
pixel0815 18.07.2016 um 15:08:11 Uhr
Goto Top
Oh no, thats no Printserver Printer.
thats a local Printer with a TCP IP Port, i want to switch from IP Adress to the Hostname on local installed Printer.
Mitglied: 129813
129813 18.07.2016 um 15:16:02 Uhr
Goto Top
That doesn't matter ...it's the same logic.
Mitglied: 129813
129813 18.07.2016 aktualisiert um 15:20:00 Uhr
Goto Top
You can also create a new port with different name with my first posted line. You simply exchange $_.HostAddress with $_.Name, then a new port with this name is automatically created. After that assign the new port to the printer, finished.