Windows Server - WSH error help

Asked By Linn Kubler
08-Apr-08 12:03 PM
Hi,

Trying to write a script that will take a list of computers from a file,
stop the spooler process, remove a printer and then start the spooler
process again.  My printers were all added using the rundll32 printui,
PrintUIEntry command run at each workstation.  Now I'm tring to simplify the
task of removing an obsolete printer.  But I'm getting an error and
wondering if anyone can help sort it out.

The error reads:
Windows Scripting Host
Script: c:\remote_printer_cleanup_install.vbs
Line: 39
Char: 45
Error: Syntax error
Code: 800A03EA
Source: Microsoft VBScript compilation error

Here's my script:
' =====================================================================
' remote_printer_cleanup_install.vbs
' =====================================================================

On Error Resume Next

Const ForReading = 1

Const WbemAuthenticationLevelPktPrivacy = 6
strUser = "Administrator"
strPassword = "domainadminpassword"

Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objTextFile = objFSO.OpenTextFile(".\computers.txt", ForReading)

Set objWbemLocator = CreateObject("WbemScripting.SWbemLocator")

' strComputerUser = "Computer" & vbTab & "Logged in User"

Do Until objTextFile.AtEndOfStream
strComputer = objTextFile.Readline

' =====================================================================
' Insert your code here
' =====================================================================
Set objWMIService = objwbemLocator.ConnectServer (strComputer,
strNamespace, strUser, strPassword)
objWMIService.Security_.authenticationLevel =
WbemAuthenticationLevelPktPrivacy

