installing application with vbscript - powershell

I would like to convert this cmd command to vb script or powershell
c:\windows\system32\certutil.exe -f -addstore "TrustedPublisher" "Mycert.cer"
My problem is creating the space between "TrustedPublisher" "Mycert.cer"
Thanks

In vbscript strings are enclosed in quotes. To put quotes in a string use "" for each quote.
"""c:\windows\system32\wordpad"" ""c:\windows\win.ini"""
means the string contains
"c:\windows\system32\wordpad" "c:\windows\win.ini"

Use
a function to handle quoting
an array to store the components of the command
Join() to deal with spaces/separators
to build your commandline in a structured way. In code:
Option Explicit
Function qq(s) : qq = """" & s & """" : End Function
Dim aParts : aParts = Array( _
qq("c:\windows\system32\certutil.exe") _
, "-f" _
, "-addstore" _
, qq("TrustedPublisher") _
, qq("Mycert.cer") _
)
Dim sCmd : sCmd = Join(aParts)
WScript.Echo sCmd
output:
cscript 29649158.vbs
"c:\windows\system32\certutil.exe" -f -addstore "TrustedPublisher" "Mycert.cer"

You can try also like this code :
Option Explicit
Dim MyCmd,Ws
Set Ws = CreateObject("Wscript.Shell")
MyCmd = "c:\windows\system32\certutil.exe -f -addstore "& DblQuote("TrustedPublisher") &" "& DblQuote("Mycert.cer") &""
MsgBox MyCmd
ws.run MyCmd
'**************************************************************************
'Adding Double quotes into variable
Function DblQuote(Str)
DblQuote = Chr(34) & Str & Chr(34)
End Function
'**************************************************************************

Related

Calling PowerShell from VBScript - Missing Double Quote

I have a VBScript that calls powershell.exe using the Shell object's Exec method.
For the purpose of the question, I have extracted the troublesome bit of code from my script and simplified it.
The expected outcome of the script below is that PowerShell's Write-Host cmdlet will print the following:
Please save the file to "C:\test"
But the actual result is this:
Please save the file to C:\test"
The initial double quote surrounding the file path is missing.
Option Explicit
Dim strPsCommand, ps, objShell, objExec
Dim strStdOut, strStdErr
Dim strStringToPrint
strStringToPrint = "$text = #'" & vbCrLf & "Please save the file to ""C:\test\""" & vbCrLf & "'#"
strPsCommand = strStringToPrint & "; Write-Host $text"
ps = "powershell.exe -Command " & strPsCommand
Set objShell = CreateObject("WScript.Shell")
Set objExec = objShell.Exec(ps)
' Close standard input before reading standard output.
objExec.StdIn.Close()
strStdOut = objExec.StdOut.ReadAll()
strStdErr = objExec.StdErr.ReadAll()
WScript.Echo strStdOut
WScript.Echo strStdErr
The code above uses a here-string (which is best suited for my script). However I have tried substituting this for a regular string but the result is the same. To do this, I changed the value of strStringToPrint as follows:
strStringToPrint = "$text = 'Please save the file to " & """" & "C:\test\" & """" & "'"
You need to escape the double quotes around the path twice:
for VBScript
for the commandline (the Exec call)
The former is done by doubling the double quotes. For the latter you need backslashes. You already have a backslash before the double quotes after the path (although probably unintentional), but not before the double quotes before the path.
Change this:
strStringToPrint = "$text = #'" & vbCrLf & "Please save the file to ""C:\test\""" & vbCrLf & "'#"
into this:
strStringToPrint = "$text = #'" & vbCrLf & "Please save the file to \""C:\test\""" & vbCrLf & "'#"
or this (if you want the trailing backslash in the path):
strStringToPrint = "$text = #'" & vbCrLf & "Please save the file to \""C:\test\\\""" & vbCrLf & "'#"

vbscript to run command

I'm trying to create a script that runs the command: RunDll32.exe InetCpl.cpl,ClearMyTracksByProcess 255
Right now this errors out
Dim objShell
Set objShell = Wscript.CreateObject("WScript.Shell")
sEXE = """C:\Windows\system32\RunDll32.exe InetCpl.cpl,ClearMyTracksByProcess 255 """
with CreateObject("WScript.Shell")
.Run sEXE & " " , 1, true
end with
Wscript.Quit
Any suggestions? Could I make the script so I could run a bunch of commands and wait until each process executed has finished before executing the other command?
The command line should appear exactly as it would if you typed it at the command prompt (verify by Wscript.Echo sEXE):
sEXE = """C:\Windows\system32\RunDll32.exe"" InetCpl.cpl,ClearMyTracksByProcess 255"

