Multiple commands to run using Visual Basic - vb.net

I am developing a tool on Visual Studio 2010 which has a button which executes a powershell program. But before this execution we need to change the path on cmd prompt.
cd Try & powershell C:\Users\Medha\Try\out.ps1
, this statement works fine on cmd prompt but in my VB code, both the commands are taken together and executed at once, which needs to be one by one.
I have tried this
> Shell("cmd.exe /k" + "cd Try & powershell C:\Users\Medha\Try\out.ps1")
Please suggest changes to make it work.

Why don't you use WorkingDirectory property
Dim myProcess As New System.Diagnostics.Process
//if it's in system directory use Environment.SystemDirectory
myProcess.StartInfo.WorkingDirectory = "your\working\directory"
myProcess.StartInfo.FileName = "powershell.exe"
myProcess.StartInfo.UseShellExecute = True
myProcess.Start

Related

Running Perl scripts through cmd on VB with asynchronous output to VB

I'm quite new to VB.
I'm attempting to run a Perl script within perl through cmd, while streaming output from the perl script asynchronously.
Here's a part of my code:
Dim command as string = "perl perlscriptname.pl"
Dim proc As ProcessStartInfo = New ProcessStartInfo("cmd", "/c " & command)
proc.RedirectStandardOutput = True
proc.UseShellExecute = False
proc.CreateNoWindow = True
Dim process As Process = Process.Start(proc)
Dim stream As StreamReader = process.StandardOutput
While Not process.HasExited
tb.AppendText(Await stream.ReadLineAsync & vbNewLine)
End While
This allow me to run different cmd commands from VB, and I will mainly be only running Perl scripts from VB.
With this code, I'm able to get asynchronous output while executing some functions such as pinging, but when I attempt to run a Perl script, I'm unable to get a "live"/asynchronous output from the Perl script to VB.
Hope guys can help or advice me on how to proceed cus i'm stuck at this for quite some time.
Thanks in advance!

How to open the SCCM Configuration Manager in VB - Visual Studio 2015

I'm creating a tool in VB using Visual Studio 2015 and I'm having some issues with forcing one item on a menu strip when clicked to open the SCCM Configuration Manager.
So far I've tried:
Option 1
Dim ProcID As Integer
ProcID = Shell("control smscfgrc", AppWinStyle.NormalFocus)
Option 2
Process.Start("cmd.exe", "control smscfgrc")
Option 3
Dim p as Process = new Process()
Dim pi as ProcessStartInfo = new ProcessStartInfo()
pi.Arguments = "control smscfgrc"
pi.FileName = "cmd.exe"
p.StartInfo = pi
Option 4
Shell=("control smscfgrc", 0)
None of the above work, they just open the console but nothing else.
If I open a regular cmd window using "windows + R" and type the command "control smscfgrc" it open the SCCM Configuration Manager as it should.
I really need this to complete my tool, any help is much appreciated!
Thank you for the time you took to read this.
I'm not a guru with VS nor VB, but your commands to open cmd.exe looks incorrect. You need to add a /c. The command in the Run window ( + R) would look like this ...
cmd.exe /c control smscfgrc
Of course, control is actually control.exe, so you don't even need cmd.exe:
control.exe smscfgrc
Tested and confirmed that this opens the Configuration Manager Properties window from the Run windows on my computer.
You also may need the full path to control.exe. I would use environment variables; I think this is how it would be done in VB:
Dim control_exe As String
control_exe = Environment.GetEnvironmentVariable("SystemRoot") & "\System32\control.exe"
You will automatically get redirected to SysWOW64 if running on as a 32-bit process on a 64-bit OS.
Option 2
Process.Start(control_exe, "smscfgrc")
Option 3
Dim p as Process = new Process()
Dim pi as ProcessStartInfo = new ProcessStartInfo()
pi.Arguments = "smscfgrc"
pi.FileName = control_exe
p.StartInfo = pi

Running SETX from Visual Basic

At the moment, I am building a program in Visual Basic 2010 Professional. When a button is pressed, I need the following to run...
Shell(CMD.exe)
SendKeys.Send("SETX CCDeviceID " & DeviceID & "")
SendKeys.Send()
SendKeys.Send("{ENTER}")
SendKeys.Send("exit")
SendKeys.Send("{ENTER}")
Shell("C:\Python27\python C:\FusionTechnology\stage1.py")
SendKeys.Send("{ENTER}")
SendKeys.Send("exit")
and this works, but the python script starts running before the SETX command completes. Any ideas on how I can wait for the SETX to run and finish before executing the next command?
Thanks!
I would use the System.Diagnostics.Process class, something like this;
Dim vProcess As Diagnostics.Process
vProcess = Process.Start("SETX.exe", "CCDeviceID " & DeviceID)
vProcess.WaitForExit()
vProcess = Process.Start("C:\Python27\python", "C:\FusionTechnology\stage1.py")
Saves messing about with commands in the shell one line at a time!

