VB.Net Execute PowerShell script file - vb.net

I would like to execute powershell script from vb.net
Here is my code that i try, it opens the powershell window but it doesn't read the .ps1 file information it just stays open window and nothing doing.
Dim FileNamePath As String = My.Application.Info.DirectoryPath & "\Script.ps1"
Dim processStartInfo As New ProcessStartInfo("powershell.exe") With {
.Arguments = "-NoProfile -noexit -ExecutionPolicy Bypass -File " & FileNamePath,
.UseShellExecute = False,
.CreateNoWindow = False,
.RedirectStandardError = True,
.RedirectStandardOutput = True,
.Verb = "runas",
.WindowStyle = ProcessWindowStyle.Normal
}
Using p As New Process()
p.StartInfo = processStartInfo
p.Start()
p.WaitForExit()
End Using

Related

VB.Net Process Start Info With Arguments

Hello Can Anyone please help me i have a code here but it wont work when i use the argumenets
maybe someone can help me out
Dim procStartInfo As New ProcessStartInfo
Dim InstallVirtualBoxSetup As New Process
Dim Output As String = InstallVirtualBoxSetup.StandardOutput.ReadToEnd()
'Run As Admin
With procStartInfo
.UseShellExecute = True
.FileName = My.Application.Info.DirectoryPath & "\Components\Virtualbox.msi" & """ -quiet -norestart -l vlog.txt VBOX_INSTALLDESKTOPSHORTCUT=0 VBOX_INSTALLQUICKLAUNCHSHORTCUT=0 VBOX_START=0 VBOX_REGISTERFILEEXTENSIONS=1"
.Arguments = My.Application.Info.DirectoryPath & "\Components\Virtualbox.msi" & """ -quiet -norestart -l vlog.txt VBOX_INSTALLDESKTOPSHORTCUT=0 VBOX_INSTALLQUICKLAUNCHSHORTCUT=0 VBOX_START=0 VBOX_REGISTERFILEEXTENSIONS=1"
.WindowStyle = ProcessWindowStyle.Normal
.Verb = "runas" 'add this to prompt for elevation
End With
InstallVirtualBoxSetup = Process.Start(procStartInfo)
Output = InstallVirtualBoxSetup.StandardOutput.ReadToEnd
InstallVirtualBoxSetup.WaitForExit()
When using Arguments, you should supply a list of the arguments only, without the file being executed.
The FileName should just be the path to the executed\opened file.
Here's the code that's been changed to reflect this:
With procStartInfo
.UseShellExecute = True
.FileName = My.Application.Info.DirectoryPath & "\Components\Virtualbox.msi"
.Arguments = "-quiet -norestart -l vlog.txt VBOX_INSTALLDESKTOPSHORTCUT=0 VBOX_INSTALLQUICKLAUNCHSHORTCUT=0 VBOX_START=0 VBOX_REGISTERFILEEXTENSIONS=1"
.WindowStyle = ProcessWindowStyle.Normal
.Verb = "runas" 'add this to prompt for elevation
End With

Play Star Wars in CMD with Visual basic

I know that the 2 commands I need to run are:
pkgmgr /iu:"TelnetClient"
Telnet Towel.blinkenlights.nl
I would like it to keep cmd open after running Telnet Towel.blinkenlights.nl.
This is what I have tried:
Dim start As New Process()
start.StartInfo.FileName = "cmd.exe"
start.StartInfo.Arguments = "/c pkgmgr /iu:" & """" & "TelnetClient" & """"
start.StartInfo.CreateNoWindow = True
start.StartInfo.WindowStyle = ProcessWindowStyle.Hidden
start.StartInfo.UseShellExecute = False
start.Start()
System.WaitForExit()
Dim playSW As New Process()
playSW.StartInfo.FileName = "cmd.exe"
playSW.StartInfo.Arguments = "/k Telnet Towel.blinkenlights.nl"
playSW.Start()
playSW.WaitForExit()
You can use the Process's WaitForExit command to ensure a process is completed:
Dim start As New Process()
start.StartInfo.FileName = "cmd.exe"
start.StartInfo.Arguments = "/c pkgmgr /iu:" & """" & "TelnetClient" & """"
start.StartInfo.CreateNoWindow = True
start.StartInfo.WindowStyle = ProcessWindowStyle.Hidden
start.StartInfo.UseShellExecute = False
start.Start()
start.WaitForExit()
Dim playSW As New Process()
playSW.StartInfo.FileName = "cmd.exe"
playSW.StartInfo.Arguments = "/c Telnet Towel.blinkenlights.nl"
playSW.Start()
playSW.WaitForExit()
Reference: https://learn.microsoft.com/en-us/dotnet/api/system.diagnostics.process.waitforexit?view=netframework-4.7.2