VBS Objshell.run not executing "%COMSPEC% /c ping " & MyObj & " >>" & ReportFileNAme

The code is supposed to execute some ping and tracert commands to a file, then email the file. Instead it creates a BLANK file.
I tried redirecting from Objshell.EXEC but the pop-ups windows that pop up are annoying and steal focus; and I want this to run periodically in the background using Task Scheduler.
The generated syntax looks like this (And works when pasted to command line):
%COMSPEC% /c ping speedtest.advance2000.com >>c:\temp\testforteresa2-foo#bar-2014-01-08__10-01.txt
The resultant command string works when pasted into a CMD> window but the tests in excel and in the executed VBS it yields a blank file...
Wouldn't mind having a wait state to check for the email to be sent so it could delete the txt file. Will figure that out later :)
'On Error Resume Next
Const ForReading = 1
Const ForAppending = 8
'PingSpeedTest
Sub PingSpeedTest()
Dim GetUserLoginID ''As String
Set objfso = CreateObject("Scripting.FileSystemObject")
Dim WSHNetwork
Set WSHNetwork = CreateObject("WScript.Network")
GetUserLoginID = CStr(WSHNetwork.UserName)
getuserdomain = CStr(WSHNetwork.UserDomain)
'''''''''''REPORT NAME''''''''''''''''''''''''''''''
ReportFileNAme = "c:\temp\testforteresa2-" & GetUserLoginID & "#" & getuserdomain & "-" & _
DatePart("yyyy", Now) & "-" & _
Right("0" & DatePart("m", Now), 2) & "-" & _
Right("0" & DatePart("d", Now), 2) & "__" & _
Right("0" & DatePart("h", Now), 2) & "-" & _
Right("0" & DatePart("m", Now), 2) & ".txt"
On Error Resume Next
objfso.DeleteFile (ReportFileNAme)
On Error GoTo 0
Set reportfile = objfso.OpenTextFile(ReportFileNAme, ForAppending, True)
Set objShell = CreateObject("WScript.Shell")
Set List = CreateObject("System.Collections.ArrayList")
List.Add "speedtest.advance2000.com"
List.Add "myphone.advance2000.com"
List.Add "vdesk.advance2000.com"
'''
For Each MyObj In List
MyCmd = "%COMSPEC% /c ping " & MyObj & " >>" & ReportFileNAme '''<<< Should work- creates correct syntax but no output
objShell.Run MyCmd, 3, True
MyCmd = "%COMSPEC% /c tracert " & MyObj & " >>" & ReportFileNAme
objShell.Run MyCmd, 3, True
Next ''MyObj
Dim olLook ''As Object 'Start MS Outlook
Dim olNewEmail ''As MailItem ' Object 'New email in Outlook
Dim strContactEmail ''As String 'Contact email address
Set olLook = CreateObject("Outlook.Application")
Set olNewEmail = olLook.createitem(0)
strEmailSubject = "TopSellers.accdb Application"
strEmailText = "PING AND TRACEROUTE RESULTS"
'strContactEmail = GetUserLoginID & "#" & getuserdomain & ".com"
With olNewEmail 'Attach template
.To = "Foo#BAR.com" 'strContactEmail<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
'.CC = strCc
.body = strEmailText
.Subject = "RE:PING AND TRACERT RESULTS"
.display
.Attachments.Add (ReportFileNAme)
End With
'objfso.DeleteFile (ReportFileNAme)
End Sub
Your
Set reportfile = objfso.OpenTextFile(ReportFileNAme, ForAppending, True)
opens the file named ReportFileNAme. The .Run
"%COMSPEC% /c ping " & MyObj & " >>" & ReportFileNAme
then asks the OS to write to that open file. Try to skip the creation of reportfile.

Vbscript Wscript.Run method stuck because of space character

Error System can't find the file specified
strCline = Document.getElementById("head").innerHtml
msgbox strCline
strCline = replace(strCline, " ",Chr(32))
oShell.run strCline
Set oShell = Nothing
Above code produces error because it can't read file name properly. It's all because of space characters in file name. After reading, i found chr(32) would replace space character but it won't. How do I make it take space character.
Edit:
My final code looked like this which worked. I made mistake while creating object.
Sub funEdit
set oShell=createobject("Wscript.shell")
strCline = Document.getElementById("head").innerHtml
msgbox strCline
strCline = replace(strCline, " ",Chr(32))
oShell.run strCline
Set oShell = Nothing
End Sub
The shell splits a command line into parameters using blank(s) for a delimiter. If you want to send text file specifications to .Run to display them automagically in the default editor, you must double quote the (logically) single parameter. This demo code:
Option Explicit
Dim sFSpec : sFSpec = "C:\Documents and Settings\eh\tmp.txt"
Dim sCmd : sCmd = sFSpec
Dim oWSH : Set oWSH = CreateObject("WScript.Shell")
On Error Resume Next
oWSH.Run sCmd
WScript.Echo qq(sCmd), "=>", Err.Number, Err.Description
Err.Clear
sCmd = qq(sFSpec)
oWSH.Run sCmd
WScript.Echo qq(sCmd), "=>", Err.Number, Err.Description
On Error GoTo 0
Function qq(s)
qq = """" & s & """"
End Function
will output:
"C:\Documents and Settings\eh\tmp.txt" => -2147024894
""C:\Documents and Settings\eh\tmp.txt"" => 0
and open only one Notepad.
See here for some context.

Vbs script to check date and extension

Hi I cant get the below script ive worked on to pickup the extension of the files, Can any help me out by pointing where I have gone wrong?
dim fileSystem, folder, file
dim path
dim count : count = 0
path = "C:\temp"
Set fileSystem = CreateObject("Scripting.FileSystemObject")
Set folder = fileSystem.GetFolder(path)
for each file in folder.Files
if file.DateLastModified > dateadd("h", -24, Now) & File.name = "txt" then
count = count + 1
end if
Next
if count < 4 then
Set WshShell = WScript.CreateObject("WScript.Shell")
strcommand = "eventcreate /T ERROR /ID 666 /L Application /SO BESROffsite /D " & _
Chr(34) & count & " Files found please check offsite copy " & Chr(34)
WshShell.Run strcommand
wScript.Quit ( 1001 )
Else
Set WshShell = WScript.CreateObject("WScript.Shell")
strcommand = "eventcreate /T Information /ID 666 /L Application /SO BESROffsite /D " & _
Chr(34) & count & " Files found offsite is working fine " & Chr(34)
WshShell.Run strcommand
wScript.Quit ( 0 )
End if
File.name is the full name including the extension, to test for the extension;
if ... fileSystem.getExtensionName(file.name) = "txt" then
You also want the logical And not the bitwise concatenating & in their too.
Alex's answer is the one you want, but just for reference if you were working just with vbs and a string filename, without the filesystemobject collection you could achieve the same via:
Right(strFilename, Len(strFilename) - Instrrev(strFilename, "."))
This essentially finds the position of the final "." in the filename, takes this away from the length of your filename, and then gives you however many character's that equals from the right hand side. This could be amended slightly to use the "Mid" command rather than the "Right" but I don't think it matters too much in a case like this.