2014-03-28 63 views
0

我正嘗試將本地計算機上的打印機從舊打印服務器移動到新打印服務器。如果我保持打印機名稱與我的代碼相同。但是,如果我想在重新命名打印機的同時嘗試重置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" 
+0

的'Win32_Printer'類有一個[RenamePrinter](http://msdn.microsoft.com/en-us/library/aa393050%28v=vs.85%29.aspx)函數。 'objItem.RenamePrinter「」'應該做的伎倆。 – Bond

+0

不幸的是,這並沒有幫助。我不只是試圖重新命名一臺打印機。我試圖將幾個移動到一個新的打印服務器,並在同一時間重新命名一些。我真的想改變objItem.Name的值,所以其餘的代碼將工作。 – user3473084

回答

0

是的,RenamePrinter方法可供使用。我更新了下面的代碼以適應該命令。

具體做法是:

Select Case UCase(objItem.Name) 
    Case "\\OldPrintServerName\HP4000" 
     objItem.RenamePrinter("HPP3005") 
    End Select 

而且,你所需要的打印機名稱和共享名來區分。 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" 

而這裏的一篇文章詳細介紹了方法 - >HERE

+0

Hello Rich,感謝您的回覆。我將不得不看腳本男人的文章,並試圖更好地理解。使用修改後的代碼,如果我在Select Case語句後立即添加了wscript.echo objItem.Name,它仍然以\\ OldPrintServerName \ HP4000而不是\\ OldPrintServerName \ HPP3005形式出現。 – user3473084

+0

對,因爲我們重命名了打印機存儲的文件,所以既然這是聯網打印機,我已經更新了上面的代碼以重命名SharedName。請嘗試使用上面更新的代碼並讓我知道。 – Rich

+0

你好,很抱歉,我仍然得到相同的結果。同樣,如果我在End Select之後立即添加了wscript.echo objItem.Name,它仍然以\\ OldPrintServerName \ HP4000而不是\\ OldPrintServerName \ HPP3005形式出現。謝謝您的幫助。我知道必須有一種方法來修改objItem的name屬性。 – user3473084