How to run CMD commands and hide it

Hey i am programming in Visual Basic (VB.NET) and i am trying to run cmd commands without showing the cmd screen on the computer, I have the following code but i cant get it to hide it.. :(
Code:
Dim CMD As New Process
CMD.StartInfo.FileName = "cmd.exe"
CMD.StartInfo.UseShellExecute = False
CMD.StartInfo.RedirectStandardInput = True
CMD.StartInfo.RedirectStandardOutput = True
CMD.StartInfo.CreateNoWindow = True
CMD.Start()
Dim SW As System.IO.StreamWriter = CMD.StandardInput
Dim SR As System.IO.StreamReader = CMD.StandardOutput
SW.WriteLine("exit")
Process.Start("Cmd.exe", "/C systeminfo > C:\Users\" & Environment.UserName & "\Pictures\hello.txt")
Thread.Sleep(5000)
Ugly code aside, you can basically do it like this:
Dim CMD As New Process
CMD.StartInfo.FileName = "cmd.exe"
CMD.StartInfo.UseShellExecute = False
CMD.StartInfo.RedirectStandardInput = True
CMD.StartInfo.RedirectStandardOutput = True
CMD.StartInfo.CreateNoWindow = True
CMD.Start()
Dim SW As System.IO.StreamWriter = CMD.StandardInput
Dim SR As System.IO.StreamReader = CMD.StandardOutput
SW.WriteLine("exit")
Dim p2 As New Process
p2.StartInfo.FileName = "Cmd.exe"
p2.StartInfo.Arguments = "/C systeminfo > C:\Users\" & Environment.UserName & "\Pictures\hello.txt"
p2.StartInfo.WindowStyle = ProcessWindowStyle.Hidden
p2.Start()
Thread.Sleep(5000)
Process.Start is a static method with a limited set of parameters for modifying its behavior.
So where you give the name of the .exe and the parameters via the static method
Process.Start("Cmd.exe", "/C systeminfo > C:\Users\" & Environment.UserName & "\Pictures\hello.txt")
, is similar to:
Dim p2 As New Process
p2.StartInfo.FileName = "Cmd.exe"
p2.StartInfo.Arguments = "/C systeminfo > C:\Users\" & Environment.UserName & "\Pictures\hello.txt",
p2.Start()
But by doing it like that you have many more options available, including the option to set the WindowStyle to hidden:
p2.StartInfo.WindowStyle = ProcessWindowStyle.Hidden

Need to prompt user for UAC when attempting to open a share using Process.Start

This works, if the current user has rights to the UNC path. Opens it right up.
Process.Start("\\USERSHARE\VALUE\EMPLOYEES\")
However, I have to run the entire program as a user that doesn't have access to the UNC path due to SQL permissions in the code.
I made a button in the app that will open the UNC path in an explorer window, but I cannot figure out how to force a runas for the operation.
I have tried the following as well:
Dim procStartInfo As New ProcessStartInfo
Dim procExecuting As New Process
With procStartInfo
.UseShellExecute = True
.FileName = "Notepad.exe"
.WindowStyle = ProcessWindowStyle.Normal
.Verb = "runas" 'add this to prompt for elevation
End With
procExecuting = Process.Start(procStartInfo)
This works and prompts with UAC to open "notepad".
This doesn't work to open the UNC path:
Dim procStartInfo As New ProcessStartInfo
With procStartInfo
.UseShellExecute = True
.FileName = "\\USERSHARE\VALUE\EMPLOYEES\"
.WindowStyle = ProcessWindowStyle.Normal
.Verb = "runas" 'add this to prompt for elevation
End With
Process.Start(procStartInfo)
I understand opening the fileshare isn't the same as pointing the .FileName at an executable.
I'm having problems trying to have the app prompt for UAC before attempting to open the remote folder.
You don't want to execute the folder itself, but rather explorer.exe with the folder as an argument:
Dim procStartInfo As New ProcessStartInfo
With procStartInfo
.UseShellExecute = True
.FileName = "explorer.exe"
.Arguments = "\\USERSHARE\VALUE\EMPLOYEES\"
.WindowStyle = ProcessWindowStyle.Normal
.Verb = "runas" 'add this to prompt for elevation
End With
Process.Start(procStartInfo)

Wait for batch file to close before continuing - VB.net

I'm trying to run a batch file via VB and I need to wait for it to complete/exit before progressing. The issue I believe I am having is that when a batch file is executed, it opens cmd.exe and not the batch file.
This is what I am executing with VB
My.Computer.FileSystem.DeleteFile(My.Application.Info.DirectoryPath & "\PingCheck\machines.txt")
FileCopy(My.Application.Info.DirectoryPath & "\machines.txt", My.Application.Info.DirectoryPath & "\PingCheck\machines.txt")
Dim psi As New ProcessStartInfo(My.Application.Info.DirectoryPath & "\PingCheck\go.bat")
psi.RedirectStandardError = True
psi.RedirectStandardOutput = True
psi.CreateNoWindow = False
psi.WindowStyle = ProcessWindowStyle.Hidden
psi.UseShellExecute = False
Dim process As Process = process.Start(psi)
process.WaitForExit()
ProgressBar1.Value = ProgressBar1.Value + 2
FileCopy(My.Application.Info.DirectoryPath & "\PingCheck\machines.txt", My.Application.Info.DirectoryPath & "\machines.txt")
'My.Computer.FileSystem.DeleteFile(My.Application.Info.DirectoryPath & "\ping.bat")
MsgBox("Ping Check Complete")
The problem im having is that it will just delete ping.bat before it completes.
How do I go about monitoring the process from the batch file I call. Then once it exits, continue with the script?
RHicke shows a nice example of how to run a batch process in VB.NET here, Run batch file in vb.net?.
To expand, you should use the function WaitForExit() to wait for the process to complete.
Dim psi As New ProcessStartInfo("Path TO Batch File")
psi.RedirectStandardError = True
psi.RedirectStandardOutput = True
psi.CreateNoWindow = False
psi.WindowStyle = ProcessWindowStyle.Hidden
psi.UseShellExecute = False
Dim process As Process = Process.Start(psi)
process.WaitForExit()
You could use the System.Diagnostics.Process class to start the batch file. The process reference will give you access to the property HasExited (and more interesting information). The HasExited property indicates whether a process has completed.
var process = System.Diagnostics.Process.Start(new ProcessStartInfo
{
FileName = "batch file path",
RedirectStandardError = true,
RedirectStandardOutput = true,
UseShellExecute = false,
Arguments = "parameters if applicable",
CreateNoWindow = true
});
while(!process.HasExited)
{
// obviously do some clever here to wait
}
Code is in C# but the principle should work in VB.NET
I've done something similar before. This code invokes RoboCopy from within VB.Net, using the System.Diagnostics.Process (mentioned by rivethead_).
Dim proc As System.Diagnostics.Process = New System.Diagnostics.Process()
proc.EnableRaisingEvents = False
proc.StartInfo.FileName = "d:\robocopy\robocopy"
proc.StartInfo.Arguments = strSrcDir & " " & strDestDir & " " & strFile & " " & My.Settings.robocopyFlags
proc.Start()
proc.WaitForExit()
Otherwise, what is the ping.bat doing? Is it just doing a "ping" command? If so, maybe you could invoke that with a System.Diagnostics.Process (instead of invoking a .bat file to do it). That might give you some more control over your process.