Using Shell and Cmd - vba

'Hello All, I am very new to VBA macro actually. I need help to create a macro which should open command prompt or powershell and then perform ping www.yahoo.com -t whenever user run that macro enabled doc file.
I am not able to create this. I tried below on windows 10 host.
Sub openBatch()
Call Shell("cmd ping yahoo.com -t", vbNormalFocus)
End Sub'

You can see Shell() as an equivalent of the Start > Run... tool in Windows or a call command in a command shell. In both cases, cmd ping yahoo.com -t would not work. You can either use ping yahoo.com -t or cmd -c ping yahoo.com -t.
So your code would become:
Sub openBatch()
Call Shell("ping yahoo.com -t", vbNormalFocus)
End Sub

https://learn.microsoft.com/en-us/previous-versions/windows/desktop/wmipicmp/win32-pingstatus
Set objWMIService = GetObject("winmgmts:\\.\root\cimv2")
Set colItems = objWMIService.ExecQuery("Select * From win32_PingStatus where address='www.yahoo.com'")
For Each objItem in colItems
msgbox "Status " & objItem.statuscode & " - Time " & objItem.ResponseTime
Next
Programmers don't shell to user's commands as its very unprofessional. The above is VBScript/VBA/VB6 code. If a user's command can do then so can you.
WMI has a command line interface as well. You can get help on Win32_PingStatus by typing in a command prompt
wmic path win32_pingstatus get /?

Related

command window not closed after restart PC Remotly using vb.net

I want to run two cmd commands in vb.net
Dim command As String ="Net USE \\serverip\IPC$ password /USER:username"
Dim command2 As String ="shutdown /r /t 3 /m \\serverip"
Process.Start("cmd.exe", "/k" + command)
Process.Start("cmd.exe", "/C" + command2)
Command window is opened how can we close this.
I try process.kill() it is not working.

How to save time for using plink in VBA

My VBA codes below shows the template about how to control my host via plink
Set wsh = VBA.CreateObject("WScript.Shell")
for i = 1 to 1000
wsh.Run("cmd.exe /c plink -ssh 192.168.100.xx -l root -pw xxxx command", 0, True)
Sleep 1000
next i
If I have to get information from host via plink.exe every second, it is necessary to execute "wsh.Run("cmd.exe /c plink -ssh 192.168.100.xx -l root -pw xxxx command", 0, True)" every time, which wastes time in opening shell and executing plink.exe and then close it all each round. Is there a way to save the time, which means that to open shell and execute plink.exe once and keep plink.exe listening my command until loop ends?
You may want to take a look at Chip Pearsons solution, Shell and Wait.
http://www.cpearson.com/excel/ShellAndWait.aspx
The ShellAndWait function calls Shell and then waits for the Shell'd process to terminate. You can specify an interval after which the function times out and returns to the caller. The declaration of ShellAndWait is as follows:

Second SSH command is not executed on running multiple ssh shell commands via VBScript (objShell.Run)

Invalid output is displayed on running multiple ssh shell commands via vbscript (QTP), whereas the same command is working fine with putty (unix server).
Line 1 - strShellCommand: The first ssh command connects to 'serverA', view the content of LogA.log and the second internal ssh command connects to 'serverB', view the content of LogB.log.
Line 3: Runs the shell command
'Build the command string for the Shell.Run command
1. strShellCommand = ". $HOME/.profile;ssh -q user#serverA '. $HOME/.bash_profile; cd /apps/logs;cat LogA.log;ssh -q user#serverB " & """. $HOME/.bash_profile; cd /apps/logs;cat LogB.log;""" & "'"
2. strCommand = "cmd /K CD " & PLINK_LOC & " & " & "plink.exe -ssh "& strUserName & "#" & strServerName & " -pw " & strPassword & " " & strShellCommand
'Run the command and direct out put to temporary result file
3. objShell.Run strCommand & ">" & strOutputLog & " 2>"& strErrorLog &" & exit", 0, blnCursorReturn
Output:
Subversion client 1.6.11
LogA Content
Subversion client 1.6.11
LogA Content
LogB content is not displayed in the output file, whereas the contents of LogA is repeated. It doesn't executes the second ssh command.
Shell Command Works Fine in Putty
If I run the same script via putty or shell, it is working fine
ssh -q user#serverA '. $HOME/.bash_profile; cd /apps/logs;cat LogA.log;ssh -q user#serverB ". $HOME/.bash_profile; cd /apps/logs;cat LogB.log;"'
Output:
Subversion client 1.6.11
LogA Content
Subversion client 1.6.11
LogB Content
Note: You can see the first ssh command uses single quote, whereas the second uses double quotes. This is how it works in putty.
I want to save both the logs in output file, so can you help to run it in single command via vbscript

