vbsScript remove space echo - vbscript

Hello im making unhide usb files i modify konboot usb installer to usb unhide
CMD
cscript //Nologo USB.vbs>unhide.bat
Wscript.Echo "attrib -s -h -r /s /d "& SingleLogicalDisk.DeviceID,"\*.*"
the output
attrib -s -h -r /s /d F:(have space here)*.*
i need no space on F:(Here)*.*
my final output i want
attrib -s -h -r /s /d F:*.*
vbscript
Dim query
Dim WMBIObj
Dim AllDiskDrives
Dim SingleDiskDrive
Dim AllLogicalDisks
Dim SingleLogicalDisk
Dim AllPartitions
Dim Partition
Dim result
Dim textmsg
Dim wshShell
Dim FileObj
Dim Counter
set FileObj = CreateObject("Scripting.FileSystemObject")
set wshShell = wscript.createObject("wscript.shell")
Set WMBIObj = GetObject("winmgmts:\\.\root\cimv2")
Set AllDiskDrives = WMBIObj.ExecQuery("SELECT * FROM Win32_DiskDrive where InterfaceType='USB'") '
For Each SingleDiskDrive In AllDiskDrives
counter = counter + 1
query = "ASSOCIATORS OF {Win32_DiskDrive.DeviceID='" + SingleDiskDrive.DeviceID + "'} WHERE AssocClass = Win32_DiskDriveToDiskPartition"
Set AllPartitions = WMBIObj.ExecQuery(query)
For Each Partition In AllPartitions
query = "ASSOCIATORS OF {Win32_DiskPartition.DeviceID='" + Partition.DeviceID + "'} WHERE AssocClass = Win32_LogicalDiskToPartition"
Set AllLogicalDisks = WMBIObj.ExecQuery (query)
For Each SingleLogicalDisk In AllLogicalDisks
textmsg = "============================================" & VbCr & _
"DeviceID: " & SingleDiskDrive.DeviceID & VbCr & _
"Logical Drive: " & SingleLogicalDisk.DeviceID & VbCr & _
"Model: " & SingleDiskDrive.Model & VbCr & _
"Manufacturer: " & SingleDiskDrive.Manufacturer & VbCr & _
"============================================" & VbCr & _
"Would you like to use this drive as destination?" & VbCr & _
"Warning, disk data may be overwritten"
result = MsgBox(textmsg, vbQuestion + vbOKCancel, "USB")
if result = vbOk Then
WScript.Echo "echo DeviceID: " & SingleDiskDrive.DeviceID
WScript.Echo "echo Logical Drive: " & SingleLogicalDisk.DeviceID
WScript.Echo "echo Model: " & SingleDiskDrive.Model
WScript.Echo "echo Manufacturer: " & SingleDiskDrive.Manufacturer
Wscript.Echo "attrib -s -h -r /s /d "& SingleLogicalDisk.DeviceID,"\*.*"
wshShell.Run "unhide.bat"
WScript.quit
MsgBox "Your USB is ready!", vbInformation + vbOKOnly, "USB"
WScript.quit
End If
Next
Next
Next
if counter = 0 Then
MsgBox "No USB disks detected or unknown error!", vbCritical + vbOkOnly, "USB"
End If

In this line:
Wscript.Echo "attrib -s -h -r /s /d "& SingleLogicalDisk.DeviceID,"\*.*"
You need to concatenate the strings "attrib -s -h -r /s /d ", SingleLogicalDisk.DeviceID and "\*.*"
You are using , instead of & to concatenate the second and third strings. It should actually be:
Wscript.Echo "attrib -s -h -r /s /d "& SingleLogicalDisk.DeviceID & "\*.*"
When you use , instead of &, the third string is considered as another argument to the echo method. So, it adds a space between the arguments in the output.
From: Echo Method
Each displayed item is separated with a space character.

Related

Pausing vbs code until shell script finishes

