Deckyon
-
Total Posts
:
45
- Scores: 0
-
Reward points
:
0
- Joined: 8/1/2006
- Location: Louisville, KY - USA
-
Status: offline
|
Backup and restore network printers
Wednesday, March 03, 2010 9:31 AM
( permalink)
This script quickly lists network printers into a txt file for use on restoring them when the user gets a new PC or has their existing one reimaged. As this was quickly thrown together, it doesnt have all the features I would like to have in it yet, but, as with all my scripts, it is not done yet. I also plan on adding the ability to reset the default printer as well after all the printers are restored. I am also planning on making this run remotely and grabbing all users' printers and defaults. But, that is as time permits. '==========================================================================
' Title: RemapPrinter.vbs
' Date: 02/18/2010
' Author: Bradley Buskey
' Version: 1.01
' Updated: 02/22/2010
' Purpose: To temporarily store printers to be remapped.
' Updates: Added code to delete the old file.
' Cleaned up MsgBox code.
'==========================================================================
' Instructions:
' 1. Run script on the old workstation to get printer names.
' 2. Run script on the new workstation to remap printers.
' 3. Note: This does not set a default printer at this time.
'==========================================================================
' Checks to see if you are logged in as the user.
varUser = MsgBox("Are you logged in as the user getting swapped?", 36, "Verify Identity")
If varUser = "7" Then
MsgBox "You must be logged in as the user for the script to work properly." & vbCRLF & "Please log in and run this script as the user.", 48, "Please switch users."
Wscript.Quit
End If
Const ForAppending = 8
Const ForReading = 1
' Get the local computer name.
strComputer = "."
' Sets up the different objects required by the script to function.
Set WshNetwork = CreateObject("WScript.Network")
Set objWMIService = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
Set colInstalledPrinters = objWMIService.ExecQuery("Select * from Win32_Printer")
Set colItems = objWMIService.ExecQuery("Select * from Win32_ComputerSystem",,48)
Set WshShell = WScript.CreateObject("WScript.Shell")
Set objFSO = CreateObject("Scripting.FileSystemObject")
' Determines the user name of the current logged in user.
For Each objItem in colItems
UserName = objItem.UserName
arrUserName = Split(UserName, "\", -1, 1)
varUserName = arrUserName(1)
Next
' Set the filename of the text file to the current logged in user.
filOutput = varUserName & ".txt"
' Determine if a backup or restore is needed.
' Run the Backup (1) on the old PC and the Restore (2) on the new PC.
' MUST be logged in as the user.
varResponse = inputbox("Do you want to Read or Restore printers?" & vbCRLF & vbCRLF & _
"Enter '1' to Backup" & vbCRLF & "Enter '2' to Restore","Enable or Disable","1")
If varResponse = "1" Then
' Creates a text file with the listing of all network printers for the logged in user.
Set objOutputFile = objFSO.OpenTextFile (filOutput, ForAppending, True)
For Each objPrinter in colInstalledPrinters
strTest = Left(objPrinter.Name, 2)
If strTest = "\\" Then
objOutputFile.WriteLine(objPrinter.Name)
End If
Next
objOutputFile.Close
MsgBox "Printer mappings have been backed up in '" & filOutput & "'.", 64, "Script Complete"
Else
' Opens the text file matching the logged in user and reconnects to the printers listed.
Set objTextFile = objFSO.OpenTextFile (filOutput, ForReading)
Do Until objTextFile.AtEndOfStream
strPrinter = objTextFile.Readline
strTest = Left(strPrinter, 2)
If strTest = "\\" Then
WshNetwork.AddWindowsPrinterConnection strPrinter
End If
Loop
objTextFile.Close
varDelete = MsgBox("Do you want to delete the backup file?",36,"Delete File?")
If varDelete = vbYes Then
objFSO.DeleteFile(filOutput)
varDelText = "Backup file has been deleted."
Else
varDelText = "Backup file has NOT been deleted."
End If
MsgBox "Printers have been mapped." & vbCRLF & varDelText & vbCRLF & vbCRLF & "Please note: No Default printer has been set.", 64, "Script Complete"
End If
Wscript.Quit
|
|
|
|