我正在尝试将本地计算机上的打印机从旧打印服务器移动到新的打印服务器。如果我保持打印机名称相同我的代码工作。但是如果我想在重命名打印机的同时尝试重置objItem.Name的值。在下面的代码中,如何在select select语句中重命名objItem.Name?
On Error Resume Next
Dim strComputer, DefaultPTR
Dim objWMIService, colItems, WshNetwork
Dim LogFile
Dim wshShell
CONST ForWriting = 2
CONST ForAppending = 8
Set wshShell = CreateObject( "WScript.Shell" )
Set objFSO = CreateObject("Scripting.FileSystemObject")
UserProf = wshShell.ExpandEnvironmentStrings( "%USERPROFILE%" )
LogFile = UserProf & "\MovePtrsToNewPrintServerName.txt"
If (objfso.FileExists(UserProf & "\MovePtrsToNewPrintServerName.txt")) Then
'WScript.Echo("File exists!")
WScript.Quit()
Else
Set objFile = objFSO.CreateTextFile(LogFile, ForWriting)
End If
strComputer = "."
DefaultPTR=0
Set WshNetwork = CreateObject("WScript.Network")
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
Set colItems = objWMIService.ExecQuery("Select * from Win32_Printer",,48)
For Each objItem in colItems
If UCase(Left(objItem.Name,21)) = "\\OldPrintServerName\" Then
If objItem.Attributes And 4 Then
DefaultPTR = 1
End If
objItem.Name.Delete_
Select Case UCase(objItem.Name)
Case "\\OldPrintServerName\HP4000"
objItem.Name.Rename "\\OldPrintServerName\HPP3005" '<--- I know this doesn't work so
' how can I rename objItem.Name to
' the new printer name?
End Select
WshNetwork.AddWindowsPrinterConnection "\\NewPrintServerName\" & Right(objItem.Name,Len
(objItem.Name) - 9)
If Not Err.Number = 0 then
objFile.WriteLine "Problem with " & Right(objItem.Name,Len(objItem.Name) - 21)
Err.Clear
End if
If DefaultPTR = 1 then
WshNetwork.SetDefaultPrinter "\\NewPrintServerName\" & Right(objItem.Name,Len
(objItem.Name) - 21)
DefaultPTR = 0
End If
End If
Next
objFile.WriteLine "Printers have been moved from OldPrintServerName to NewPrintServerName"
答案 0 :(得分:0)
是的,RenamePrinter方法可供使用。我已经更新了下面的代码以适应命令。
具体来说:
Select Case UCase(objItem.Name)
Case "\\OldPrintServerName\HP4000"
objItem.RenamePrinter("HPP3005")
End Select
此外,您需要区分打印机名称和ShareName。 ShareName是用户直接转到打印服务器时在网络浏览器中显示的内容。
因此,要配置共享名称而不是打印机名称,它将是:
For Each objPrinter in colPrinters
objPrinter.ShareName = "HPP3005"
objPrinter.Put_
Next
On Error Resume Next
Dim strComputer, DefaultPTR
Dim objWMIService, colItems, WshNetwork
Dim LogFile
Dim wshShell
CONST ForWriting = 2
CONST ForAppending = 8
Set wshShell = CreateObject( "WScript.Shell" )
Set objFSO = CreateObject("Scripting.FileSystemObject")
UserProf = wshShell.ExpandEnvironmentStrings( "%USERPROFILE%" )
LogFile = UserProf & "\MovePtrsToNewPrintServerName.txt"
If (objfso.FileExists(UserProf & "\MovePtrsToNewPrintServerName.txt")) Then
'WScript.Echo("File exists!")
WScript.Quit()
Else
Set objFile = objFSO.CreateTextFile(LogFile, ForWriting)
End If
strComputer = "."
DefaultPTR=0
Set WshNetwork = CreateObject("WScript.Network")
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
Set colItems = objWMIService.ExecQuery("Select * from Win32_Printer",,48)
For Each objItem in colItems
If UCase(Left(objItem.Name,21)) = "\\OldPrintServerName\" Then
If objItem.Attributes And 4 Then
DefaultPTR = 1
End If
objItem.Name.Delete_
Select Case UCase(objItem.Name)
Case "\\OldPrintServerName\HP4000"
objItem.ShareName = "HPP3005"
objItem.Put_
End Select
WshNetwork.AddWindowsPrinterConnection "\\NewPrintServerName\" & Right(objItem.Name,Len
(objItem.Name) - 9)
If Not Err.Number = 0 then
objFile.WriteLine "Problem with " & Right(objItem.Name,Len(objItem.Name) - 21)
Err.Clear
End if
If DefaultPTR = 1 then
WshNetwork.SetDefaultPrinter "\\NewPrintServerName\" & Right(objItem.Name,Len
(objItem.Name) - 21)
DefaultPTR = 0
End If
End If
Next
objFile.WriteLine "Printers have been moved from OldPrintServerName to NewPrintServerName"
这是一篇详细介绍方法的文章 - &gt; HERE