Change machine name from code, win7 - vb.net

i'm writing a little utility for my job, i need to change the actual machine name into serial number of the machine, i already found how to get serial number but i have no idea how to set the machine name and every solution i have tried haven't worked, someone have some suggestions?
Dim q As New SelectQuery("Win32_bios")
Dim search As New ManagementObjectSearcher(q)
Dim info As New ManagementObject
Try
For Each info In search.Get
Call MessageBox.Show("Serial Number: " & info("serialnumber").ToString +
vbCrLf + "Machine Name : " + Environment.MachineName)
Next
Return 1
Catch err As ManagementException
Call MessageBox.Show("Error: " & err.Message)
Return -99
End Try
I found a solution
Dim p As Process = New Process()
Dim pi As ProcessStartInfo = New ProcessStartInfo()
pi.Verb = "Runas"
pi.WindowStyle = ProcessWindowStyle.Hidden
pi.Arguments = "/K WMIC computersystem where caption='" + Environment.MachineName + "' rename Prova"
pi.FileName = "cmd.exe"
pi.UseShellExecute = True
pi.CreateNoWindow = True
Process.Start(pi)
It's just an utility for me but there is a way if i don't want ask the permission to run the process?

Related

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

VB.Net shell output sometimes not returning

for 99% of all my Shell commands this code works fine, but for just one command I don't get the returned text, which is normally visible in the Shell window. Here is the code I use to execute commands and read back the results:
Function RunCommandCom(command As String, arguments As String) As String()
Dim p As Process = New Process()
Dim pi As ProcessStartInfo = New ProcessStartInfo()
Dim output As String
pi.Arguments = " " + "/c" + " " + command + " " + arguments
pi.FileName = "cmd.exe"
pi.RedirectStandardOutput = True 'pi.CreateNoWindow = True
pi.WindowStyle = ProcessWindowStyle.Normal
pi.UseShellExecute = False
p.StartInfo = pi
p.Start()
output = p.StandardOutput.ReadToEnd()
p.WaitForExit()
p.Close()
If output Is ""
Then
Return Nothing
Else
Return output.Replace(Chr(13), "").Split(Chr(10))
End If
End Function
The command making Problems is executed fine. It shall create a entry in a database, which defintly is existing after calling my funciton. Executing the command directly in a Shell generates the same entry and I can see the returned text.
Does anyone has an idea why the streamreader does not read/contain anything for exactly this one special command?
Arg. I found the problem. These two commands send their output to the StandardError stream. I don't know why, as no error occured.

VB.Net ProcessStartInfo not working in Windows Service

I want to stop a windows service in a different service.
When I run the below code in a desktop app, it works correctly but same code doesn't work in a Windows Service app.
Is somebody has any idea?
PS: I tried to use SC stop instead of net stop but the result is exactly the same.
Dim p As Process = New Process()
Dim pi As ProcessStartInfo = New ProcessStartInfo()
pi.Arguments = " " + "/K" + " " + "net stop MyService"
pi.CreateNoWindow = True
pi.UseShellExecute = True
pi.FileName = "cmd.exe"
pi.Verb = "runas"
p.StartInfo = pi
p.Start()

Displaying the output of Process.Start

I have a Process.Start command that I would like to see the output of, but the new window is opening and closing too quickly for me to see anything. Here is the code I have so far that I'm working with:
System.Diagnostics.Process.Start(Environment.GetEnvironmentVariable("VS110COMNTOOLS") & "..\Ide\MSTEST.EXE", "/Testsettings: """ & rwSettings & "" & " /Testcontainer: """ & rwContainer & "" & " /Resultsfile: """ & rwResults & "")
Unfortunately as I try to debug this if I allow this to run it flashes up the window but doesn't let me see what the error is, or if it's running successfully at all. I'm using VS2012 so I might just not be looking at the right view when I'm debugging.
Here is some code taen out of the middle of some logic, so it is not standalone. You can use ProcessStartInfo() and Process() to have more control:
Dim start_info As New ProcessStartInfo("sqlcmd", cmd)
start_info.UseShellExecute = False
start_info.CreateNoWindow = True
start_info.RedirectStandardOutput = True
start_info.RedirectStandardError = True
' Make the process and set its start information.
Dim proc As New Process()
proc.StartInfo = start_info
Dim dt As Date = Now()
' Start the process.
proc.Start()
' Attach to stdout and stderr.
Dim std_out As StreamReader = proc.StandardOutput() ' will not continue until process stops
Dim std_err As StreamReader = proc.StandardError()
' Retrive the results.
Dim sOut As String = std_out.ReadToEnd()
Dim sErr As String = std_err.ReadToEnd()

Using "process" to call another program. Wanting to pass info back to parent program

I know how to send parameters to the child program via startinfo.arguments. I think I even know how to "listen" to anything the child might "say" via standardoutput.readline. What I don't know is what method to use for the child to "speak" back to the parent. Here is the code I have so far (on the parent side):
Dim proc As Process
Dim bRunProgramWorked As Boolean = True
Try
proc = New Process
Dim procInfo As New ProcessStartInfo
proc.StartInfo.FileName = strPathUpgradeEXE
proc.StartInfo.Arguments = param1 & " " & param2 & " " & param3
proc.StartInfo.WindowStyle = ProcessWindowStyle.Hidden
'proc.StartInfo.WindowStyle = ProcessWindowStyle.Maximized
proc.StartInfo.UseShellExecute = False
proc.StartInfo.CreateNoWindow = True
proc.StartInfo.RedirectStandardOutput = True
'proc.StartInfo.RedirectStandardError = True
proc.Start()
Dim output1 As String = proc.StandardOutput.ReadLine
Dim output2 As String = proc.StandardOutput.ReadLine
proc.WaitForExit()
proc.Close()
proc.Dispose()
Catch ex As Exception
LogIt(strMyBaseID, "Error at trying to run PVT-Export. " & ex.ToString)
bRunProgramWorked = False
End Try
The child process needs to write to Console.Out.