I'm trying to create a script that will auto install a printer.
I found some code that I modified to give the user some prompts. I think the original was a batch file. There are 4 processes that have to shell out to run a cscript command. These are the ones that I need to have paused until they are done. I thought the WaitOnReturn option in the shell command would have made them wait but it doesn't I marked them in the code with 'Need to wait here...
Here is the code.
Dim fso
Dim Folder
Dim ProgramPath
Dim WshShell
Dim ProgramArgs
Dim WaitOnReturn
Dim intWindowStyle
'Dim objShell
'strInput = UserInput( "Enter some input:" )
strInput = MsgBox("This will install the default HP Print driver?",1,"Windows 7 Print Driver Install")
'WScript.Echo "You entered: " & strIpAddress
If strInput = 2 Then
WScript.Echo "Please run again when you are ready"
Else '=1 Prompt for IP Address
'WScript.Echo "You entered: " & strInput
strIpCheck = MsgBox("Do you have the Printers IP Address?",1,"Choose options")
If strIpCheck = 2 Then 'Does not have IP Address
WScript.Echo "Please run again when have the IP Address"
Else 'Start install routine
strIpAddress = InputBox("Enter the IP Address", "IP Address")
Set WshShell = CreateObject("WScript.Shell")
'Create directories
Set FSO = CreateObject("Scripting.FileSystemObject")
If NOT (fso.FolderExists("C:\DRIVERS")) Then
fso.CreateFolder("C:\DRIVERS")
End If
If NOT (fso.FolderExists("C:\SCRIPTS")) Then
fso.CreateFolder("C:\SCRIPTS")
End If
'Location of Windows 7 HP print drivers
strSourceDriver = "C:\Windows\System32\DriverStore\FileRepository\hpoa1so.inf_amd64_neutral_4f1a3f1015001339"
'Location of Win7 built in printer scripts
strSourceScripts = "C:\Windows\System32\Printing_Admin_Scripts\en-US"
If (fso.FolderExists(strSourceDriver)) Then
fso.copyFolder strSourceDriver, "C:\DRIVERS"
End If
If (fso.FolderExists(strSourceScripts)) Then
fso.copyFolder strSourceScripts, "C:\SCRIPTS"
End If
'Delete existing printer named HP Printer
ProgramPath = "C:\SCRIPTS\prnmngr.vbs"
ProgramArgs = "-d -p " & Chr(34) & "HP Printer" & Chr(34) & ""
intWindowStyle = 1
WaitOnReturn = true
WshShell.Run "cscript.exe " & Chr(34) & ProgramPath & Chr(34) & Space(1) & ProgramArgs, intWindowStyle, WaitOnReturn
'Need to wait here until the above shell process is done
ProgramPath = "C:\SCRIPTS\Prnport.vbs"
ProgramArgs = "-a -r " & strIpAddress & "Port -h " & strIpAddress & " -o raw -n 9100"
intWindowStyle = 1
WaitOnReturn = true
WshShell.Run "cscript.exe " & Chr(34) & ProgramPath & Chr(34) & Space(1) & ProgramArgs, intWindowStyle, WaitOnReturn
'Need to wait here until the above shell process is done
ProgramPath = "C:\SCRIPTS\Prndrvr.vbs"
ProgramArgs = "-a -m " & Chr(34) & "HP Photosmart C8100" & Chr(34) & "-i C:\DRIVERS\hpoa1so.inf -h C:\DRIVERS"
intWindowStyle = 1
WaitOnReturn = true
WshShell.Run "cscript.exe " & Chr(34) & ProgramPath & Chr(34) & Space(1) & ProgramArgs, intWindowStyle, WaitOnReturn
'Need to wait here until the above shell process is done
ProgramPath = "C:\SCRIPTS\Prnmngr.vbs"
ProgramArgs = "-a -p " & Chr(34) & "HP Printer" & Chr(34) & "-m" & Chr(34) & "HP Photosmart C8100" & Chr(34) & "-r " & strIpAddress
intWindowStyle = 1
WaitOnReturn = true
WshShell.Run "cscript.exe " & Chr(34) & ProgramPath & Chr(34) & Space(1) & ProgramArgs, intWindowStyle, WaitOnReturn
'Need to wait here until the above shell process is done
End If
End If
All WshShell.Run "cscript.exe " & …, intWindowStyle, WaitOnReturn should wait until the called shell process is done providing WaitOnReturn = true. However, output from the following simplified script (where assignments of ProgramArgs are Copied&Pasted from the original code) shows some missing spaces in supplied parameters:
strIpAddress = "10.10.10.10"
ProgramArgs = "-d -p " & Chr(34) & "HP Printer" & Chr(34) & ""
Wscript.Echo ProgramArgs
ProgramArgs = "-a -r " & strIpAddress & "Port -h " & strIpAddress & " -o raw -n 9100"
Wscript.Echo ProgramArgs
ProgramArgs = "-a -m " & Chr(34) & "HP Photosmart C8100" & Chr(34) & "-i C:\DRIVERS\hpoa1so.inf -h C:\DRIVERS"
Wscript.Echo ProgramArgs
ProgramArgs = "-a -p " & Chr(34) & "HP Printer" & Chr(34) & "-m" & Chr(34) & "HP Photosmart C8100" & Chr(34) & "-r " & strIpAddress
Wscript.Echo ProgramArgs
Output: cscript D:\bat\SO\55303301.vbs
-d -p "HP Printer"
-a -r 10.10.10.10Port -h 10.10.10.10 -o raw -n 9100
-a -m "HP Photosmart C8100"-i C:\DRIVERS\hpoa1so.inf -h C:\DRIVERS
-a -p "HP Printer"-m"HP Photosmart C8100"-r 10.10.10.10
Moreover, the Run method returns an integer. You could grab a process return code to a variable RetCode and then check if its value is zero (when all went OK) using
RetCode = WshShell.Run ( "cscript.exe " & _
Chr(34) & ProgramPath & Chr(34) & Space(1) & ProgramArgs _
, intWindowStyle, WaitOnReturn )

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

