Perhaps someone has written such a script before that does this job and
knows what is wrong.
----------------------------------
1. function to rename host
----------------------------------
Sub changeHostname(hostname)
Dim WSH, FSO, strCCS, strCompNameRegPath, strTcpipParamsRegPath
Set WSH = CreateObject ("WSCript.shell")
Set FSO = CreateObject("Scripting.FileSystemObject")
hostname = UCase(Trim(hostname))
'Computername
strCCS = "HKLM\SYSTEM\CurrentControlSet\"
strTcpipParamsRegPath = strCCS & "Services\Tcpip\Parameters\"
strCompNameRegPath = strCCS & "Control\Computername\"
'Delete current settings in registry and write new ones
With WSH
.RegWrite strCompNameRegPath & "Computername\Computername", hostname
.RegWrite strTcpipParamsRegPath & "NV Hostname", hostname
'.RegWrite strCompNameRegPath & "ActiveComputername\Computername", hostname
'.RegWrite strTcpipParamsRegPath & "Hostname", hostname
End With ' WSH
Set WSH = Nothing
Set FSO = Nothing
End Sub
----------------------------------
2. function to join the domain
----------------------------------
Function joinDomain()
Dim Hostname
Dim WSH
Set WSH = CreateObject("WScript.Shell")
Hostname =
WSH.RegRead("HKLM\SYSTEM\CurrentControlSet\Control\Computername\ActiveComputername\Computername")
Set WSH = Nothing
Const Domain = "domain"
Const Administrator = "admin"
Const AdminPassword = "password"
If(Trim(Hostname) = "") Then
' get computername
Dim WSHNetwork
Set WSHNetwork = CreateObject("WScript.Network")
Hostname = WSHNetwork.ComputerName
End If
' create bitfield
Const JOIN_DOMAIN = 1
Const ACCT_CREATE = 2
Const ACCT_DELETE = 4
Const WIN9X_UPGRADE = 16
Const DOMAIN_JOIN_IF_JOINED = 32
Const JOIN_UNSECURE = 64
Const MACHINE_PASSWORD_PASSED = 128
Const DEFERRED_SPN_SET = 256
Const INSALL_INVOCATION = 262144
' join domain
Dim objComputer
Set objComputer =
GetObject("winmgmts:{impersonationLevel=Impersonate}!\\.\root\cimv2:Win32_ComputerSystem.Name='" & Hostname & "'")
WScript.Echo("Now joining domain " & Domain & "...")
WScript.Echo("Username: " & Domain & "\" & Administrator)
WScript.Echo("Password: " & "<hidden>")
Dim result
result = objComputer.JoinDomainOrWorkgroup(Domain, AdminPassword, Domain &
"\" & Administrator, Null, JOIN_DOMAIN + ACCT_CREATE + DOMAIN_JOIN_IF_JOINED)
joinDomainAs = result
WScript.Echo("Result: " & result)
'Error-Codes:
'http://msdn.microsoft.com/library/default.asp?url=/library/en-us/netmgmt/netmgmt/network_management_error_codes.asp
End Function
Are you sure you remembered to UNJOIN it before you re-joined it?
--
Gerry Hickman (London UK)
> Hi,
> the workstation is not a member of a domain before I try to join one, it's a
> member of a workgroup. Do I have to unjoin this before joining a domain or an
> other workgroup?
OK sorry, in that case my comment wasn't useful. Are you sure you
rebooted the computer after renaming it, BEFORE you tried to join it to
the domain and then rebooted it after you joined it before trying to log
back in?
I do this kind of thing a lot and never had any problems.
> Well, this works
Aha, there it is then.
> but it would be better not to reboot before joining the
> domain. Doing this directly with the given Windows Tools also works without
> the first reboot. So it has to be possible to do the same with a WSH-Script.
What do you mean by "given Windows tools"? Do you mean the GUI? I kind
of know what you mean, it let's you rename and join at the same time?
Thing is I don't recommend doing it that way, but I haven't used a GUI
for a long time.
"Gerry Hickman" wrote:
> Hi Lutz,
>
> > Well, this works
>
> Aha, there it is then.
>
> What do you mean by "given Windows tools"? Do you mean the GUI? I kind
> of know what you mean, it let's you rename and join at the same time?
> Thing is I don't recommend doing it that way, but I haven't used a GUI
> for a long time.
>
> --
> Gerry Hickman (London UK)
>
Hi Gerry,
Yes I mean the GUI. Also some other tools do this 2 jobs at the same time.
I've also asked for help in some other communities and they said that I
should do this 2 reboots. Now I'll do it the way you mentioned and registered
the script to join the domain to be executed automatically after the reboot
(HKLM\SYSTEM\..\RunOnce). The problem is that I have to login as
Administrator to make Windows call the script. Is there an other way to let
Windows execute the script before any logon?
Thanks for help
Lutz Schoenemann