I need some idea to correct this VBS code - vbscript

Hello everyone.
Hope everyone is fine.
I need help with this code below with the section (comment line) saying "'Help needed from here'":
strComputer = "."
Set objWMIService = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
Set colItems = objWMIService.ExecQuery("Select * from Win32_LocalTime")
For Each objItem in colItems
MsgBox "Welcome to the ALARM tool",0+0,"ALARM tool"
MsgBox "Note: 1) If the computer is given a shutdown or a restart command then the alarm will be disabled. 2) This ALARM tool dosen't support decimals & words. If you will enter a decimal, the elapsed message box will appear without processing the alarm due to the error. If you enter words then an error message will appear.",0+0,"ALARM tool"
thour=InputBox ("Type which hour you want the alarm to elapse in 24-hour format from 0 to 23 inclusive (not 24; instead 0): (E.G: 19, 15)","ALARM tool")
If thour="" Then
MsgBox "The value is empty, so no alarm is set.",16,"ALARM tool"
WScript.Quit
End If
tmin=InputBox ("Type which minute you want the alarm to elapse from 0 to 60 inclusive:","ALARM tool")
If tmin="" Then
MsgBox "The value is empty, so no alarm is set.",16,"ALARM tool"
WScript.Quit
End If
c=InputBox ("Enter your comment; leave it blank if you don't want one:","ALARM tool")
If c="" Then
MsgBox "Time: ''"+thour+":"+tmin+"'' Comment: None",0,"ALARM tool: Preview"
Else
MsgBox "Time: ''"+thour+":"+tmin+"'' Comment: ''"+c+"''",0,"ALARM tool: Preview"
End If
co=MsgBox ("Are you sure?",36,"ALARM tool")
If co="7" Then
MsgBox "Alarm cancelled!!!",0,"ALARM tool"
WScript.Quit
End If
'Help needed from here'
Do
If thour=objItem.Hour Then
If tmin=objItem.Minute Then
If c="" Then
MsgBox "ALARM set to elapse on "+thour+":"+tmin+" has elapsed!!!",0+0,"ALARM set to elapse in "+t
WScript.Quit
Else
MsgBox c,0+0,"ALARM set to elapse in "+t
WScript.Quit
End If
End If
End If
'No more help needed after this'
If Err.Number <> 0 Then
MsgBox "There's an error while setting the alarm. Maybe you typed words or it's an internal error. Please try again. Sorry for the inconvenience.",0,"ALARM tool"
WScript.Quit
End If
Loop
Next
You can give me the answer while even use it for yourself.Topic: Alarm for specific timeI will say thanks for helping me and it will be a very good help.

Related

Turn off a VBScript program while it is running

(This is a .vbs program) I am trying to make a message bot for a program but I came up with a problem where if you set the time for too long, and set number of messages too high, you can't stop the program easily and have to deal with your message being typed over and over again until it is finished. Is there a way to add an option while it is running to stop the message from being typed?
Here is the code:
set shell = createobject ("wscript.shell")
strtext = inputbox ("Type the message you like to type")
strtimes = inputbox ("How many times would you like to type this message?")
strspeed = inputbox ("How fast do you like to message? (1000 = one per sec, 100 = 10 per sec etc)")
strtimeneed = inputbox ("How many SECONDS do you need to get to your input box?")
If not isnumeric (strtimes & strspeed & strtimeneed) then
msgbox "You entered something else then a number on Times, Speed and/or Time need. Closing program"
wscript.quit
End If
strtimeneed2 = strtimeneed * 1000
do
msgbox "You have " & strtimeneed & " seconds to get to your input area where you are going to message."
wscript.sleep strtimeneed2
shell.sendkeys ("Hello" & "{enter}")
for i=0 to strtimes
shell.sendkeys (strtext & "{enter}")
wscript.sleep strspeed
Next
shell.sendkeys ("Bye" & "{enter}")
wscript.sleep strspeed * strtimes / 10
returnvalue=MsgBox ("Want to send the messages again with the same info?",36)
If returnvalue=6 Then
Msgbox "Ok Messagebot will activate again"
End If
If returnvalue=7 Then
msgbox "Messaging is shutting down"
wscript.quit
End IF
loop
You can just set up a shortcut to terminate all running wscript.exe processes. Click right mouse button on the desktop, in context menu choose New - Shortcut:
Enter %comspec% /c taskkill /f /im wscript.exe as location and click Next:
Enter a name you like and click Finish:
Also you may assign hot keys in shortcut properties.

VbScript make computer say something error

