Solved

Convert small batch script to VBS

Posted on 2007-03-29
5
604 Views
Last Modified: 2008-01-09
Hi All,

I have a batch script (below) that backs up the printer divers using printmig, and logs the result into the event log.  I need to convert this into VBS.

___________________________________________________________________

:: backup print drivers using printmig utility
printmig -b \\localhost\print_share\prtbkp%computername%.cab

:: If the newly created printmig file doesn't exist log a sucess event
if not exist \\localhost\print_share\prtbkp%computername%.cab Eventcreate /L APPLICATION /so "Scheduled Task" /t ERROR /id 501 /d "backup Failed"

:: If the newly created printmig file does exist log a sucess event
if exist \\localhost\print_share\prtbkp%computername%.cab Eventcreate /L APPLICATION /so "Scheduled Task" /t INFORMATION /id 1 /d "backup Success"

___________________________________________________________________



Many thanks
0
Comment
Question by:detox1978
  • 3
  • 2
5 Comments
 
LVL 67

Expert Comment

by:sirbounty
ID: 18816204
This should do it...

'PrintBkup.vbs
Const WARNING = 3
Const INFO = 4

Dim objFSO:Set objFSO=CreateObject("Scripting.FileSystemObject")
Dim objShell:Set objShell=CreateObject("Wscript.Shell")
Dim objNet: Set objNet=CreateObject("Wscript.Network")
PC=objNet.ComputerName
strFile="\\localhost\print_share\prtbkp" & PC & ".cab"

objShell.Run "printmig -b " & strFile
If Not objFSO.FileExists (strFile) Then
  ret = objShell.LogEvent(WARNING, "Backup Failed", PC)
else
  ret = objShell.LogEvent(INFO,"Backup Success", PC)
End If

Set objShell=Nothing
Set objFSO=Nothing
Set objNet=Nothing
0
 
LVL 2

Author Comment

by:detox1978
ID: 18816405
Cheers,

I also need to add this;

regedit /e \\localhost\print_share\shares.reg HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\lanmanserver\Shares


which exports a registry hive....


0
 
LVL 67

Expert Comment

by:sirbounty
ID: 18816580
Easiest method with what you have here is:

'PrintBkup.vbs
Const WARNING = 3
Const INFO = 4

Dim objFSO:Set objFSO=CreateObject("Scripting.FileSystemObject")
Dim objShell:Set objShell=CreateObject("Wscript.Shell")
Dim objNet: Set objNet=CreateObject("Wscript.Network")
PC=objNet.ComputerName
strFile="\\localhost\print_share\prtbkp" & PC & ".cab"

objShell.Run "printmig -b " & strFile
If Not objFSO.FileExists (strFile) Then
  ret = objShell.LogEvent(WARNING, "Backup Failed", PC)
else
  ret = objShell.LogEvent(INFO,"Backup Success", PC)
End If
objShell.Run "regedit /e \\localhost\print_share\shares.reg HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\lanmanserver\Shares"

Set objShell=Nothing
Set objFSO=Nothing
Set objNet=Nothing
0
 
LVL 2

Author Comment

by:detox1978
ID: 18816991
Cheers,


I checked the first script and it works, but occationally it displays an error box saying "Microsoft VBScript runtime error '800a0005', line 13, char 3"


line 13 is;
  ret = objShell.LogEvent(WARNING, "Backup Failed", PC)
0
 
LVL 67

Accepted Solution

by:
sirbounty earned 500 total points
ID: 18817081
Try this version...

'PrintBkup.vbs
Const WARNING = 3
Const INFO = 4

Dim objFSO:Set objFSO=CreateObject("Scripting.FileSystemObject")
Dim objShell:Set objShell=CreateObject("Wscript.Shell")
Dim objNet: Set objNet=CreateObject("Wscript.Network")
PC=objNet.ComputerName
strFile="\\localhost\print_share\prtbkp" & PC & ".cab"

objShell.Run "printmig -b " & strFile
On Error Resume Next
If Not objFSO.FileExists (strFile) Then
  ret = objShell.LogEvent(WARNING, "Backup Failed", PC)
else
  ret = objShell.LogEvent(INFO,"Backup Success", PC)
End If
wscript.echo err.Description
On Error Goto 0
objShell.Run "regedit /e \\localhost\print_share\shares.reg HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\lanmanserver\Shares"

Set objShell=Nothing
Set objFSO=Nothing
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Hello again, all.  For those of you that have been following along, you'll know that this is my third article on this topic (though it is not Part III).  This article is sort of remedial, and probably the topic with which I should have started the s…
This script will sweep a range of IP addresses (class c only, 255.255.255.0) and report to a log the version of office installed. What it does: 1.)      Creates log file in the directory the script is run from (if it doesn't already exist) 2.)      Sweep…
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

947 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now