Error while sending ( character with sendkeys in vbscript - vbscript

I am getting the following error when I try to send to send a "(" or ")" character using SendKeys .In my vbscript.
Invalid procedure call or argument
My script:
Set WshShell = WScript.CreateObject("WScript.Shell")
WScript.Sleep 100
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.OpenTextFile("C:\Documents and Settings\Administrator\Desktop\readme.txt", 1)
Wshshell.SendKeys "!##$%^&*()"
Do Until objFile.AtEndOfStream
strCharacters = objFile.Read(1)
WshShell.SendKeys strCharacters
Loop
It does not send the "(" and ")" when I try to send them before the loop but shows no error and continues till a little further where it encounters another "(" character and stops with the error.

Parenthesis are considered a special character and need to be surrounded in braces. See this link for more details.
Set WshShell = WScript.CreateObject("WScript.Shell")
WScript.Sleep 100
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.OpenTextFile("C:\Documents and Settings\Administrator\Desktop\readme.txt", 1)
Wshshell.SendKeys "!##$%^&*{(}{)}"
Do Until objFile.AtEndOfStream
strCharacters = objFile.Read(1)
WshShell.SendKeys strCharacters
Loop

Related

Sendkeys "variable"

Is it possible to use the SendKeys the type the characters inside a variable.
for example I got characters stored in variable firstname = Regie
Here is my code.
On Error Resume Next
Dim objSysInfo, objUser
Set objSysInfo = CreateObject("ADSystemInfo")
Set objUser = GetObject("LDAP://" & objSysInfo.UserName)
firstname = objUser.givenName
Set WshShell = WScript.CreateObject("WScript.Shell")
WshShell.AppActivate "Notepad"
WScript.Sleep 400
WshShell.SendKeys "{& objUser.givenName}"
WScript.Sleep 250
Or is there any way to make this happen?
Sure you can, try this little script on the command line, it will print "test".
Set WshShell = WScript.CreateObject("WScript.Shell")
keys = "test"
WshShell.SendKeys keys

Use VBscript to paste data into a txt file and save

I need to paste data from clipboard (Ctrl+V) into a text file and save it.
I already did some research and it seems that a text file doesn't have a method like SaveAs.
With code below, I could create a new text file and paste data into it, but I can't Save it:
Set WShshell = CreateObject("WScript.Shell")
WShshell.run "c:\WINDOWS\system32\notepad.exe",1
WshShell.AppActivate "notepad"
WShshell.SendKeys "^V"
I understand there is a method called CreateTextFile, but seems I can't perform paste with it.
I also tried to combine those two:
Set WShshell = CreateObject("WScript.Shell")
Set fso = CreateObject("Scripting.FileSystemObject")
fso.CreateTextFile "c:\1.txt",true
WshShell.AppActivate "notepad"
WShshell.SendKeys "^V"
But nothing happened...
UFT has its own clipboard access methods you could use. Here I've created an instance of the clipboard, and extracted its content into sText, then created a text file in C:\temp and written the data from the clipboard directly into it. oFile.Close closes the file and saves it at the same time.
Dim oClipboard : Set oClipboard = CreateObject("Mercury.Clipboard")
sText = oClipboard.GetText ' gets the current content of the clipboard
Dim oFso : Set oFso = CreateObject("Scripting.FileSystemObject")
Dim oFile : Set oFile = oFso.CreateTextFile("C:\temp\myTextFile.Txt", True)
oFile.Write sText
oFile.Close
I took a stab at it... This was my successful result.
I also noted that this script got really angry when another notepad was already opened.
Const ForReading = 1, ForWriting = 2, ForAppending = 8
Set WShshell = CreateObject("WScript.Shell")
Set fso = CreateObject("Scripting.FileSystemObject")
'Gets user profile variable with CMD
UserProfile = CreateObject("WScript.Shell").Exec("cmd /c echo.%USERPROFILE%").Stdout.ReadAll
'Creates temporary files in appdata\local\temp
'--------------------------------------------------------------------------------------
tmpfilename = fso.GetTempName
tmpfile = Replace(UserProfile & "\AppData\Local\Temp\" & tmpfilename, vbcrlf, "")
Set CreateTempFile = FSO.OpenTextFile(tmpfile, forwriting, true) : CreateTempFile.Close
'Creates a new file for the pasted content
'--------------------------------------------------------------------------------------
MyFile = ThisFolder & "ClipBoard_Extract_" & _
Replace(FormatDateTime(Cdate(now),2), "/", "-") & "_" & _
Hour(now) & Minute(Now) & Second(now) & ".txt"
'Execute's the Main Sub but you could get along without it.
' I usually build my scripts to scale and do logging and other magical things.
'--------------------------------------------------------------------------------------
MainScript
Sub MainScript()
CaptureClipboardText
ExtractTempFile
End Sub
Sub CaptureClipboardText
WShshell.run "c:\WINDOWS\system32\notepad.exe " & tmpfile, 1
WshShell.AppActivate "Notepad"
wscript.sleep 1000
WShshell.SendKeys "^V"
WShshell.SendKeys "^S"
Wshshell.SendKeys "%F"
Wshshell.SendKeys "x"
Wshshell.SendKeys "s"
wscript.sleep 1000
End Sub
Sub ExtractTempFile
Set Extract = fso.OpenTextFile(tmpfile, ForReading)
Set Output = fso.OpenTextFile(MyFile, ForWriting, True) 'True on this syntax means - Create the file if it doesn't exist.
Do Until Extract.AtEndofSTream
line = Extract.Readline
Output.Writeline Line
Loop
Extract.Close : Set Extract = Nothing
fso.DeleteFile tmpfile, true
End Sub
Function ThisFolder
ThisFolder = Left(Wscript.ScriptFullName, Len(Wscript.ScriptFullName) - Len(Wscript.ScriptName))
End Function
Cheers!

Delete shortcut from remote computers with multiple users

Warning I have zero VB knoeledge
So I found this handy script this morning:
InputFile = "C:\MachineList.Txt"
Const DeleteReadOnly = True
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.OpenTextFile(InputFile)
Do While Not (objFile.AtEndOfStream)
strComputer = objFile.ReadLine
On Error Resume Next
Set objFSO = CreateObject("Scripting.FileSystemObject")
objFSO.DeleteFile("\\" & strComputer & "\c$\Documents and Settings\all users\Desktop\Malwarebytes Anti-Malware.LNK")
Err.Clear
Loop
MsgBox "Done"
It did the job great. The problem I am facing is the shortcut is not always under all users or their name lets call it user1
So I would love for it to go through MachineList.txt and browse through all of the profiles searching for Malwarebytes Anti-Malware.LNK. I have seen a few scripts on this but I just cannot wrap my head around VB is a short amount of time. I appreciate any input.
I assume that what you provided results in valid paths... therefore this should work:
InputFile = "C:\MachineList.Txt"
Const DeleteReadOnly = True
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.OpenTextFile(InputFile)
Do While Not (objFile.AtEndOfStream)
strComputer = objFile.ReadLine
For Each objsubfolder In objFSO.GetFolder("\\" & strComputer & "\c$\Documents and Settings\").subfolders
If objFSO.FileExists(objsubfolder.Path & "\desktop\Malwarebytes Anti-Malware.LNK") Then
objFSO.DeleteFile (objsubfolder.Path & "\desktop\Malwarebytes Anti-Malware.LNK")
End If
'To check another file uncomment this
'Add as many of these as you like here
'If objFSO.FileExists(objsubfolder.Path & "\desktop\Otherfile.LNK") Then
' objFSO.DeleteFile (objsubfolder.Path & "\desktop\Otherfile.LNK")
'End If
Next
Loop
MsgBox "Done"

How to output Command prompt to a log file using VBScript

I'm not a programmer so I don't want to overly irritate the fine folk in this forum. My issue is that I would like to use VBScript to Telnet into a Linux device, issue a DF command and output all response to a log file which I can parse later. I originally found a method to successfully Telnet but I have have been experimenting without success regarding the text file output requirement. The following code certainly does not work but I am wondering if I am even close to the correct method?
Dim WshShell, oExec
Set WshShell = CreateObject("WScript.Shell")
Set oExec = WshShell.Exec("cmd /c dir")
WshShell.run"cmd" '*** open command window ***
WScript.Sleep 250
WshShell.SendKeys("{Enter}")
WshShell.SendKeys"telnet 10.13.2.2"
WshShell.SendKeys("{Enter}")
WScript.Sleep 2000
WshShell.SendKeys"root"
WshShell.SendKeys("{Enter}")
WScript.Sleep 1500
WshShell.SendKeys"password"
WshShell.SendKeys("{Enter}")
WScript.Sleep 1500
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objLogFile = objFSO.OpenTextFile("C:\VBSmemSize.txt", 2, True)
WshShell.SendKeys"df /mnt/cf"
WshShell.SendKeys("{Enter}")
Do
strFromProc = oExec.Stdout.Readline()
WScript.Echo strFromProc
Loop While Not objLogFile.StdOut.atEndOfStream
You can capture output from external commands but not at the same time interact with them like you do with sendkeys. Here an example of what works
Function ExecPing(strTarget)
Set objShell = CreateObject("WScript.Shell")
Set objExec = objShell.Exec("ping -n 2 -w 1000 " & strTarget)
strPingResults = LCase(objExec.StdOut.ReadAll)
If InStr(strPingResults, "antwoord van") Then '"reply from" in E
WScript.Echo VbCrLf & strTarget & " responded to ping."
ExecPing = True
Else
WScript.Echo VbCrLf & strTarget & " did not respond to ping."
ExecPing = False
End If
End Function
ExecPing pcname

VBScript to Notepad/Wordpad

I'd like to write output from VBScript to notepad/wordpad in realtime. What's the best way to do this? I'm aware of sendkeys, but it requires that I parse the input for special commands.
SendKeys is the only method for writing to a third-party application in realtime. Why don't you use CScript and write to the standard output instead? That is what it is meant for.
' Force the script to run in the CScript engine
If LCase(Right(WScript.FullName, 11)) <> "cscript.exe" Then
strPath = WScript.ScriptFullName
strCommand = "%comspec% /k cscript " & Chr(34) & strPath & chr(34)
CreateObject("WScript.Shell").Run(strCommand)
WScript.Quit
End If
For i = 1 to 10
For j = 0 to 25
WScript.StdOut.WriteLine String(j, " ") & "."
WScript.Sleep 50
Next
For j = 24 to 1 Step - 1
WScript.StdOut.WriteLine String(j, " ") & "."
WScript.Sleep 50
Next
Next
Try this
Const fsoForWriting = 2
Dim objFSO
Set objFSO = CreateObject("Scripting.FileSystemObject")
'Open the text file
Dim objTextStream
Set objTextStream = objFSO.OpenTextFile("C:\SomeFile.txt", fsoForWriting, True)
'Display the contents of the text file
objTextStream.WriteLine "Hello, World!"
'Close the file and clean up
objTextStream.Close
Set objTextStream = Nothing
Set objFSO = Nothing

Resources