Having a hard time with this one. I can run the following command from a command prompt successfully, but can't get it working with a VB script.
From CMD:
Change directory to C:\Program Files (x86)\VMware\VMware Workstation\
then run: vmrun.exe -T ws start "C:\Users\Office\Documents\Virtual Machines\Windows 7\Windows 7.vmx" nogui
What I've tried in VBS:
Dim objShell, strPath1, strAttr, strPath2
Set objShell = CreateObject ("WScript.Shell")
strPath1 = "C:\Program Files (x86)\VMware\VMware Workstation\vmrun.exe"
strAttr1 = " -T ws start "
strAttr2 = "C:\Users\Office\Documents\Virtual Machines\Windows 7\Windows 7.vmx"
strAttr3 = " nogui"
'WScript.Echo strPath1 & strAttr1 & """" & strAttr2 & """" & strAttr3
objShell.Run strPath1 & strAttr1 & """" & strAttr2 & """" & strAttr3
The error I get is: The system cannot find the file specified.
Working Code ended up being:
Dim objShell, strPath1, strAttr1, strAttr2, strAttr3
Set objShell = CreateObject ("WScript.Shell")
strPath1 = """C:\Program Files (x86)\VMware\VMware Workstation\vmrun.exe"""
strAttr1 = " -T ws start "
strAttr2 = """C:\Users\Office\Documents\Virtual Machines\Windows 7\Windows 7.vmx"""
strAttr3 = " nogui"
objShell.Run strPath1 & strAttr1 & strAttr2 & strAttr3
i would replace objShell.Run strPath1 & strAttr1 & """" & strAttr2 & """" & strAttr3
with
objShell.Run strPath1 & strAttr1 & chr(34) & strAttr2 & chr(34) & strAttr3
or include the chr(34) before and after the strAttr2 variable
strAttr2 = chr(34) & "C:\Users\Office\Documents\Virtual Machines\Windows 7\Windows 7.vmx" & chr(34)
btw chr(34) = "
Related
I have a command that I have build and stored in a variable in PowerShell. This command works if I do a Write-Host and copy and paste into a standard cmd.exe window.
How do I execute this command from inside my script?
I have tried several combination of Invoke-Command or Invoke-Expression with no luck.
This is how I built the variable:
$cmd1 = $arcprg + $arcdir + "\" + $site1 + "-" + $hst + "-" + $yesterday + ".zip " + $logpath1 + "u_ex" + $yesterday + ".log"
This is what the variable looks like if it is printed to the screen:
7z.exe a -tzip c:\arc_logs\site-host-at-web1-100827.zip c:\inetpub\logs\logfiles\w3svc1\u_ex100827.log
Here is yet another way without Invoke-Expression but with two variables
(command:string and parameters:array). It works fine for me. Assume
7z.exe is in the system path.
$cmd = '7z.exe'
$prm = 'a', '-tzip', 'c:\temp\with space\test1.zip', 'C:\TEMP\with space\changelog'
& $cmd $prm
If the command is known (7z.exe) and only parameters are variable then this will do
$prm = 'a', '-tzip', 'c:\temp\with space\test1.zip', 'C:\TEMP\with space\changelog'
& 7z.exe $prm
BTW, Invoke-Expression with one parameter works for me, too, e.g. this works
$cmd = '& 7z.exe a -tzip "c:\temp\with space\test2.zip" "C:\TEMP\with space\changelog"'
Invoke-Expression $cmd
P.S. I usually prefer the way with a parameter array because it is easier to
compose programmatically than to build an expression for Invoke-Expression.
Try invoking your command with Invoke-Expression:
Invoke-Expression $cmd1
Here is a working example on my machine:
$cmd = "& 'C:\Program Files\7-zip\7z.exe' a -tzip c:\temp\test.zip c:\temp\test.txt"
Invoke-Expression $cmd
iex is an alias for Invoke-Expression so you could do:
iex $cmd1
For a full list :
Visit https://ss64.com/ps/ for more Powershell stuff.
Good Luck...
The following VBScript gives the error:
failing : error invalid character line 5 column 54
My VBScript:
dim myobject :
set myobject = createobject ( "wscript.shell" ) :
powershellcommand = "powershell.exe -Executionpolicy bypass -noprofile -windowstyle hidden -command Set-content -value (new-object System.net.webclient).downloaddata( https://website.com/folder/download.exe ) -encoding byte -Path $env:appdata\download.exe ; start $env:appdata\download.exe " :
myobject.run " & myobject.expandenvironmentstrings( "%systemroot%" ) & "\SYSTEM32\windowspowerShell\v1.0\powershell.exe" & " & ' ' & " & powershellcommand & " , 0 : set myobject = NOTHING
How do I fix it?
With regard to .run method: essentially, the string value indicating the command line you want to run should appear exactly as it would if you typed it at the command prompt, e.g. something like
powershell.exe -Executionpolicy bypass -noprofile -windowstyle hidden -command "Set-Content -value (new-object System.net.webclient).downloaddata( 'http://pspad.poradna.net/release/pspad462_setup.exe' ) -encoding byte -Path $env:appdata\download.exe; Start-Process $env:appdata\download.exe"
The above command runs as expected (unfailing in Windows 8.1/64, PSVersion 5.1.14409.1012). However, if you want to supply full path to powershell executable, then it could look as follows:
"C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe" -Executionpolicy bypass -noprofile -windowstyle hidden -command "Set-Content -value (new-object System.net.webclient).downloaddata( 'http://pspad.poradna.net/release/pspad462_setup.exe' ) -encoding byte -Path $env:appdata\download.exe; Start-Process $env:appdata\download.exe"
You need to properly build such string value in VBScript e.g. as in the following code snippet for the latter:
Option Explicit
Dim WshShell, powershellcommand, objFSO, sPSexePath
Set WshShell = CreateObject("Wscript.Shell")
Set objFSO = CreateObject("Scripting.FileSystemObject")
sPSexePath = objFSO.BuildPath( WshShell.expandenvironmentstrings( "%systemroot%" ) _
, "System32\WindowsPowerShell\v1.0\powershell.exe" )
powershellcommand = """" & sPSexePath & """ -Executionpolicy bypass -noprofile " _
& " -windowstyle hidden -command ""Set-Content -value " _
& " (new-object System.net.webclient)" _
& ".downloaddata( 'http://pspad.poradna.net/release/pspad462_setup.exe' ) " _
& " -encoding byte -Path $env:appdata\download.exe; " _
& " Start-Process $env:appdata\download.exe"""
WshShell.Run powershellcommand, 0
Note that in VBScript, a sequence of literal characters, enclosed in double quotes ("), is recognized as a string. A single quotation mark (') is allowed within a string. To insert a double quotation mark (") into a string, it should be duplicated as "".
Am trying to open a website and filling a form, from "echo off" till "del %command%" is working fine but from the second example website it's not working which related to filling forms!
#echo off
set command=C:\Users\%USERNAME%\Desktop\GMAIL.VBS
start https://www.example.com/sign-in
echo Set objShell = WScript.CreateObject("WScript.Shell") > %command%
echo Set WshShell = WScript.CreateObject("WScript.Shell") >> %command%
echo Do Until Success = True >> %command%
echo Success = objShell.AppActivate("Google Chrome") >> %command%
echo Loop >> %command%
echo WshShell.SendKeys "E-mail" >> %command%
echo WshShell.SendKeys "{TAB}" >> %command%
echo WshShell.SendKeys "Password" >> %command%
echo WshShell.SendKeys "{ENTER}" >> %command%
ping 192.0.2.2 -n 1 -w 5000 > nul
start %command%
ping 192.0.2.2 -n 1 -w 1000 > nul
del %command%
start https://www.example2.com/my-account/new-order/
echo WshShell.Sendkeys "Name"
echo WshShell.Sendkeys "{TAB}"
echo WshShell.Sendkeys "City"
echo WshShell.Sendkeys "{TAB}"
echo WshShell.Sendkeys "City"
exit
I get this error:
Image
When i try to run this code:
strComputer = "server01"
strCommand = "powershell.exe -NoLogo -Command Start-Service -InputObject $(Get-Service -Computer " & strComputer & " -Name " & strService & ")"
Set WshShell = WScript.CreateObject("WScript.Shell")
Set objExec = WshShell.Run(strCommand,0,True)
I want to run strCommand in an hidden window. This works the code ran through and do what it should do but this error occure and this is really bad.
.Run() - as opposed to .Exec() - returns an error code (not an object). So you must not use
Set objExec = WshShell.Run(strCommand,0,True)
but
nErrorCode = WshShell.Run(strCommand,0,True)
You need to add a double quote before the command and at its end:
strCommand = "powershell.exe -NoExit -NoLogo -Command ""Start-Service -PassThru -InputObject (Get-Service -Computer " & strComputer & " -Name " & strService & ")"""
I have a command that I have build and stored in a variable in PowerShell. This command works if I do a Write-Host and copy and paste into a standard cmd.exe window.
How do I execute this command from inside my script?
I have tried several combination of Invoke-Command or Invoke-Expression with no luck.
This is how I built the variable:
$cmd1 = $arcprg + $arcdir + "\" + $site1 + "-" + $hst + "-" + $yesterday + ".zip " + $logpath1 + "u_ex" + $yesterday + ".log"
This is what the variable looks like if it is printed to the screen:
7z.exe a -tzip c:\arc_logs\site-host-at-web1-100827.zip c:\inetpub\logs\logfiles\w3svc1\u_ex100827.log
Here is yet another way without Invoke-Expression but with two variables
(command:string and parameters:array). It works fine for me. Assume
7z.exe is in the system path.
$cmd = '7z.exe'
$prm = 'a', '-tzip', 'c:\temp\with space\test1.zip', 'C:\TEMP\with space\changelog'
& $cmd $prm
If the command is known (7z.exe) and only parameters are variable then this will do
$prm = 'a', '-tzip', 'c:\temp\with space\test1.zip', 'C:\TEMP\with space\changelog'
& 7z.exe $prm
BTW, Invoke-Expression with one parameter works for me, too, e.g. this works
$cmd = '& 7z.exe a -tzip "c:\temp\with space\test2.zip" "C:\TEMP\with space\changelog"'
Invoke-Expression $cmd
P.S. I usually prefer the way with a parameter array because it is easier to
compose programmatically than to build an expression for Invoke-Expression.
Try invoking your command with Invoke-Expression:
Invoke-Expression $cmd1
Here is a working example on my machine:
$cmd = "& 'C:\Program Files\7-zip\7z.exe' a -tzip c:\temp\test.zip c:\temp\test.txt"
Invoke-Expression $cmd
iex is an alias for Invoke-Expression so you could do:
iex $cmd1
For a full list :
Visit https://ss64.com/ps/ for more Powershell stuff.
Good Luck...