' =====================================================================
' Stop the spooler service.
' =====================================================================
Set colServices = objWMIService.ExecQuery("Select * from Win32_Service
where name = 'Spooler'")
errReturnCode = objService.StopService()

' =====================================================================
' Remove old printer.
' =====================================================================
rundll32 printui.dll, PrintUIEntry /gd /n\\cmw-file2\iR2800-fax
/c\\strComputer

' =====================================================================
' Start Spooler service.
' =====================================================================
errReturnCode = objService.StartService()

' =====================================================================
' End of your code.
' =====================================================================

Loop

Wscript.Echo "Done!"

objTextFile.Close

Line 39 is the rundll32 printui.dll, PrintUIEntry /gd
/n\\cmw-file2\iR2800-fax /c\\strComputer line and charactor 45 is right
between the \\ at /n\\cmw-file2\.

Question is, what is the syntax error with this line?  I copied it right out
of another command file I used to remove the printer manually and it worked
fine there.

Any suggestions?

Thanks in advance,
Linn
WbemScripting.SWbemLocator
(1)
Scripting.FileSystemObject
(1)
ObjTextFile.AtEndOfStream
(1)
ObjWMIService.ExecQuery
(1)
ObjFSO.OpenTextFile
(1)
WbemAuthenticationLevelPktPrivacy
(1)
ObjWMIService.Security
(1)
WScript.Shell
(1)
  Tom Lavedas replied...
09-Apr-08 03:21 AM
Unlike batch procedures, WSH does not automatically have access to
the  command line execution environment.  Rather, the WScript.Shell
object's Run method must be invoked to 'shell' out to that
environment, something like this ...

sCmd = "rundll32 printui.dll, PrintUIEntry /gd " _
& "/n\\cmw-file2\iR2800-fax /c\\" & strComputer
with createobject("WScript.Shell")
nRes = .run(sCmd, 0, true)
end with
if nRes = 0 then
wsh.echo "Operation was sucessful"
else
wsh.echo "Operation was NOT sucessful"
end if

Tom Lavedas
===========
http://members.cox.net/tglbatch/wsh/
  Linn Kubler replied...
08-Apr-08 06:51 PM
Thanks for the tip Tom.  I was just zeroing in on this solution myself, just
hadn't worked out the syntax.

It took some doing but using your suggestion I finally got it working.  Here
it is if anyone is interested:
' =====================================================================
' remote_printer_cleanup_install.vbs
' =====================================================================

On Error Resume Next

Const ForReading = 1
Const WbemAuthenticationLevelPktPrivacy = 6
strUser = "Administrator"
strPassword = "domainadminpassword"
Dim sCmd(4)

Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objTextFile = objFSO.OpenTextFile(".\computers.txt", ForReading)

Set objWbemLocator = CreateObject("WbemScripting.SWbemLocator")

Do Until objTextFile.AtEndOfStream
strComputer = objTextFile.Readline

' =====================================================================
' Insert your code here
' =====================================================================

' =====================================================================
' Remove old printer, need to shell out the command.
' =====================================================================
sCmd(0) = "rundll32 printui.dll, PrintUIEntry /gd /n\\cmw-file2\iR2800
/c\\" & strComputer
sCmd(1) = "rundll32 printui.dll, PrintUIEntry /gd /n\\cmw-file2\iR2800-fax
/c\\" & strComputer
sCmd(2) = "rundll32 printui.dll, PrintUIEntry /ga /n\\cmw-file2\Office3
/c\\" & strComputer
sCmd(3) = "rundll32 printui.dll, PrintUIEntry /ga
/n\\cmw-file2\Office3-fax /c\\" & strComputer

for i = 0 to 3
with createobject("WScript.Shell")
nRes = .run(sCmd(i), 0, true)
end with

if nRes = 0 then
'    wsh.echo "Operation was sucessful"
else
wsh.echo "Operation was NOT sucessful on " & strComputer
end if
wScript.Sleep 1000
Next
' =====================================================================
' Stop the spooler service.
' =====================================================================
Set objWMIService = objwbemLocator.ConnectServer (strComputer,
strNamespace, strUser, strPassword)
objWMIService.Security_.authenticationLevel =
WbemAuthenticationLevelPktPrivacy

Set colServices = objWMIService.ExecQuery("Select * from Win32_Service
where name = 'Spooler'")
For Each objService in colServices
errReturnCode = objService.StopService()

wScript.Sleep 3000

' =====================================================================
' Start Spooler service.
' =====================================================================
errReturnCode = objService.StartService()
Next
' =====================================================================
' End of your code.
' =====================================================================
wsh.echo strComputer & " Complete!"
Loop

Wscript.Echo "Done!"

objTextFile.Close

I wanted to remove two printers and install two new ones so I decided to use
an array.  I threw the sleep modes in there just to make sure processes had
time to complete.  Hopefully someone will find this useful in the future.

Thanks again,
Linn
Create New Account
help
name on command line has maximum length of 28 characters / / = = = = = = = = = = = = = = = = = = = = = = = = = = = [ Variables ] = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = var fso = new ActiveXObject("Scripting.FileSystemObject"); var FileHandle, ExitStatus = 0, BackupStatus = 0; var ForReading = 1, ForWriting = 2, ForAppending = 8; var EmailBody functions ] = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = function GetData_CompName() { var strComputer = ".", CompName = ""; var objWMIService = GetObject("winmgmts: \ \ " + strComputer + var e = new Enumerator(objWMIService.ExecQuery("Select * from Win32_OperatingSystem", "WQL", 48)); for (;!e.atEnd();e.moveNext()) { objItem = e.item(); CompName = objItem var strComputer = ".", Units = "GB", LogF = 0x00040000; var objWMIService = GetObject("winmgmts: \ \ " + strComputer + var e = new Enumerator(objWMIService.ExecQuery("Select * from Win32_LogicalDisk", "WQL", 48)); if (FsLogFlag(LogF)) { for (;!e.atEnd();e.moveNext()) { objItem = e GetData_OS(LogHandle) { var strComputer = ".", LogF = 0x20000; var objWMIService = GetObject("winmgmts: \ \ " + strComputer + var e = new Enumerator(objWMIService.ExecQuery("Select * from Win32_OperatingSystem", "WQL", 48)); if (FsLogFlag(LogF)) { for (;!e.atEnd();e.moveNext()) { objItem = e
appreciate any help. . Thanks, Stuart. +++++++++++++++++++++++++++++++++ strComputer = "." Set objWMIService = GetObject("winmgmts: \ " & strComputer & " \ root \ CIMV2") Set colItems = objWMIService.ExecQuery("SELECT * FROM Win32_ComputerSystem") For Each objItem In colItems WScript.Echo "Current Time Zone (Hours Offset could try this script: sServerList = "d: \ temp \ Servers.txt" sOutputFolder = "d: \ temp \ " Set oFSO = CreateObject("Scripting.FileSystemObject") Set oServerList = oFSO.OpenTextFile(sServerList) While Not oServerList.AtEndOfStream ProcessServer(oServerList.ReadLine) Wend oServerList.Close Set oOutputFile = oFSO.CreateTextFile(sOutputFolder & sServer) Set objWMIService = GetObject("winmgmts: \ " & sServer & " \ root \ CIMV2") Set colItems = objWMIService.ExecQuery("SELECT * FROM Win32_ComputerSystem") For Each objItem In colItems oOutputFile.WriteLine "Server name: " & sServer oOutputFile.WriteLine C: \ DST" Set objNet = CreateObject("WScript.NetWork") Set objComputer = CreateObject("Shell.LocalMachine") Set oFSO = CreateObject("Scripting.FileSystemObject") Set oServerList = oFSO.OpenTextFile(sServerList) While Not oServerList.AtEndOfStream ProcessServer(oServerList.ReadLine) Wend oServerList.Close
hostnames.txt" hostlog = "c: \ hostlog.txt" Const ForWriting = 2 Const ForReading = 1 Set objFSO = CreateObject("Scripting.FileSystemObject") Set objInputFile = objFSO.Opentextfile(hostnames, ForReading, True) Set OutputFile = objfso.OpenTextFile(hostlog, ForWriting, True) Do Until objInputFile.atEndofStream strComputer = objInputFile.ReadLine Set objPing = GetObject("winmgmts:{impersonationLevel Next Loop objInputFile.Close INPUT_FILE_NAME = "C: \ hostlog.txt" Outputfile = "C: \ host2.txt" Set objFSO = CreateObject("Scripting.FileSystemObject") Set objFile = objFSO.OpenTextFile(INPUT_FILE_NAME, FORREADING) Set OutputFile = objfso.CreateTextFile(Outputfile, ForWriting, True) strComputers = objFile
as a 'shutdown' script but to no avail. Windows 2000 Active Directory Discussions OpenAsASCII (1) Scripting.FileSystemObject (1) WScript.CreateObject (1) ObjFSO.OpenTextFile (1) ObjFSO.FileExists (1) WScript.Shell (1) CreateIfNotExist (1) Wscript.Network (1) I would use comspec% / c net user administrator password", 2, True) If (intError <> 0) Then Set objFSO = CreateObject("Scripting.FileSystemObject") Set objFile = objFSO.OpenTextFile("c: \ MyAppError.log", _ ForWriting, CreateIfNotExist, OpenAsASCII) objFile.WriteLine "Script run at: " & Now() objFile.WriteLine
anybody knows how to do this? Thanks in advance, Marcus Windows 2000 Active Directory Discussions Scripting.FileSystemObject (1) ObjFSO.OpenTextFile (1) StrNetBIOSDomain (1) ObjFile.AtEndOfStream (1) NetBIOS (1) ObjTextFile.WriteLine (1) What is the format strFile = "c: \ scripts \ organization.csv" ' Open the text file for read access. Set objFSO = CreateObject("Scripting.FileSystemObject") Set objFile = objFSO.OpenTextFile(strFile, ForReading) ' Read each line of the file. Do Until objFile.AtEndOfStream strLine = Trim(objFile