How many quotes to use when calling cmd from vbs? mklink example - vbscript

just a quick question. I am trying to execute the following cmd command from vbs:
mklink /j "%userprofile%\AppData\Roaming\FIEBIG\bin" "%Programfiles(x86)%\fiebig-team\fticclient\bin"
with the following code:
Set objShell = WScript.CreateObject("WScript.Shell")
objShell.Run "cmd.exe mklink /j ""%userprofile%\AppData\Roaming\FIEBIG\bin"" ""Programfiles(x86)%\fiebig-team\fticclient\bin"" "
However, no matter what I've tried with adding or excluding quotes, it still does not work.
Can you please help with this,
Cheers

You don't need the CMD.EXE, do you?
strCommand = "mklink /j"
strCommand = strCommand & " "
strCommand = strCommand & Chr(34) & "%userprofile%\AppData\Roaming\FIEBIG\bin" & Chr(34)
strCommand = strCommand & " "
strCommand = strCommand & Chr(34) & "%Programfiles(x86)%\fiebig-team\fticclient\bin" & Chr(34)
objShell.Run strCommand
BTW, here's a debugging trick I use to make sure I get the proper command line. Use an InputBox() and pass your command string as the 3rd parameter. That allows you to copy and paste it to your command prompt window.
InputBox "", "", strCommand

Thanks a lot for your help. I am really into software packaging and just starting coding. How does that look to you?
Set objShell = WScript.CreateObject ("WScript.Shell")
objShell.run
strCommand = "mklink /j"
strCommand = strCommand & " "
strCommand = strCommand & Chr(34) & "%userprofile%\AppData\Roaming\FIEBIG\bin" & Chr(34)
strCommand = strCommand & " "
strCommand = strCommand & Chr(34) & "%Programfiles(x86)%\fiebig-team\fticclient\bin" & Chr(34)
objShell.Run strCommand

Related

Can't find where "Expected End of Statement is"