I have this file that makes a computer say something. I want it to loop with a VbCancel function. I get this error. Code so far:
Do
Dim Message, Speak
Message=InputBox("Enter text","Speak")
Set Speak=CreateObject("sapi.spvoice")
MsgBox ("You entered: " & Speak)
Speak.Speak Message
If Len(Speak) = 0 Then
MyMessageBox = MsgBox("Click Yes if you mean to Cancel." & vbCrLf & _
"If you mean to enter a zero length string, click No.", vbYesNo, "DO YOU MEAN TO CANCEL?")
If MyMessageBox = vbYes Then
MsgBox "Operation Cancelled"
Exit Sub
End If
Loop
BTW the error is Invalid exit statement
I'm working on Windows 7
Dim Message, Speak
Do
Message=InputBox("Enter text","Speak")
Set Speak=CreateObject("sapi.spvoice")
MsgBox ("You entered: " & Message)
Speak.Speak Message
If Len(Message) = 0 Then
MyMessageBox = MsgBox("Click Yes if you mean to Cancel." & vbCrLf & _
"If you mean to enter a zero length string, click No.", vbYesNo, "DO YOU MEAN TO CANCEL?")
If MyMessageBox = vbYes Then
MsgBox "Operation Cancelled"
Exit Do
End If
End If
Loop
You had several issues here
Exit Sub is for subroutines. You were trying to exit a Do loop
Speak is an object. I dont know if it has a string property but it is not itself a string. Both Len(Speak) and "You entered: " & Speak has Speak changed to Message.
You were missing an End If
I moved the Dim statements out of the loop. No point recreating the object over and over again.
Dim Message, Speak
Do
Message=InputBox("Enter text","Speak")
Set Speak=CreateObject("sapi.spvoice")
MsgBox ("You entered: " & Message)
Speak.Speak Message
If Len(Message) = 0 Then
MyMessageBox = MsgBox("Click Yes if you mean to Cancel." & vbCrLf & _
"If you mean to enter a zero length string, click No.", vbYesNo, "DO YOU MEAN TO CANCEL?")
If MyMessageBox = vbYes Then
MsgBox "Operation Cancelled"
Exit Do
End If
End If
Loop

VBScript to Display Countdown using Vbscript Graphical Elements

I wanted to display a MessageBox which displays countdown from 10 to 1 and autocloses after 10 seconds. As Msgbox in vbscript passes code execution untill the user acts on it i tried it using Popup in Wscript Shell Object
Dim counter
Dim oShell
counter = 10
Set oShell= CreateObject("Wscript.Shell")
While counter > 0
oShell.Popup " Left " & counter & " Seconds",1,"Remind"
counter = counter-1
Wend
But it auto-closes for every second and opens a new popup is there any way i can display the countdown and autoclose using the available GUI elements in vb script
Afraid not, the popup is modal & can't be interacted with while its displayed so there is no way to update its existing content.
If you want a more flexible UI you will need to use something different, the console or HTML in an HTA.
You can use Internet Explorer to create a non-modal display.
Set oIE = CreateObject("InternetExplorer.Application")
With oIE
.navigate("about:blank")
.Document.Title = "Countdown" & string(100, chrb(160))
.resizable=0
.height=200
.width=100
.menubar=0
.toolbar=0
.statusBar=0
.visible=1
End With
' wait for page to load
Do while oIE.Busy
wscript.sleep 500
Loop
' prepare document body
oIE.document.body.innerHTML = "<div id=""countdown"" style=""font: 36pt sans-serif;text-align:center;""></div>"
' display the countdown
for i=10 to 0 step -1
oIE.document.all.countdown.innerText= i
wscript.sleep 1000
next
oIE.quit
I have a code, but it might not help
dTimer=InputBox("Enter timer interval in minutes","Set Timer") 'minutes
do until IsNumeric(dTimer)=True
dTimer=InputBox("Invalid Entry" & vbnewline & vbnewline & _
"Enter timer interval in minutes","Set Timer") 'minutes
loop
flop=InputBox("What do you want to set the timer for?")
if dTimer<>"" then
do
WScript.Sleep dTimer*60*1000 'convert from minutes to milliseconds
Set Sapi = Wscript.CreateObject("SAPI.spVoice")
Sapi.speak "Time's up."
t=MsgBox(flop & vbnewline & vbnewline & "Restart Timer?", _
vbYesNo, "It's been " & dTimer &" minute(s)")
if t=6 then 'if yes
'continue loop
else 'exit loop
exit do
end if
loop
end if
please excuse my weird variable names.
This code is from https://wellsr.com/vba/2015/vbscript/vbscript-timer-with-wscript-sleep/
It actually is possible to edit the countdown, very easily for VBScript, so I don't understand what the problem is.
Here's the code:
Dim counter
Dim oShell
counter =InputBox("")
#if you add =InputBox you can have the choice to type in the amount of times the popup repeats#
Set oShell= CreateObject("Wscript.Shell")
While counter > 0
oShell.Popup " Time until shutdown " & counter & " Seconds",(1),"[3]Critical Viruses Detected
#and if you change the (1) to whatever number you want then you can change how fast the popup repeats and if you join this with the "open command" then you can make a gnarly nondangerous virus#
counter = counter-1
Wend

