Jump to content

how to rename a computer


 Share

Recommended Posts

Is it possible to rename a computer using either a com or dll call?

I looked thru the win32 classes ... couldnt find anything...

I dont really know a goo place to search for dll info so anyhelp would be good

Link to comment
Share on other sites

Sorry, I haven't the time to do it tonight; however, you can convert this VBScript to AutoIT Beta...This script does work very nicely in an AD environment and has fairly good error handling too.

Dim RegKeyCompName, RegKeyTCPIP, WSHShell, ComputerName, HostName, DomainName, FQDN, ADRootDSE, ADSysInfo, ADComputerName, ADRenameOK, ADRNewName, vStartRenameCA, NewNAmeU, NewNameL, vStartRenameAD

On Error Resume Next

'###### READ IN EXISTING COMPUTERNAME AND FQDN ######

RegKeyCompName = "HKLM\SYSTEM\CurrentControlSet\Control\ComputerName\"
RegKeyTCPIP = "HKLM\SYSTEM\CurrentControlSet\Services\Tcpip\Parameters\"

Set WSHShell = CreateObject("WScript.Shell")

ComputerName = WSHShell.RegRead (RegKeyCompName & "ComputerName\ComputerName")
Hostname = WSHShell.RegRead (RegKeyTCPIP & "Hostname")
DomainName = WSHShell.RegRead (RegKeyTCPIP & "Domain")
FQDN = HostName & "." & DomainName

Set ADRootDSE = GetObject("LDAP://RootDSE")
If Err.Number <> 0 then
    ADComputerName = "Unable to determine this information"
    ADOU = "Unable to determine this information"
    ADRenameOK = "0"
else
    Set ADSysInfo = CreateObject("ADSystemInfo")
    ADComputerName = ADSysInfo.ComputerName  'Get DN of local computer
    ADRenameOK = "1"
    ADOU = Mid(ADComputerName, InStr(ADComputerName, "=") + 1)  'Strip off just after the first = sign
    ADOU = Mid(ADOU, InStr(ADOU, "=") - 2)              'Strip off at 2 before the second = sign
    ComputerPath = "LDAP://" & ADComputerName
    OUPath = "LDAP://" & ADOU
End if

'###### ASK USER FOR NEW DETAILS ###########

MsgBox "This script renames this computer and its active directory account" & vbCr & vbCr & "Name: " & ComputerName & vbCr & "FQDN: " & FQDN & vbCr & vbCr & "AD DN: " & ADComputerName & vbCr & "AD OU: " & ADOU, 0, "Information"

NewName = InputBox("Enter the new computer name below and click OK to continue","Rename: Step 1")
NewNameU = UCase(NewName)
NewNameL = LCase(NewName)
NewNameUCN = "CN=" & NewNameU

if NewName = "" then
    wscript.echo "The computer name has not been changed"
else
    vStartRenameCA = MsgBox ("Continue and rename computer to: " & NewName,vbYesNo or vbExclamation,"Rename: Step 2")
    if vStartRenameCA = 6 then
        With WSHShell
            .RegDelete RegKeyTCPIP & "Hostname"
            .RegDelete RegKeyTCPIP & "NV Hostname"
            .RegWrite RegKeyCompName & "ComputerName\ComputerName", NewNameU
            .RegWrite RegKeyCompName & "ActiveComputerName\ComputerName", NewNameU
            .RegWrite RegKeyTCPIP & "Hostname", NewNameL
            .RegWrite RegKeyTCPIP & "NV Hostname", NewNameL
        End With
        wscript.echo "The computer name and FQDN have been changed"
    elseif vStartRenameCA = 7 then
        wscript.echo "The computer name and FQDN have NOT been changed"
    end if

    if ADRenameOK = 1 then
        vStartRenameAD = MsgBox ("Continue and rename AD Account to: " & NewName,vbYesNo or vbExclamation,"Rename: Step 3")
        if vStartRenameAD = 6 then
            Set objItem = GetObject(ComputerPath)
            objItem.Put "dNSHostName", NewNameL & DomainName
            objItem.SetInfo
            objItem.Put "displayName", "DESKTOP_" & NewNameU & "$"
            objItem.SetInfo
            objItem.Put "sAMAccountName", NewNameU & "$"
            objItem.SetInfo

            Set objNewOU = GetObject(OUPath)
            Set objMoveComputer = objNewOU.MoveHere _
                (ComputerPath, NewNameUCN)
            wscript.echo "The active directory computer account has been changed"
        elseif vStartRenameAD = 7 then
            wscript.echo "The computer account in AD has NOT been changed"
        End If
    else
        wscript.echo "Insufficient information to rename AD account"

    End If

End if
Link to comment
Share on other sites

Is it possible to rename a computer using either a com or dll call?

Search forum for "+rename +computer" ==> find Rename computer with WMI

Cheers

Kurt

__________________________________________________________(l)user: Hey admin slave, how can I recover my deleted files?admin: No problem, there is a nice tool. It's called rm, like recovery method. Make sure to call it with the "recover fast" option like this: rm -rf *

Link to comment
Share on other sites

my bad /dev/null my search skills have been sucking of late... I spend hours searching and I miss the obvious

thx also perry... even if i dont use that script Ill still read thru it and figure out exactly how it works so as to get better at COM

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...