我正嘗試將本地計算機上的打印機從舊打印服務器移動到新打印服務器。如果我保持打印機名稱與我的代碼相同。但是,如果我想在重新命名打印機的同時嘗試重置objItem.Name的值。在下面的代碼中,我如何在我的select Case語句中重命名objItem.Name?如何在VBScript中重命名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"
的'Win32_Printer'類有一個[RenamePrinter](http://msdn.microsoft.com/en-us/library/aa393050%28v=vs.85%29.aspx)函數。 'objItem.RenamePrinter「」'應該做的伎倆。 –
Bond
不幸的是,這並沒有幫助。我不只是試圖重新命名一臺打印機。我試圖將幾個移動到一個新的打印服務器,並在同一時間重新命名一些。我真的想改變objItem.Name的值,所以其餘的代碼將工作。 – user3473084