Jump to content


 


Register a free account to unlock additional features at BleepingComputer.com
Welcome to BleepingComputer, a free community where people like yourself come together to discuss and learn how to use their computers. Using the site is easy and fun. As a guest, you can browse and view the various discussions in the forums, but can not create a new topic or reply to an existing one unless you are logged in. Other benefits of registering an account are subscribing to topics and forums, creating a blog, and having no ads shown anywhere on the site.


Click here to Register a free account now! or read our Welcome Guide to learn how to use this site.

Photo

Powershell Help


  • Please log in to reply
1 reply to this topic

#1 JasonBourne2

JasonBourne2

  • Members
  • 19 posts
  • OFFLINE
  •  
  • Local time:07:57 AM

Posted 25 October 2016 - 03:19 PM

Hi, i have a script that remaps my printers, this script will delete printers and then remap them to the new server. Problem is, if users already have the new printers added, it deletes them too.
Does anyone know what i can add to the script below to prevent the already existing(if existing) printers from the new server and where i can add it? You help is greatly appreciated.


 
Clear-Host
This procedure adds a new printer
 
Function InstallNewPrinter{ Param ([string]$currentPrinter, [string]$newPrinter)
Write-Host "CurrentPrinter : " $currentPrinter
 
Write-Host "New Printer : " $newPrinter
 
Write-Host "Is Printer Default : " $isDefault
 
  #Add new Printer 
  $global:net.AddWindowsPrinterConnection($newPrinter) 
 
  #Add current printer to the TobeDeleted List 
  $global:PrintersTobeDeleted += $currentPrinter 
}
This procedure sets the respected new printer default
 
Function SetPrinterDefault{
Param ([string]$defaultPrinter)
#If current printer is default, make the new a default printer $global:net.SetDefaultPrinter($defaultPrinter) }
This procedure deletes all the old printer which were replaced by this script.
 
Function DeleteCurrentPrinters{
  foreach ($printerTodelete in $global:PrintersTobeDeleted) 
  { 
        $global:net.RemovePrinterConnection($printerTodelete) 
  } 
}
Set print server name
 
$Printserver = "."
This section will identify all the installed Network printer under logged in users profile using WMI
 
$Printers = Get-WMIObject Win32_Printer -computername $Printserver -Filter "Network=True"
This is a blank array to store the installed Network printer under logged in users profile and pass it on
 
$PrintersTobeDeleted = @() $newPrinter = "" $defaultPrinter = ""
if($Printers) { # Get Network object $global:net = new-Object -com WScript.Network
  foreach ($Printer in $Printers) 
  { 
Write-Host "Name: " $Printer.Name
 
Write-Host "Location: " $Printer.Location
 
Write-Host "Comment: " $Printer.Comment
 
Write-Host "DriverName: " $Printer.DriverName
 
Write-Host "Shared: " $Printer.Shared
 
Write-Host "ShareName: " $Printer.ShareName
 
Just swap your old printer name with new server\printer name here
 
      switch ($Printer.ShareName)  
      {  
          "printer1" {$newPrinter = "\\newserver\printer1"} 
          "printer2" {$newPrinter = "\\newserver\printer2"} 
          "printer3" {$newPrinter = "\\newserver\printer3} 
          "printer4" {$newPrinter = "\\newserver\printer4"} 
 
      } 
      if ($newPrinter -ne "") 
      { 
              if ($Printer.Default) {$defaultPrinter = $newPrinter} 
            InstallNewPrinter $Printer.Name $newPrinter  
            $newPrinter = "" 
      } 
  } 
SetPrinterDefault $defaultPrinter 
   DeleteCurrentPrinters

Edited by Platypus, 25 October 2016 - 11:02 PM.
Deleted duplicate topic from Programming forum.


BC AdBot (Login to Remove)

 


#2 sflatechguy

sflatechguy

  • BC Advisor
  • 2,242 posts
  • OFFLINE
  •  
  • Gender:Male
  • Local time:08:57 AM

Posted 29 October 2016 - 10:08 AM

Problem is the last line -- you need to move that to the top. Your current script is set to delete the current printers just before exiting.






0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users