Windows Update Check with vbscript

has someone a script/or can help me to check, is there a windows update for a server available?
So i will get a mail when the yellow update icon is in the taskbar.
My idea is: Send a mail, if the wuauclt.exe longer than 10 minutes in the taskbar.
But i have no idea to make this.
I found only this:
Dim strComputer, strProcess
Do
strProcess = inputbox( "Please enter the name of the process (for instance: explorer.exe)", "Input" )
Loop until strProcess <> ""
Do
strComputer = inputbox( "Please enter the computer name", "Input" )
Loop until strComputer <> ""
If( IsProcessRunning( strComputer, strProcess ) = True ) Then
WScript.Echo "Process " & strProcess & " is running on computer " & strComputer
Else
WScript.Echo "Process " & strProcess & " is NOT running on computer " & strComputer
End If
Thanks for help.
How about something like this
'Microsoft magic
Set updateSession = CreateObject("Microsoft.Update.Session")
Set updateSearcher = updateSession.CreateupdateSearcher()
Set searchResult = updateSearcher.Search("IsInstalled=0 and Type='Software'")
'End Microsoft magic
If searchResult.Updates.Count <> 0 Then 'If updates were found
'This is where you add your code to send an E-Mail.
'Send E-mail including a list of updates needed.
'This is how you can list the title of each update that was found.
'You could include the list in the body of your E-Mail.
For i = 0 To searchResult.Updates.Count - 1
Set update = searchResult.Updates.Item(i)
WScript.Echo update.Title
Next
End If
wuauclt could quite possibly be running for more than 10 minutes, without necessarily be notifying the user that there are pending updates.
I know that this is StackOverflow, and this is a programming question, but I'm a sysadmin, and I'm of the opinion that this belongs on ServerFault, and that you're doing it wrong. WSUS (http://technet.microsoft.com/en-us/wsus/default.aspx) is designed to manage Windows Updates.

How do I know when a file has been modified in a VBA Macro?

Is there a way to watch a file in VBA (which is essentially VB6), so that I know when the file has been modified? -- similar to this only I don't want to know when a file is unused, just when its modified.
The answers I've found have recommended using "FileSystemWatcher" and the Win32 API "FindFirstChangeNotification". I can't figure out how to use these though, any idea?
Okay, I put together a solution that is able to detect file system changes, in VBA (VB6).
Public objWMIService, colMonitoredEvents, objEventObject
'call this every 1 second to check for changes'
Sub WatchCheck()
On Error GoTo timeout
If objWMIService Is Nothing Then InitWatch 'one time init'
Do While True
Set objEventObject = colMonitoredEvents.NextEvent(1)
'1 msec timeout if no events'
MsgBox "got event"
Select Case objEventObject.Path_.Class
Case "__InstanceCreationEvent"
MsgBox "A new file was just created: " & _
objEventObject.TargetInstance.PartComponent
Case "__InstanceDeletionEvent"
MsgBox "A file was just deleted: " & _
objEventObject.TargetInstance.PartComponent
Case "__InstanceModificationEvent"
MsgBox "A file was just modified: " & _
objEventObject.TargetInstance.PartComponent
End Select
Loop
Exit Sub
timeout:
If Trim(Err.Source) = "SWbemEventSource" And Trim(Err.Description) = "Timed out" Then
MsgBox "no events in the last 1 sec"
Else
MsgBox "ERROR watching"
End If
End Sub
Copy and paste this sub near the above, it is called automatically if needed to initialize the global vars.
Sub InitWatch()
On Error GoTo initerr
Dim watchSecs As Integer, watchPath As String
watchSecs = 1 'look so many secs behind'
watchPath = "c:\\\\scripts" 'look for changes in this dir'
strComputer = "."
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
Set colMonitoredEvents = objWMIService.ExecNotificationQuery _
("SELECT * FROM __InstanceOperationEvent WITHIN " & watchSecs & " WHERE " _
& "Targetinstance ISA 'CIM_DirectoryContainsFile' and " _
& "TargetInstance.GroupComponent= " _
& "'Win32_Directory.Name=""c:\\\\scripts""'")
MsgBox "init done"
Exit Sub
initerr:
MsgBox "ERROR during init - " & Err.Source & " -- " & Err.Description
End Sub
You should consider using a WMI temporary event consumer to watch the file, along the lines suggested here but narrowing it down to a specific file instead of a folder
(This is assuming you can't just keep an eye on the file's Modified Date property..)
Have a look here. The page has a "Watch Directory Demo" VB sample, by Bryan Stafford.
I take it into vb6,run,display:ERROR watching.

Resources