start batch file from within vb.net as admin - vb.net

In my application, I need to run a batch file as admin for it to function.
I'm using this so far but I cant remember how to use the runas feature which allows it to run with admin rights.
process.start("filelocation.bat")
Any help would be apreciated.

Try
Dim procInfo As New ProcessStartInfo()
procInfo.UseShellExecute = True
procInfo.FileName = (FileLocation)
procInfo.WorkingDirectory = ""
procInfo.Verb = "runas"
Process.Start(procInfo)
Catch ex As Exception
MessageBox.Show(ex.Message.ToString())
End Try

You could try with this code:
Dim proc as ProcessStartInfo = new ProcessStartInfo()
proc.FileName = "runas"
proc.Arguments = "/env /user:Administrator filelocation.bat"
proc.WorkingDirectory = "your_working_dir"
Process.Start(proc)
This code will ask the Administrator password and the start the execution of your batch file
EDIT:
This is an alternative without the cmd window
Dim proc as ProcessStartInfo = new ProcessStartInfo()
proc.FileName = "filelocation.bat"
proc.WorkingDirectory = "your_working_dir" // <- Obbligatory
proc.UseShellExecute = False
proc.Domain = userDomain // Only in AD environments?
proc.UserName = userName
proc.Password = securePassword
Process.Start(proc)
It's a little more complicated because you need to get the input values (userName, Password, domain) before using this code and the password is a SecureString that you need to build in a dedicated way
Dim securePassword as New Security.SecureString()
For Each c As Char In userPassword
securePassword.AppendChar(c)
Next c

Related

How to run Powershell scripts in vb.net using WorkingDirectory and shellExecute = false

