0

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?

  • Most (if not all) solutions that I could find seem to require a computer restart for the changes to take effect. Did you try that as well? – Visual Vincent Feb 11 '18 at 10:18
  • @VisualVincent yeah, every try that i have done i restarted my pc – Antonio Panella Feb 11 '18 at 10:36
  • And yet it didn't work? It's hard to suggest anything when we don't know what you've tried... I've found plenty of Stack Overflow posts with different approaches to changing the name, though all requires a computer restart. – Visual Vincent Feb 11 '18 at 10:40
  • @VisualVincent i've update my post with the solution that i'm testing – Antonio Panella Feb 11 '18 at 10:55
  • And what happens when you run it? Do you get any errors? Also, why are you setting the user name and password to null? – Visual Vincent Feb 11 '18 at 11:09
  • [This question](https://stackoverflow.com/q/7646669) and its accepted answer could be of interest. It's in C#, but should be easy to convert using an [online converter](http://converter.telerik.com/). – Visual Vincent Feb 11 '18 at 11:14
  • https://msdn.microsoft.com/en-us/library/aa393056(v=vs.85).aspx if i have understood if i set null, the application use the context of the caller – Antonio Panella Feb 11 '18 at 11:18
  • Null = `Nothing` in VB.NET, which they should already be set to by default. You shouldn't have to change them at all. You might also want to check the return value when you execute the `Rename` function. Some times it returns an error code which could help you identify the issue. – Visual Vincent Feb 11 '18 at 11:20
  • @VisualVincent thank you vincent but another way had success, the post is updated. – Antonio Panella Feb 11 '18 at 15:24
  • 2
    http://www.pinvoke.net/default.aspx/kernel32.setcomputernameex – Hans Passant Feb 11 '18 at 15:41

0 Answers0