Hostname ändern per Batch/Reg/VBS

Sylar

Commander
Registriert
März 2011
Beiträge
2.955
Hallo!

Ich habe folgende Aufgaben...Ich bekomme immer PC's mit dem Namen "XName". Jetzt bräuchte ich eine Batch/Reg Datei, der mir den Aktuellen Computername, also "XName" ausliest und dann es umbennent in "Name". Und dann noch ein Fenster für ein Reboot bestätigen wäre ideal.

Sprich ich möchte nur mehr ein Skript ausführen, der mir den Hostname ändert..
 
Wie wäre es mit:
alles ungetestet :D
Code:
@ECHO OFF
SET PCNAME="Mein_PC"
REG ADD HKLM\SYSTEM\CurrentControlSet\Control\ComputerName\ComputerName /v ComputerName /t REG_SZ /d %PCNAME% /f
shutdown -r -f -t 10
oder so in der Art.... ansonsten
Zusätzlich noch der msdn Link zu Registry-Keys bearbeiten.

Zum auslesen kannst du:
Code:
FOR /F "TOKENS=3" %%A IN ('REG QUERY "HKLM\SYSTEM\CurrentControlSet\Control\ComputerName\ComputerName"^|findstr "ComputerName"') DO SET PC-NAME=%%A
ECHO %PC-NAME%
verwenden.
 
Zuletzt bearbeitet: (Auslesen überarbeitet)
Danke, der 1. Link war schon sehr hilfreich, aber ich kann wohl erst nächste W

Code:
Option Explicit
Set ws =  CreateObject("WScript.Shell")

Dim ws, title, reg_key, comp_name, new_name
Dim itemtype


reg_key = "HKLM\SYSTEM\CurrentControlSet\Control\ComputerName\ComputerName\"
comp_name = ws.RegRead(reg_key & "ComputerName")
title = "Change Computer Name"
MsgBox(comp_name)
if Left(comp_name,1) = "X" Then
	MsgBox("OK")
	new_name = Replace(comp_name,"X","")
	ws.RegWrite reg_key & "ComputerName", new_name
	MsgBox(new_name) 
Else
	MsgBox("Kein X-Host")
End If

Bei mir funktioniert es mit der If Bedingung, nur ob der dann auch ins registry schreibt ist halt noch eine andere frage^^
 
Code:
Option Explicit
Set wShell =  CreateObject("WScript.Shell")

Dim wShell, title, reg_key,sTcpipParamsRegPath, sCompNameRegPath, comp_name, new_name
Dim itemtype


reg_key = "HKLM\SYSTEM\CurrentControlSet\Control\ComputerName\ComputerName\"

sTcpipParamsRegPath = "HKLM\SYSTEM\CurrentControlSet\Services\Tcpip\Parameters\"
sCompNameRegPath = "HKLM\SYSTEM\CurrentControlSet\Control\ComputerName\"
comp_name = wShell.RegRead(reg_key & "ComputerName")

title = "Change Computer Name"
MsgBox(comp_name)
if Left(comp_name,1) = "W" Then
	MsgBox("OK")
	new_name = Replace(comp_name,"W","")

	With wShell
	.RegDelete sTcpipParamsRegPath & "Hostname\"
	.RegDelete sTcpipParamsRegPath & "NV Hostname\"

	.RegWrite sCompNameRegPath & "ComputerName\ComputerName", sNewName
	.RegWrite sCompNameRegPath & "ActiveComputerName\ComputerName", sNewName
	.RegWrite sTcpipParamsRegPath & "Hostname", sNewName
	.RegWrite sTcpipParamsRegPath & "NV Hostname", sNewName

	End With ' wShell 


	ws.RegWrite reg_key & "Computername", new_name
	MsgBox ("Computername wurde in "+ new_name+ " geändert. Der Computer muss neu gestartet werden")
wShell.RegWrite reg_key & "ComputerName", new_name

Else
	MsgBox("Kein X-Host")
End If

Leider sagt er mir in der Zeile 21, Zeichen 2 folgender Fehler
Ungültige Wurzel in Registirerungsschlüssel "HKLM\SYSTEM\CurrentControlSet\Services\Tcpip\Parameters\"
Code: 80070005
Quelle: WshShell.RegDelete

Im Prinzip funktioniert bei mir alle .RegDelete und .RegWrite befehle gar nicht.. Was stimmt da nicht. Hab es ja 1:1 kopiert vom oberen Link
 
Zurück
Oben