PowerShell

Script-Gallery: Fix-Printer.ps1

#######################################################################
## (C) 2015 Michael Miklis (michaelmiklis.de)
##
##
## Filename:      Fix-Printer.ps1
##
## Version:       1.0
##
## Release:       Final
##
## Requirements:  -none-
##
## Description:   Script for fixing fqdn during computer domain
##                movement.
##
## This script is provided 'AS-IS'.  The author does not provide
## any guarantee or warranty, stated or implied.  Use at your own
## risk. You are free to reproduce, copy & modify the code, but
## please give the author credit.
##
####################################################################
Set-PSDebug -Strict
Set-StrictMode -Version latest


function Fix-Printer {
    <#
    .SYNOPSIS
    Fixes printer fqdn references in registry 

    .DESCRIPTION
    The Fix-Printer CMDlet fixes the serverName, uNCName and url registry key
    for the windows spooler service

    .PARAMETER OldDomain
    FQDN of the old domain

    .PARAMETER NewDomain
    FQDN of the new domain

    .EXAMPLE
    Fix-Printer -OldDomain "olddomain.com" -NewDomain "newdomain.com"

    .NOTES
    You need to run this CMDlet with elevated permissions
    #>
    
    param (
        [parameter(Mandatory=$true)]
        [ValidateNotNullOrEmpty()]$OldDomain,
        [parameter(Mandatory=$true)]
        [ValidateNotNullOrEmpty()]$NewDomain
    )

    foreach ($printer in (Get-ChildItem -Path HKLM:SYSTEMCurrentControlSetControlPrintPrinters)) {
    
        if ((Test-RegistryValue -Path ("HKLM:SYSTEMCurrentControlSetControlPrintPrinters" + $printer.PSChildName + "DsSpooler") -Value ("serverName")) -eq $true) {
            $strOldVal = (Get-ItemProperty -Path ("HKLM:SYSTEMCurrentControlSetControlPrintPrinters" + $printer.PSChildName + "DsSpooler")).serverName
            Set-ItemProperty -Path ("HKLM:SYSTEMCurrentControlSetControlPrintPrinters" + $printer.PSChildName + "DsSpooler") -Name "serverName" -Value $strOldVal.Replace($olddomain, $newdomain)
        }
        
        if ((Test-RegistryValue -Path ("HKLM:SYSTEMCurrentControlSetControlPrintPrinters" + $printer.PSChildName + "DsSpooler") -Value ("uNCName")) -eq $true) {
            $strOldVal = (Get-ItemProperty -Path ("HKLM:SYSTEMCurrentControlSetControlPrintPrinters" + $printer.PSChildName + "DsSpooler")).uNCName
            Set-ItemProperty -Path ("HKLM:SYSTEMCurrentControlSetControlPrintPrinters" + $printer.PSChildName + "DsSpooler") -Name "uNCName" -Value $strOldVal.Replace($olddomain, $newdomain)
        }
        
        if ((Test-RegistryValue -Path ("HKLM:SYSTEMCurrentControlSetControlPrintPrinters" + $printer.PSChildName + "DsSpooler") -Value ("url")) -eq $true) {
            $strOldVal = (Get-ItemProperty -Path ("HKLM:SYSTEMCurrentControlSetControlPrintPrinters" + $printer.PSChildName + "DsSpooler")).url
            Set-ItemProperty -Path ("HKLM:SYSTEMCurrentControlSetControlPrintPrinters" + $printer.PSChildName + "DsSpooler") -Name "url" -Value $strOldVal.Replace($olddomain, $newdomain)
        }
    }
}

#___________________________________________________________________

function Test-RegistryValue {
    <#
    .SYNOPSIS
    Tests existence of regisry value (C) by Jonathan Medd

    .DESCRIPTION
    The Test-RegistryValue CMDlet was created by Jonathan Medd

    .PARAMETER Path
    Registry path

    .PARAMETER Value
    Registry value

    .EXAMPLE
    Test-RegistryValue -Path "HKCU:Environment" -Value "TEMP"
    #>
    
    param (
        [parameter(Mandatory=$true)]
        [ValidateNotNullOrEmpty()]$Path,
        [parameter(Mandatory=$true)]
        [ValidateNotNullOrEmpty()]$Value
    )

    try {
        Get-ItemProperty -Path $Path | Select-Object -ExpandProperty $Value -ErrorAction Stop | Out-Null
        return $true
    }

    catch {
        return $false
    }
}

Fix-Printer -OldDomain "olddomain.com" -NewDomain "newdomain.com"

You may also like
Get running processes including CPU and memory usage
Change DNS settings on multiple Windows Servers and Workstations
Creating PowerShell scripts with a GUI

Leave Your Comment

Your Comment*

Your Name*
Your Webpage