How to pass an argument to Plink in VBA

I am trying the following string in command prompt to execute some (test) remote commands on my server:
plink.exe -ssh -pw [PASSWD] [U/NAME]#[SERVER] -m cmds.bat -v
In my cmds.bat file I have some test commands:
sleep 3
#echo off
ls -la ~/
#echo on
sleep 1
I now want to beef this up to run a remote script while passing an argument. The argument will be handled and appended by my VBA code. This is the part I am stuck at. Please note the following VBA code is only a snippet; the part that calls Plink. The surrounding code, I am happy with:
If re.Test(Msg.Subject) Then
Set matchCol = re.Execute(Msg.Subject)
For Each match In matchCol
shellStr = "plink.exe -ssh [USERNAME]#[SERVER] -pw [P/WORD] -m cmds.bat " & match
Shell(shellStr, vbNormalFocus)
Next
End If
The offending line is:
shellStr = "plink.exe -ssh [USERNAME]#[SERVER] -pw [P/WORD] -m cmds.bat " & match
I do not know how to append the value held in variable match (captured by the Regexp) to the string to be executed in opening the shell.
The bat file will handle the command for actually running the script on the remote Unix server, where instead of ls -la ~/ in the above example I will use:
python ~/myscript.py [ARGUMENT FROM VBA VARIABLE "match"]
But how do I pass this match variable's value into this?
you cannot do this; plink just doesn't support this usage. your best bet is doing what you suggested in your comment: build the script on the fly. (and maybe don't call it .bat, that seems misleading a bit …)

How to run a command in vmware using vmrun, command is (echo %PROGRAMFILES%)

How to run a command in vmware using vmrun, command is (echo %PROGRAMFILES%) on the guest machine..
and the guest machine should return a value of the command result... how to do this??? please suggest
I needed to do something similar and found this unanswered question. Here's my solution.
#ECHO OFF
REM Set up abbreviations that we'll be using a lot.
SET VMRUN="C:\Program Files (x86)\VMware\VMware VIX\vmrun.exe" -T ws -gu Administrator -gp password
SET VMX="C:\Users\whoever\Documents\Virtual Machines\Windows\Windows.vmx"
SET GUEST_COMSPEC="C:\WINDOWS\system32\cmd.exe"
REM Tried to do this in one line, but couldn't figure out the quoting.
%VMRUN% CreateTempfileInGuest %VMX% >%TEMP%\GuestTmp.txt || GOTO :CantCreate
FOR /F "delims=;" %%F IN ( %TEMP%\GuestTmp.txt ) DO SET GTEMP=%%F
REM The batch file is a one-liner that echos the variable into a file.
REM It could be generated dynamically and copied to the guest
REM but I didn't want to complicate things any further.
%VMRUN% runProgramInGuest %VMX% %GUEST_COMSPEC% "/c C:\echo-ProgramFiles.bat %GTEMP%"
%VMRUN% CopyFileFromGuestToHost %VMX% %GTEMP% %TEMP%\GuestOut.txt
%VMRUN% DeleteFileInGuest %VMX% %GTEMP%
REM Do something with the result and delete the temp files.
TYPE %TEMP%\GuestOut.txt
DEL %TEMP%\GuestOut.txt %TEMP%\GuestTmp.txt
GOTO :EOF
:CantCreate
REM Provide details on any problems.
TYPE %TEMP%\GuestTmp.txt 1>&2
DEL %TEMP%\GuestTmp.txt
EXIT 100
And here's the batch file on the guest host. As you can see, it's pretty simple. I couldn't get redirection to work in runProgramInGuest (probably didn't experiment enough)
so I just pass the file as a command line argument.
#echo %PROGRAMFILES% >%1
Have a look at the vmrun commands here. You need the Guest OS Command runScriptInGuest.
I have not checked the command , but it should look like this. Please verify it.
vmrun -T server -h https://xps:8333/sdk -u user -p mypassword -gu administrator -gp guestpaswd
runScriptInGuest "[Vol1] win2008-1/win2008-1.vmx" "echo %PROGRAMFILES%"
I had to use runProgramInGuest
capture the output to a file
copy the file back to my host and use it
Thats the soln I used.