command window not closed after restart PC Remotly using vb.net - 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.

Related

How to Release File-Lock on File Created by Shell in VBA?

This command generates an output file:
Update: See new command
Shell "cmd /c fc file1 file2 > result.txt"
i initially didn't use cmd /c at the front of the command:
Shell "fc file1 file2 > result.txt"
That failed to generated the expected result.txt file. i don't know why. Including cmd /c at the front of the command makes it work.
The subsequent Open statement gets "Path/File access error"
iFile = FreeFile
Open "result.txt" For Input As iFile
Neither the file nor parent directory are open in Windows Explorer.
The following changes don't help (TaskKill and AccessRead):
vPID = Shell("cmd /c fc file1 file2 > result.txt", vbHide)
Shell "TaskKill /F /PID " & CStr(vPID), vbHide
iFile = FreeFile
Open "result.txt" For Input Access Read As iFile
The Open statement works if it's run as a separate, subsequent process.
How to do the Open in the same process?

Using Shell and Cmd

'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 /?

How to use command prompt via vb.net to use BCDEDIT

I have a batch file which contains some commands with bcdedit.
but in my vb.net application when I run that batch file via: Process.Start("CMD", " /C \blahblah\test.bat") it says bcdedit is not recognized.
so what I've found so far is the cmd.exe that vb.net opens it doesn't recognize bcdedit command at all.

Exit processes launched from cmd with cmd exit

I wrote a script that launches a cmd window. Cmd window further launches several processes. What I want is that on closing the cmd window manually or using exit command, all processes launched via cmd also get killed.
Technically it is not possible to change how the exit command works. What you could do is make your own custom start and exit commands by having something like this:
Start Command Batch File: (context would be start2 program.exe)
start "" "%1"
if exist onExit.txt echo %1>> onExit.txt
if not exist onExit.txt echo %1> onExit.txt
Save that as start2.bat
Exit command BatchFile: (context would just be Exit2)
for /F "tokens=*" %%A in (onExit.txt) do (taskkill /f /im "%%A"
exit
Save that as Exit2.bat
What those scripts do is keep track of each program you start with the custom command start2 and then taskkill them on your custom exit. You could have these files in the same directory of the batch file, or you could even have your batch file create these files with echo file contents> file.bat and echo more file contents>> file.bat. This is the best you could probably do without placing the actual cmd Executable in system32, which I would certainly not recommend since many other programs use it. Hope this helps

How to check if a particular command line is running in cmd propmt

I desperately need help to create a vb / dos code which will do the following:
Check if a command prompt window is running with the following command: mgms A1 (mgms is a custom command)
If it is running, exit.
If it is not running, start cmd prompt and run the command , exit
Thanks a lot for your help!
The Windows cmd.exe batch language is horrible, but you should be able to put this in a batch file and get it working:
tasklist /FI "IMAGENAME eq mgms.exe" 2>&1 | findstr /B "INFO: No tasks running" > tmp
for /F "delims=" %x in (tmp) do mgms A1
You may need to further check that the command-line arguments to mgms.exe match what you expect -- have a look at the help for tasklist.exe and findstr.exe. Both programs are both standard in WinXP Pro and up, I believe. If you don't have them, I'm sure you can find them or (near) equivalents on the web.