Link to home
Start Free TrialLog in
Avatar of joseph_schuler
joseph_schuler

asked on

Rename machine using WMI in VB.Net

I am looking to call WMI to do a rename and join workgroup.  The following
functions compile cleanly but the renale doesn't seem to work:

Function objGetComputerSystem() As ManagementObject
        Dim objectQuery As New ObjectQuery("select * from
Win32_ComputerSystem")
        Dim searcher As New ManagementObjectSearcher(objectQuery)
        Dim computerSystem As ManagementObject
        Dim computerSystemOut As ManagementObject
        computerSystemOut = Nothing
        For Each computerSystem In searcher.Get()
            computerSystemOut = computerSystem
            Exit For
        Next computerSystem
        Return computerSystemOut
    End Function

    Function renameComputer(ByRef computerSystem As ManagementObject, ByVal
newName As String) As UInt32
        Dim rc As UInt32

        Dim objArgs(1) As Object
        objArgs(0) = newName
        rc = computerSystem.InvokeMethod("Rename", objArgs)
        Return rc

    End Function

    Function JoinWorkgroup(ByRef computerSystem As ManagementObject, ByVal
newWorkgroup As String) As UInt32
        Dim rc As UInt32

        Dim objArgs(5) As Object
        objArgs(0) = newWorkgroup
        objArgs(1) = vbNull
        objArgs(2) = vbNull
        objArgs(3) = vbNull
        objArgs(4) = vbNull


        rc = computerSystem.InvokeMethod("JoinDomainOrWorkgroup", objArgs)
        Return rc

    End Function

The objGetComputerSystem  works but the rename silenlly fails.  I havent
cheched the JoinWorkgroup function yet.  Any Ideas?
ASKER CERTIFIED SOLUTION
Avatar of graye
graye
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of joseph_schuler
joseph_schuler

ASKER

I knew i would have to reboot, but when the machine was part of a domain I also needed to unjoin it first and then join s workgroup, otherwise the rename would not take place.
see my last comment