Showing posts with label baseline. Show all posts
Showing posts with label baseline. Show all posts

Thursday, May 29, 2014

Change server for installed printers

While changing our printserver we needed a way to update configuration on all machines in the domain.
First, the printers were migrated from old server to new with exactly the same configuration.

To update workstations, Configuration Baseline was created in SCCM.

Discovery script (returning something else than 0 = not compliant):
Get-WmiObject -Class Win32_Printer | where { $_.SystemName -like "*OLDSERVER*" } | Measure-Object | select -ExpandProperty Count

Remediation script (returning $true will mean failure in process):
$fail = $false
Get-WmiObject -Class Win32_Printer | where { $_.SystemName -like "*OLDSERVER*" } | select Name, Default | foreach {
    $newname = $($($_.name) -replace "OLDSERVER", "NEWSERVER")
    write-host "Replacing $($_.name) with $newname"
    #ADD NEW PRINTER
    (New-Object -ComObject WScript.Network).AddWindowsPrinterConnection($newname)
    if ($($_.default)) {
        write-host "Above is default printer"
        $default = $newname
    }
   
    #WAIT AND CHECK FOR SUCCESS
    write-host "Waiting 30 seconds to check for success"
    Start-Sleep 30
    if ( $(Get-WmiObject -Class Win32_Printer | where { $_.Name -eq $newname } | Measure-Object | select -ExpandProperty Count) -gt 0 ) {
        write-host "SUCCESS, removing old"
        (New-Object -ComObject WScript.Network).RemovePrinterConnection($($_.name))
        write-host "Setting printer as default"
        if ($default) {
            (New-Object -ComObject WScript.Network).SetDefaultPrinter($default)
            $default = $null
        }
    } else {
        write-host "FAIL, keeping old"
        $fail = $true
    }
}
write-host "Next line is True if errors, False if no errors:"
return $fail


The scripts were made quite fast and are not optimized, it is not needed as this is only a temporary configuration baseline. After old printserver is decommissioned, this baseline will be disabled.

Could this be done in a single line of code? :)