shell script not workig with parameters

This works with CMD:
rar a c:\new.rar c:\*.*
This works running script.vbs, but does not do too much!:
Function qq(strIn)
qq = Chr(34) & strIn & Chr(34)
End Function
Set sh = CreateObject("WScript.Shell")
MyCmd = "Cmd /c CD /D " & qq("C:\Program Files (x86)\WinRAR") & " & a c:\new.rar c:\*.* "
sh.run MyCmd,1,True
It is no longer returning errors, but file new.rar is not being created either, it says 'a' is not a valid parameter
(1) Always echo the command before you try to feed it to run:
>> MyCmd = "Cmd /c CD /D " & qq("C:\Program Files (x86)\WinRAR") & " & a c:\new.rar c:\*.* "
>> WScript.Echo MyCmd
>>
Cmd /c CD /D "C:\Program Files (x86)\WinRAR" & a c:\new.rar c:\*.*
Obviously your concatenation puts a spurious "&" in the result.
(2) Never let you be persuaded to put random additions like "cmd /c cd /d" into you command.
(3) Build your command line in a structured way. E.g:
Option Explicit
Function qq(s) : qq = """" & s & """" : End Function
Dim sCmd : sCmd = Join(Array( _
qq("C:\Program Files (x86)\WinRAR") _
, "a" _
, qq("c:\some path with spaces\new.rar") _
, qq("c:\source dir with more spaces\*.*") _
))
WScript.Echo sCmd
output:
cscript y.vbs
"C:\Program Files (x86)\WinRAR" a "c:\some path with spaces\new.rar" "c:\source dir with more spaces\*.*"
See here for background and reasons why.
Update:
This way, blunders (just the path, not the full filespec of rar mentioned) can be spotted and corrected easily:
Option Explicit
Function qq(s) : qq = """" & s & """" : End Function
Dim sCmd : sCmd = Join(Array( _
qq("C:\Program Files (x86)\WinRAR\rar.exe") _
, "a" _
, qq("c:\some path with spaces\new.rar") _
, qq("c:\source dir with more spaces\*.*") _
))
WScript.Echo sCmd
Try this code and let me know if it works or not for you ?
Set sh = CreateObject("WScript.Shell")
MyCmd = "Cmd /c CD /D " & qq("C:\Program Files (x86)\WinRAR") & " & rar /? & pause"
sh.run MyCmd,1,True
'************************************
Function qq(strIn)
qq = Chr(34) & strIn & Chr(34)
End Function
'************************************
EDIT : Check this code :
Set sh = CreateObject("WScript.Shell")
MyCmd = "Cmd /c CD /D " & qq("C:\Program Files (x86)\WinRAR") & " & rar a c:\new.rar c:\*.* & pause"
sh.run MyCmd,1,True
'************************************
Function qq(strIn)
qq = Chr(34) & strIn & Chr(34)
End Function
'************************************

Popup to appear and close with a target process