How to pass command line arguments to the .exe file (e.g silent mode) using process.start

I have an vb.net windows application in this application i want run another exe file in silent mode,for this first i have run this exe file in command line it is working.But i don't know how to pass the these arguments through vb.coding process.start .
through command line i have pass like this.
D:\myapps>sample.exe /s /v/qn (working fine)
but through coding i have pass like this
Directory.SetCurrentDirectory(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location))
Dim info As New System.Diagnostics.ProcessStartInfo
info.FileName = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location) + "\sample.exe"
info.Arguments = "/s /v/qn"
Dim process As New System.Diagnostics.Process
process.StartInfo = info
process.Start()
MessageBox.Show(info.Arguments.ToString())
process.Close()
this is not working what is wrong with this code please help me..
Process.Start("D:\myapps\sample.exe", "/s /v/qn")

VB.net - Running a java application using Shell() and set its appdata folder. multiple commands?

Alright guys I have a copy of minecraft wich is a java program launched by Minecraft.exe.
Inside the same folder is my program (lets call it launcher.exe) wich I am programming in VB.net and a Folder called LocalAppData.
If I place a shortcut in the same folder as Minecraft.exe, clear the "start in" field and put this in the target field:
C:\Windows\System32\cmd.exe /c start cd LocalAppData&& set APPDATA=%cd%\LocalAppData&& javaw -Xms4096M -Xmx4096M -cp LocalAppData\Minecraft.exe net.minecraft.LauncherFrame
then minecraft launches with my custom memory allocation from inside the LocalAppData folder. Two command windows appear as well. One closes when minecraft does, but the other does not and needs to be closed by the user
My Question is: How do I acheive the same result in VB.net instead of with a windows shortcut and is there a way to either stop the command windows appearing or setting them both to close automatically?
My goal is to launch minecraft from a subfolder, so local filepaths would be far preferrable to global filepaths, but figuring out the location of the application at runtime and working from a subfolder would be ok as well.
I thought I would be able to use the same code inside a Shell() command to produce the same effect, but it appears not.
Ideally I want to create a program that runs minecraft with:
Custom memory allocation
Local filepaths so that it can be run portably
The appdata folder changed to the subfolder so that it can be run portably
Those command windows either gone or minimised and then close automatically when minecraft is closed by the user.
I know this is a big ask, but I'm 6 months into a programming course and I'll admit that I'm not the best programmer out there.
Once I know how to do this I can create the rest of the program that manages multiple installations in seperate subfolders and lets you choose wich one to launch, but I just need help with the actual launching of the java application itself.
Note:
I should clarify that Minecraft.exe is not something that I have made and that I don't program java. I'm just looking for a solution in VB.Net.
Thank you for reading all this and sorry for the long post.
Edit
Thank you for the help. This is what I have so far, but it produces an error "Error: Could not create the JavaVirtualMachine. Error: A fatal exception has occurred. Program will exit"
'Declare Processes
Dim appDataStartInfo As ProcessStartInfo = New ProcessStartInfo()
Dim javaStartInfo As ProcessStartInfo = New ProcessStartInfo()
Dim appPath As String = Application.StartupPath()
'Launch appdata relocation process
appDataStartInfo.FileName = "cmd.exe"
appDataStartInfo.Arguments = "/c start cd " & appPath & "&& set APPDATA=" & appPath & "\LocalAppData"
appDataStartInfo.UseShellExecute = True
Process.Start(appDataStartInfo)
'Launch Minecraft
javaStartInfo.FileName = "javaw.exe"
javaStartInfo.Arguments = "-Xms4096M -Xmx4096M -cp " & appPath & "\LocalAppData\.minecraft\bin\Minecraft.jar net.minecraft.LauncherFrame"
javaStartInfo.UseShellExecute = True
Process.Start(javaStartInfo)
Does anyone see where I've gone wrong?
The Process class (http://msdn.microsoft.com/en-us/library/system.diagnostics.process.aspx )allows you to launch a process. You set it up with a ProcessStartInfo instance (http://msdn.microsoft.com/en-us/library/system.diagnostics.processstartinfo(v=vs.80).aspx ).
I don't have the time to give you all the details, but this pseudo-code should get you started :
Dim startInfo As ProcessStartInfo = new ProcessStartInfo()
startInfo.FileName = "javaw.exe" 'That's the name of your executable
startInfo.Arguments = "your argument line"
startInfo.UseShellExecute = true 'Needed to open a command window
Process.Start(startInfo)