基於某些原因有時候我們要幫 User 變更印表機
比如 Printer Server 換一台了,或是租賃的影印機換一台之類的
通常對於電腦操作較熟悉的 User 用搜尋安裝就可以了 (AD 環境下有做好相關設定)
但某些人就是需要你幫忙
為了成為一位體貼 User 的 MIS,用 GPO 派送 Script 去更換印表機也是很正常的事情
以下 VBS 是非常多年以前寫的,變數的部份很偷懶就直接用兩個陣列下去自己代 Printer 進去就好
反正這種東西只是臨時性的,程式寫醜一點無所謂
設定好後可以用 GPO 設定 User 在登入時執行,Script 邏輯:
- 記下 User 有哪幾台
- 記下 User 的 Default Printer 是哪一台
- 刪除 Printer
- 新增 Printer (on New Server)
- 設定 Default Printer
===== 程式開始 =====
Dim PrinterToReplace
Dim PrinterReplacement
' PrinterToReplace 與 PrinterReplacement 欄位數量必須相同
' 下例:
' "\\Printer1.Contoso.com\HPPrinter1" 更換為 "\\Printer1.Contoso.com\HPPrinter1"
' "\\Printer1.Contoso.com\HPPrinter11" 更換為 "\\Printer2.Contoso.com\HPPrinter12"
PrinterToReplace = Array("\\Printer1.Contoso.com\HPPrinter1","\\Printer1.Contoso.com\HPPrinter11")
PrinterReplacement = Array("\\Printer1.Contoso.com\HPPrinter1","\\Printer2.Contoso.com\HPPrinter12")
strComputer = "."
Set WSHNetwork = CreateObject("WScript.Network")
Set ObjWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
Set objNewPort = objWMIService.Get("Win32_TCPIPPrinterPort").SpawnInstance_
Set objPrinter = objWMIService.Get("Win32_Printer").SpawnInstance_
Set ColPrinterObjs = ObjWMIService.ExecQuery("Select * From Win32_Printer")
DefaultPrinter = ""
For Each Printer in ColPrinterObjs
For i=0 to UBound(PrinterToReplace)
Match = ""
if LCase(Printer.Name) = LCase(PrinterToReplace(i)) then
WSHNetwork.AddWindowsPrinterConnection PrinterReplacement(i)
if Printer.Default = True then
DefaultPrinter=PrinterReplacement(i)
end if
WSHNetwork.RemovePrinterConnection Printer.Name
end if
next
Next
if DefaultPrinter <> "" then
LoopTimes = 0
do while LoopTimes < 100
if LCase(Printer.Name) = LCase(DefaultPrinter) then
WSHNetwork.SetDefaultPrinter DefaultPrinter
exit do
end if
LoopTimes = LoopTimes + 1
WScript.Sleep(1000)
Loop
end if
' 刪除沒用到的連接埠
On Error Resume Next
Set objDictionary = CreateObject("Scripting.Dictionary")
Set colPrinters = objWMIService.ExecQuery ("Select * from Win32_Printer")
For Each ListPrinter in colPrinters
objDictionary.Add ListPrinter.PortName
Next
Set colPorts = objWMIService.ExecQuery("Select * from Win32_TCPIPPrinterPort")
For Each ListPrinterPort in colPorts
If objDictionary.Exists(ListPrinterPort.Name) Then
Else
ListPrinterPort.Delete_
End If
Next
' 刪除沒用到的連接埠
沒有留言:
張貼留言