Click here to Skip to main content
15,903,838 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

I have a task to create an semi automated GUI to remove a computer from the domain and then rename it (to free up the existing name for a hardware swap-out scenario)

Now I have though along the idea of creating scripts that use NETDOM to remove the machine from the domain that will be run via the run once option in the registry after a reboot, but would rather have a cleaner option for VB to do it from the GUI interface using the admin users credentials. Once the Computer is removed from the domain and in a workgroup (of any name) then the computer can be renamed to the correct name and then rejoined to the Domain.

This might sound a bit long winded but machines are built with temporary names on the domain and when a hardware swap-out is required the temp computer name is removed from the domain, the computer renamed to the name of the computer being swapped out and then rejoined to the domain.

In a nutshell, I need to remove a remote computer from a domain and rename it.

I hope I'm making sense....
Posted

Does this[^] or this[^] help?
I found these by googling "programmatically rename a remote computer". The results above were on the first page.
 
Share this answer
 
Thanks for that, but both these solutions are C# I was looking for a VB solution.

I have trawled though google and found ideas that do some but not all... I will look at the C# solutions and see if I can adapt them to VB, but C# is not my language...

Thanks again.
 
Share this answer
 
Comments
Simon_Whale 12-Oct-11 10:49am    
There are many converters for such a task e.g. http://converter.telerik.com/

Most of the samples that you see on the internet come in the form of C# code. It is very easy to convert 99% of the time.

This is coming from someone that develops mainly in VB.NET where I work.
I have managed to sort this here's my code if anyone needs it. This works on remote machines as long as the access rights are correct.

VB
Imports System
Imports Microsoft.Win32
Imports System.Management
Imports System.Management.Instrumentation

        Try 'Remove from Domain

            Dim ComputerName as string = "" 'Insert Computer Name Here
            Dim ConnectionOpt As New ConnectionOptions()
            ConnectionOpt.Authentication = AuthenticationLevel.PacketPrivacy
            Console.WriteLine("Connecting to " & ComputerName & " WMI Namespace")

            Dim scope As New ManagementScope("\\" & ComputerName & _
            "\root\cimv2", ConnectionOpt)
            scope.Connect()
            Console.WriteLine("Connection Succeeded")
            Dim classInstance As New ManagementObject(scope, _
            New ManagementPath("Win32_ComputerSystem.Name='" _
            & ComputerName & "'"), Nothing)
            Dim inParams As ManagementBaseObject = _
            classInstance.GetMethodParameters("unjoindomainorworkgroup")
            Console.WriteLine("Removing " & ComputerName  & _
            " from Domain and adding to Workgroup")
            inParams("FunjoinOptions") = 1
            inParams("Password") = "xxxxxxxx" ' Password Here
            inParams("UserName") = "xxxxxxxx" ' Local Admin User

            Dim outParams As UInt32 = _
            classInstance.InvokeMethod("unjoindomainorworkgroup", Nothing)
            Console.WriteLine("Removal Succeeded.")
        Catch err As ManagementException
            Console.WriteLine("Removal Failed!")
            Console.WriteLine("Error: " & err.Message)
        End Try

        Try 'Changes Computer Name
            Console.WriteLine("Connecting to " & ComputerName & " WMI Namespace")
            Dim ConnectionOpt As New ConnectionOptions()
            ConnectionOpt.Authentication = AuthenticationLevel.PacketPrivacy

            Dim scope As New ManagementScope("\\" & ComputerName & _
            "\root\cimv2", ConnectionOpt)
            scope.Connect()
            Console.WriteLine("Connection Succeeded")
            Console.WriteLine("Renaming " & ComputerName & " to " _
            & NewComputerName ")
            Dim classInstance As New ManagementObject _
            (scope, New ManagementPath("Win32_ComputerSystem.Name='" & _
            ComputerName & "'"), Nothing)

            Dim inParams As ManagementBaseObject = _
            classInstance.GetMethodParameters("Rename")
            inParams("Name") = "NewComputerName"
            inParams("Password") = "xxxxxxxx" 'Local Admin Password
            inParams("UserName") = "xxxxxxxx" 'Local Admin User

            Dim outParams As ManagementBaseObject = _
            classInstance.InvokeMethod("Rename", inParams, Nothing)
            Console.WriteLine("Rename Successful")
        Catch err As ManagementException
            Console.WriteLine("Rename Failed!")
            Console.WriteLine("Error: " & err.Message)
        End Try

        Console.WriteLine("")
        Console.WriteLine("Workstation Must Be Restarted!")
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900