I need to run a PowerShell script in vb.net application with Alternate user credentials but i am not able to do so...
This is my Code:
Dim Prozess As ProcessStartInfo
Prozess = New ProcessStartInfo(MyPath\Auftragsstruktur.ps1")
Prozess.UserName = "MyUserName"
Prozess.Password = "MyPassword"
Prozess.Domain = "MyDomain"
Prozess.UseShellExecute = False
Prozess.WorkingDirectory = "C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe"
Prozess.WindowStyle = ProcessWindowStyle.Normal
Process.Start(Prozess)

VB.Net: Unable to get Standard Output from cmd Process.start over network

I'm encountering an issue with the Process.start() method in VB.Net.
I'm trying to execute an ipconfig /all and set a variable with the StandardOutput.ReadToEnd() method.
It works well if I execute the application locally, but when I place the EXE on a network share, my variable is not set with the result of the command.
It seems it cannot retrieve the output of the command.
Can you please help me?
Thank you in advance.
Here is my code:
Dim p As New Process
Dim psi As New ProcessStartInfo
psi.FileName = "cmd.exe"
psi.Arguments = "/c ipconfig /all"
psi.UseShellExecute = False
psi.RedirectStandardOutput = True
p.StartInfo = psi
p.Start()
Dim output As String = p.StandardOutput.ReadToEnd
p.WaitForExit()
MsgBox(output)

VB.NET Execute a form from a form with administrator username and password

I'm hoping to run a form using administrator (domain) credentials by a standard user.
I don't think I can run a single form without first running it as administrator but I'm wondering if you can click a button on a form and that action could launch another form with admin credentials built-in.
Dim ProcessStartInfo As New ProcessStartInfo("C:\path\to\app.exe")
With ProcessStartInfo
' The following properties run the new process as administrator
.UseShellExecute = True
.Verb = "runas"
.WindowStyle = ProcessWindowStyle.Normal
.CreateNoWindow = False
End With
Try
' Start the new process
Process.Start(ProcessStartInfo)
Catch ex As Exception
' The user did not allow the application to run as administrator
MessageBox.Show("Sorry, this application must be run as Administrator.", "", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
End Try
Can I get some input on whether this will work? I've pieced it together and haven't run it through any testing yet. It looks like it's running a .bat file. can this simply be changed to an exe??
Imports System.Diagnostics
Imports System.Security
'Password is a SecureString that needs to be built
Dim securePassword as New Security.SecureString()
For Each c As Char In userPassword
securePassword.AppendChar(c)
Next c
Dim proc as ProcessStartInfo = new ProcessStartInfo()
proc.FileName = "filelocation.bat"
proc.WorkingDirectory = "your_working_dir"
proc.UseShellExecute = False
proc.Domain = userDomain
proc.UserName = userName
proc.Password = securePassword
Process.Start(proc)

VB NET Read Remote Registry

I'm trying to get the architecture and the operating system of many remote pcs.
In order to do that i'm querying Win32_OperatingSystem and parsing the "Caption" for the O.S. and for the architecture im reading OSArchitecture .
In Windows XP this value does not exists, so i thought that reading the HKLM\SYSTEM\CurrentControlSet\Control\Session Manager\Environment\PROCESSOR_ARCHITECTURE
would have done the trick like this code:
Try
Dim co As New ConnectionOptions
co.Impersonation = ImpersonationLevel.Impersonate
co.Authentication = AuthenticationLevel.PacketPrivacy
co.EnablePrivileges = True
co.Username = username
co.Password = password
Dim scope As New ManagementScope("\\" & machine.Text & "\root\cimv2", co)
scope.Connect()
Dim environmentKey, asd2 As Microsoft.Win32.RegistryKey
Dim asd As String
environmentKey = RegistryKey.OpenRemoteBaseKey(RegistryHive.LocalMachine, machine.Text)
asd2 = environmentKey.OpenSubKey("SYSTEM\CurrentControlSet\Control\Session Manager\Environment", True)
asd = asd2.GetValue("PROCESSOR_ARCHITECTURE")
Debug.Print("asd: " + asd)
environmentKey.Close()
Catch ex As Exception
MessageBox.Show(ex.ToString)
End Try
My problem is: if im trying this code I get an System.Security.SecurityException: "Accessing remote registry not permitted"
I am, and i know the administrator username and password.
In fact if I run a simple cmdkey /add:targetname /user:username /pass:password
It works.
So why do I have to run a cmdkey /add even if i have alredy specified the username and password in the ConnectionOptions ??
P.S. Sorry for my bad English
This may very well be because remote registry access is not enabled on the target PC.
Even if you know the administrator credentials, remote access to the registry will not work if the feature isn't enabled on the target PC.
To enable it, see the following Microsoft Knowledge Base Article, which covers a variety of Windows Operating Systems: https://support.microsoft.com/en-us/kb/314837
All right, i got it:
Const HKEY_current_user As String = "80000002"
Dim options As New ConnectionOptions
options.Impersonation = ImpersonationLevel.Impersonate
options.EnablePrivileges = True
options.Username = ".\administrator"
options.Password = "my_password"
Dim myScope As New ManagementScope("\\" & RemotePCHostname & "\root\default", options)
Dim mypath As New ManagementPath("StdRegProv")
Dim mc As New ManagementClass(myScope, mypath, Nothing)
Dim inParams As ManagementBaseObject = mc.GetMethodParameters("GetDWORDValue")
inParams("hDefKey") = UInt32.Parse(HKEY_current_user,System.Globalization.NumberStyles.HexNumber) 'RegistryHive.LocalMachine
inParams("sSubKeyName") = "SYSTEM\CurrentControlSet\Control\Session Manager\Environment"
inParams("sValueName") = "PROCESSOR_ARCHITECTURE"
Dim outParams As ManagementBaseObject = mc.InvokeMethod("GetStringValue", inParams, Nothing)
If (outParams("ReturnValue").ToString() = "0") Then
MessageBox.Show(outParams("sValue").ToString())
Else
MessageBox.Show("Error retrieving value : " + outParams("ReturnValue").ToString())
End If

Run CMD Silently

I'm trying to run CMD silently, but each time I get an error. Could someone please tell me where I'm going wrong?
Dim myProcess As Process
myProcess.StartInfo.WindowStyle = ProcessWindowStyle.Hidden
myProcess.StartInfo.CreateNoWindow = True
myProcess.StartInfo.FileName = ("cmd.exe" & CmdStr)
myProcess.Start()
CmdStr is already a string to do certain things that I want in the application.
I suppose your cmdStr is a string with parameters for CMD.
If so you need to use the Arguments property of StartInfo.
You get a Null Exception on the myProcess variable because it is never instatiated with new.
You could create a ProcessStartInfo var to use with the static Process.Start method and set the UseShellExecute to False
Dim startInfo As New ProcessStartInfo("CMD.EXE")
startInfo.WindowStyle = ProcessWindowStyle.Hidden
startInfo.CreateNoWindow = True
startInfo.UseShellExecute = False
startInfo.Arguments = CmdStr
Process.Start(startInfo)
or edit your code to add
myProcess = new Process()
before using the var myProcess