So I am creating a code in VBS to automate setting up computers. Step 1 of my code works well, Step 2 not implemented in this example but works good, Step 3 throws me a "Expected end of Statement" error at Command_3A.
The string I am trying to set is start "Lightspeed" /wait /i "Programs\Lightspeed\UserAgentx64 V2.1.14.msi"
I have tried these ways but I am missing something.
Command_3A = "start "Lightspeed" /wait /i "Programs\Lightspeed\UserAgentx64 V2.1.14.msi" "`
Command_3A = "start " ""Lightspeed"" " /wait /i " ""Programs\Lightspeed\UserAgentx64 V2.1.14.msi"" "`
Command_3A = (start "Lightspeed" /wait /i "C:\Users\ccollins\Desktop\ThumbDrive\Programs\Lightspeed\UserAgentx64 V2.1.14.msi" )`
Here is my code:
'Dim objShell
Set objShell = WScript.CreateObject ("WScript.Shell")
Drive_Letter = "C:"
File_Path = "C:\Users\ccollins\Desktop\ThumbDrive\"
' Step 1 - Set Power Settings
Command_1A = "powercfg /change standby-timeout-ac 0"
Command_1B = "powercfg /change standby-timeout-dc 15"
Command_1C = "powercfg /change monitor-timeout-ac 0"
Command_1D = "powercfg /change monitor-timeout-dc 15"
Command_1E = "powercfg /change hibernate-timeout-ac 0"
Command_1F = "powercfg /change hibernate-timeout-dc 15"
objShell.Run "cmd /k " & Command_1A & "&" & Command_1B & "&" & Command_1C & "&" & Command_1D & "&" & Command_1E & "&" & Command_1F & "& exit"
' Step 2 - Remove Bloatware (Win10Apps)
' Step 3 - Install wanted programs
Command_3A = (start "Lightspeed" /wait /i "C:\Users\ccollins\Desktop\ThumbDrive\Programs\Lightspeed\UserAgentx64 V2.1.14.msi" )
Command_3B = "start "Acrobat" /wait "Programs\AcroRdrDC1801120058_en_US.exe" /sAll "
Command_3C = "start "AZMerit" /wait /I "Programs\AzMERITSecureBrowser10.4-2018-08-02.msi" /passive "
Command_3D = "start "Java" /wait "Programs\jre-8u201-windows-x64.exe" /s "
Command_3E = "start "Chrome" /wait "Programs\ChromeStandaloneSetup64.exe" /silent /install "
Command_3F = "start "Eset" /wait "Programs\ESet Rip and Replace.exe" "
objShell.Run "cmd /k " & Drive_Letter & "&" & Command_3A & "&" & Command_3B & "&" & Command_3C & "&" & Command_3D & "&" & Command_3E & "&" & Command_3F & "& exit"
Set oShell = Nothing'
I am definitely missing something and just need another set of eyes to look at my code.
So this is what I came up with. I had to use Chr(34) for the " in the code that wanted inserted as part of the command. I also inserted an input box for sPath and UAC control.
Set wshShell = WScript.CreateObject("WScript.Shell")
dim sPath
' ----------------------------------------------------------'
' UAC Control
If WScript.Arguments.Length = 0 Then
Set objShell = CreateObject("Shell.Application")
objShell.ShellExecute "wscript.exe" _
, Chr(34) & WScript.ScriptFullName & Chr(34) & " RunAsAdministrator", , "runas", 1
WScript.Quit
End If
'Step 0 - Set Drive Letter or File Path
sPath = InputBox("Enter the Path to the Drive. If entering just drive letter then add :, if file path remove the end \")
' Step 1 - Set Power Settings
' Step 2 - Remove Bloatware (Win10Apps)
' Step 3 - Install wanted programs
Function GetOsBits()
Set shell = CreateObject("WScript.Shell")
If shell.ExpandEnvironmentStrings("%PROCESSOR_ARCHITECTURE%") = "AMD64" Then
GetOsBits = 64
Else
GetOsBits = 32
End If
End Function
Command_3A = Chr(34) & sPath & "\Programs\Lightspeed\UserAgentx64 V2.1.14.msi" & Chr(34)
Command_3B = Chr(34) & sPath & "\Programs\Lightspeed\UserAgentx86 V2.1.14.msi" & Chr(34)
Command_3C = Chr(34) & sPath & "\Programs\AcroRdrDC1801120058_en_US.exe" & Chr(34) & "& /sAll "
Command_3D = Chr(34) & sPath & "\Programs\Java\jre-8u201-windows-x64.exe" & Chr(34) & "& /s "
Command_3E = Chr(34) & sPath & "\Programs\Java\jre-8u201-windows-i586.exe" & Chr(34) & "& /s "
Command_3F = Chr(34) & sPath & "\Programs\Chrome\ChromeStandaloneSetup64.exe" & Chr(34) & "& /silent /install "
Command_3G = Chr(34) & sPath & "\Programs\Chrome\ChromeStandaloneSetupi586.exe" & Chr(34) & "& /silent /install "
Command_3H = Chr(34) & sPath & "\Programs\ESet Rip and Replace.exe" & Chr(34)
If GetOsBits = 64 Then
wshShell.Run(Command_3A) 'LightSpeed
wscript.Sleep 4000
wshShell.Run(Command_3C) 'Adobe Reader
wscript.Sleep 30000
wshShell.Run(Command_3D) 'Java
wscript.Sleep 30000
wshShell.Run(Command_3F) 'Chrome
wscript.Sleep 30000
wshShell.Run(Command_3H) 'Eset
wscript.Sleep 30000
Else
wshShell.Run(Command_3B) 'LightSpeed x86
wscript.Sleep 4000
wshShell.Run(Command_3C) 'Adobe Reader
wscript.Sleep 4000
wshShell.Run(Command_3E) 'Java x86
wscript.Sleep 30000
wshShell.Run(Command_3G) 'Chrome x86
wscript.Sleep 30000
wshShell.Run(Command_3H) 'Eset
wscript.Sleep 30000
End If
Set oShell = Nothing

Specified File Can't be Found

I have been creating this to do auto-attend of setting up of computers with minimum attention needed. Been working on this and made some breakthroughs.
I want to be able to run this as admin from the start of the script. The problem is is down at the wshShell.Run(Command_3A) 'Lightspeed. The program runs great to that point but then gives me a Specified file can't be found.
The entire thing worked fine until I inserted the Runas admin command. So I am figuring it is somewhere in there. Here is the portion to Runas admin.
Set wshShell = WScript.CreateObject("WScript.Shell")
If WScript.Arguments.Length = 0 Then
Set objShell = CreateObject("Shell.Application")
objShell.ShellExecute "wscript.exe" _
, Chr(34) & WScript.ScriptFullName & Chr(34) & " RunAsAdministrator", , "runas", 1
WScript.Quit
End If
Here is all the code.
Set wshShell = WScript.CreateObject("WScript.Shell")
If WScript.Arguments.Length = 0 Then
Set objShell = CreateObject("Shell.Application")
objShell.ShellExecute "wscript.exe" _
, Chr(34) & WScript.ScriptFullName & Chr(34) & " RunAsAdministrator", , "runas", 1
WScript.Quit
End If
spath = "C:\ThumbDrive"
' Step 1 - Set Power Settings
Command_1A = "powercfg /change standby-timeout-ac 0"
Command_1B = "powercfg /change standby-timeout-dc 15"
Command_1C = "powercfg /change monitor-timeout-ac 0"
Command_1D = "powercfg /change monitor-timeout-dc 15"
Command_1E = "powercfg /change hibernate-timeout-ac 0"
Command_1F = "powercfg /change hibernate-timeout-dc 15"
wshShell.Run "cmd /k " & Command_1A & "&" & Command_1B & "&" & Command_1C & "&" & Command_1D & "&" & Command_1E & "&" & Command_1F & "& exit"
WScript.Sleep 3000
' Step 2 - Remove Bloatware (Win10Apps)
' Step 3 - Install wanted programs
Command_3A = Chr(34) & spath & "\Programs\Lightspeed\UserAgentx64 V2.1.14.msi" & Chr(34)
Command_3B = Chr(34) & spath & "\Programs\AcroRdrDC1801120058_en_US.exe" & Chr(34) & "& /sAll "
Command_3C = Chr(34) & spath & "\Programs\AzMERITSecureBrowser10.4-2018-08-02.msi" & Chr(34) & "& /passive "
Command_3D = Chr(34) & spath & "\Programs\jre-8u201-windows-x64.exe" & Chr(34) & "& /s "
Command_3E = Chr(34) & spath & "\Programs\ChromeStandaloneSetup64.exe" & Chr(34) & "& /silent /install "
Command_3F = Chr(34) & spath & "\Programs\ESet Rip and Replace.exe" & Chr(34)
wshShell.Run(Command_3A) 'LightSpeed
WScript.Sleep 4000
wshShell.Run(Command_3B) 'Adobe Reader
WScript.Sleep 30000
wshShell.Run(Command_3C) 'AzMerit
WScript.Sleep 4000
wshShell.Run(Command_3D) 'Java
WScript.Sleep 30000
wshShell.Run(Command_3E) 'Chrome
WScript.Sleep 30000
wshShell.Run(Command_3F) 'Eset
So I know the UAC works and all the code works without the UAC Control. Can anyone help me figure out why the UAC control breaks everything.
The CurrentDirectory property returns a string that contains the fully qualified path of the current working directory of the active process.
It points to C:\WINDOWS\system32 for an elevated script. You can either
supply the current working directory from unelevated process to the elevated one, or
elicit it from the Wscript.ScriptFullName property.
Both methods are illustrated in the following code snippet:
Set wshShell = WScript.CreateObject("WScript.Shell")
spath = wshShell.CurrentDirectory
If WScript.Arguments.Length = 0 Then
Set objShell = CreateObject("Shell.Application")
objShell.ShellExecute "wscript.exe" _
, Chr(34) & WScript.ScriptFullName & Chr(34) _
& " RunAsAdministrator " & Chr(34) & spath & Chr(34), , "runas", 1
WScript.Quit
End If
'' take out the supplied value:
spath = WScript.Arguments(1)
'' find out where the script resides:
shtap = Left( WScript.ScriptFullName, _
Len( WScript.ScriptFullName) - Len( WScript.ScriptName) -1 )
'' debugging output
Wscript.Echo CStr( WScript.Arguments.Length) & _
vbNewLine & Chr(34) & WScript.Arguments(0) & Chr(34) & _
vbNewLine & Chr(34) & spath & Chr(34) & _
vbNewLine & Chr(34) & shtap & Chr(34) & _
vbNewLine & Chr(34) & wshShell.CurrentDirectory & Chr(34)
' Step 1 - Set Power Settings
Output (using cscript.exe Windows Script Host):
2
"RunAsAdministrator"
"D:\bat\SO"
"D:\bat\SO"
"C:\WINDOWS\system32"
I had to create a input box to specify what the sPath was, and left the UAC reading from C:\WINDOWS\system32. It works wonderfully, just wish I could get it to run without having the input box.

If - Else If statement in VBScript

Else If exist "K:\ICT project"
(Set WshShell = CreateObject("WScript.Shell")
WshShell.Run Chr(34) & "K:\ICT project" & Chr(34), 0
Set WshShell = Nothing)
Else If exist "F:\ICT project"
(Set WshShell = CreateObject("WScript.Shell")
WshShell.Run Chr(34) & "F:\ICT project" & Chr(34), 0
Set WshShell = Nothing)
Else If exist "E:\ICT project"
(Set WshShell = CreateObject("WScript.Shell")
WshShell.Run Chr(34) & "E:\ICT project" & Chr(34), 0
Set WshShell = Nothing)
Else If exist "D:\ICT project"
(Set WshShell = CreateObject("WScript.Shell")
WshShell.Run Chr(34) & "D:\ICT project" & Chr(34), 0
Set WshShell = Nothing)
Else If exist "C:\ICT project"
(Set WshShell = CreateObject("WScript.Shell")
WshShell.Run Chr(34) & "C:\ICT project" & Chr(34), 0
Set WshShell = Nothing)
What is wrong with the code? And how well the If statement is used? please help me.
How can this code be improved?
To search for a program on different drives and call it (perhaps with arguments and decent quotes), use a loop over the drives:
Option Explicit
Const csPrg = "pipapo"
Function qq(s) : qq = """" & s & """" : End Function
Dim goFS : Set goFS = CreateObject("Scripting.FileSystemObject")
Dim sDL, sFSpec, sCmd
For Each sDL In Split("A:\ C:\ K:\ E:\")
sFSpec = goFS.BuildPath(sDL, csPrg)
sCmd = Join(Array("%comspec% /c", qq(sFSpec), "/p:arm", qq("one two three"), 4711))
WScript.Echo sCmd
If goFS.FileExists(sFSpec) Then Wscript.Echo " ==> now run sCmd (and perhaps Exit For)"
Next
output:
cscript 47922850.vbs
%comspec% /c "A:\pipapo" /p:arm "one two three" 4711
%comspec% /c "C:\pipapo" /p:arm "one two three" 4711
%comspec% /c "K:\pipapo" /p:arm "one two three" 4711
%comspec% /c "E:\pipapo" /p:arm "one two three" 4711
==> now run sCmd (and perhaps Exit For)

how to get permission to replace the utilman.exe

I would like to get the permission to replace or rename the utilman.exe. The script below is not working.
Dim strFolder, strUser, strDomain
strFolder = "c:\windows\system32\utilman.exe"
Set FSO = CreateObject("Scripting.FileSystemObject")
Set ObjShell = Wscript.CreateObject("Wscript.Shell")
objShell.Run("takeown /A /f" & Chr(34) & strFolder & chr(34)),1,True
objShell.Run("icacls chr(34) & strFodler & chr(34) /grant administrator:F"),1,True
Wscript.quit
You defined the icacls commandline as a single string when you apparently want it to contain the value of the variable strFolder in double quotes. You must use string concatenation for this to work in VBScript. Also, you may want to put a space after the parameter /f of takeown and remove the parentheses around the commands.
Change this:
objShell.Run("takeown /A /f" & Chr(34) & strFolder & chr(34)),1,True
objShell.Run("icacls chr(34) & strFodler & chr(34) /grant administrator:F"),1,True
into this:
objShell.Run "takeown /A /f " & Chr(34) & strFolder & chr(34), 1, True
objShell.Run "icacls " & chr(34) & strFolder & chr(34) & " /grant administrator:F", 1, True

synchronize run methods vbscript

I have a problem. I need to throw some synchronized commands in vbscript (one Run method, Delete one and moveFile one) in every cycle to my loop. I don't know how I do it.
My code is that:
Public Function UnificarCRIs(ByVal path, ByVal FICRIEC, ByVal sessio, ByVal CIBAA)
Dim objFile, objCurrentFolder, filesys, origenFitxers
Dim FileName, WshShell
On error resume next
Set filesys = CreateObject("Scripting.FileSystemObject")
Set WshShell = WScript.CreateObject("WScript.Shell")
Set objCurrentFolder = filesys.getFolder(path)
For Each objFile In objCurrentFolder.Files
FileName = objFile
If (right(FileName, 4) = ".cri") Then
If filesys.FileExists(path & FICRIEC & sessio) Then
'WshShell.Run ("copy " & path & FICRIEC & sessio & "+" & FileName & " " & path & FICRIEC & sessio & "_tmp")
WshShell.run ("cmd /K " & "copy /Y " & path & FICRIEC & sessio & "+" & FileName & " " & path & FICRIEC & sessio & "_tmp",8,TRUE)
Set WshShell = Nothing
filesys.DeleteFile path & FICRIEC & sessio
'filesys.MoveFile path & FICRIEC & sessio & "_tmp", path & FICRIEC & sessio
Else
WshShell.run "cmd /K " & "copy /Y " & FileName & " " & path & FICRIEC & sessio,8,TRUE
Set WshShell = Nothing
End If
End If
Next
End Function
Read the docs for .Run completely and reflect on the bWaitOnReturn parameter.
Apology:
Sorry, I didn't read your code carefully enough. The .Run should wait. But I see that you use a global On Error Resume Next and param list () in
WshShell.run ("cmd /K " & "copy /Y " & path & FICRIEC & sessio & "+" & FileName & " " & path & FICRIEC & sessio & "_tmp",8,TRUE)
What happens, when you remove those mistakes? Are you sure, that you have a usable WshShell in the second pass thru the loop?

Resources