I am trying to use the code located here, and with some tweaking for HTA. It works to a point:
Set wshShell = WScript.CreateObject( "WScript.Shell" )
changes into:
Set wshShell = CreateObject( "WScript.Shell" )
The popup comes up, but it won't go away until I click it. I need it appear when a process is running, then disappear when it ends. Why is my execution failing to do this?
ProgressMsg "Copying, Please wait.", "File Being Copied"
strCMD = "cmd.exe /c robocopy " & strLocalSemesterCourse & " " & strServerSemesterCourse &" " & strFileName & " /LOG+:" & strLogName & " /V /FP /TEE"
nReturn = objShell.Run(strCMD, 1, true)
ProgressMsg "", "Finished"
You need to define objProgressMsg as a global variable for this to work:
Dim objProgressMsg
...
ProgressMsg "Copying, Please wait.", "File Being Copied"
strCMD = "cmd.exe /c robocopy " & strLocalSemesterCourse & " " _
& strServerSemesterCourse &" " & strFileName & " /LOG+:" & strLogName _
& " /V /FP /TEE"
nReturn = objShell.Run(strCMD, 1, true)
ProgressMsg "", "Finished"
Without the global variable, ProgressMsg() will use a local variable objProgressMsg. Local variables don't retain their value after the function exits, so the variable will be empty every time you call the function.

VBScript error problems: Expected End 800A03F6

I am trying to make a vbs work, the idea is it will remotely install an msi, to a list of machines contained with a txt file.
I am getting multiple errors, the first is:
wrong number of arguments or invalid property assignment: "WshShell.Exec" Line 27, Char 1
WshShell.Exec "%COMSPEC% /C COPY " & StrInstallFile & " \\" & strComputer _
& "\C$\Windows\Temp", 0, TRUE
I seemd to have got round this with:
Set WshExec = WshShell.Exec......
then got:
expected end of statement line 27 cahr 29
adding an &:
Set WshExec = WshShell.Exec & "%COMSPEC%.....
now gets me:
expected end of statement line 27 char 110
which is the penultimate comma
Set WshExec = WshShell.Exec & "%COMSPEC% /C COPY" & StrInstallFile _
& " \\" & strComputer & "\C$\Windows\Temp", 0, TRUE
so i am not sure what is wrong at this point, and whether changing the whole line to a set was the right thing to have done.
You are mixing .Run and .Exec. The prototype for .Exec:
object.Exec(strCommand)
shows that you need someting like:
Set oExec = WshShell.Exec("%COMSPEC% /C COPY " & StrInstallFile & " \" & strComputer & "\C$\Windows\Temp")
If you want .Run instead, try something like:
Dim iRet : iRet = WshShell.Run(strCommand, [intWindowStyle], [bWaitOnReturn])
Dim iRet : iRet = WshShell.Run("%comspec% ...", 0, True)
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.OpenTextFile("MachineList.Txt", 1)
StrInstallFile="install_flash_player_11_active_x.msi"
StrNoUpdateFile="mms.cfg"
StrInstallCMD="msiexec.exe /qn /i "
Do Until objFile.AtEndOfStream
strComputer = objFile.ReadLine
' --------- Check If PC is on -------------
Set WshShell = WScript.CreateObject("WScript.Shell")
Set WshExec = WshShell.Exec("ping -n 1 -w 1000 " & strComputer) 'send 3 echo requests, waiting 2secs each
strPingResults = LCase(WshExec.StdOut.ReadAll)
If InStr(strPingResults, "reply from") Then
' ---------- Successful ping - run remote commands ----------------
Set objWMIService = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
' --------- Copy msi to windows temp folder
Set oExec = WshShell.Exec("%COMSPEC% /C COPY " & StrInstallFile & " \\" & strComputer & "\C$\Windows\Temp")
' --------- execute msi file on remote machine
Set oExec = WshShell.Exec("%COMSPEC% /C psexec \\" & StrComputer & " " & strInstallCMD & "c:\Windows\Temp\" & StrInstallFile)
' --------- Copy no "no update" file to remote machine, first line is for win7, second for xp
Set oExec = WshShell.Exec("%COMSPEC% /C COPY " & StrNoUpdateFile & " \\" & strComputer & "\C$\Windows\SysWOW64\Macromed\Flash")
Set oExec = WshShell.Exec("%COMSPEC% /C COPY " & StrNoUpdateFile & " \\" & strComputer & "\C$\Windows\system32\macromed\flash")
Else
' ---------- Unsuccessful ping - Leave computer name in MachineList.txt and continue ----------------
strNewContents = strNewContents & strComputer & vbCrLf
End If
Loop
objFile.Close
Set objFile = objFSO.OpenTextFile("MachineList.txt", 2)
objFile.Write strNewContents
objFile.Close

Resources