尝试通过 netdom 命令重命名计算机并强制重启

尝试通过 netdom 命令重命名计算机并强制重启

为什么这不起作用?看起来好像可以,但是什么也没有发生。

Option Explicit

Dim wshNetwork
Dim wshShell
Dim PCname
Dim Newname

Set wshNetwork = WScript.CreateObject("WScript.Network")
Set wshShell = WScript.CreateObject("WScript.Shell")

PCname = InputBox("Type in the name of the pc you want to rename")
Newname = InputBox("Type in the name of the new pc name")

wshShell.run("netdom renamecomputer " &PCname& " /NewName:"&Newname& " /reboot:00 " )
'MsgBox("netdom renamecomputer " &PCname& " /NewName:"&Newname& " /reboot:00 /y")

答案1

我以前遇到过这种情况,虽然我知道如何通过 PowerShell 来解决,但如果您正在寻找 VBS,您可能需要尝试一下。

取自http://www.wisesoft.co.uk/scripts/vbscript_rename_domain_computers_from_csv_file.aspx

OPTION EXPLICIT
CONST adOpenStatic = 3
CONST adLockOptimistic = 3
CONST adCmdText = &H0001
CONST intWindowStyle = 7
CONST blnWait = TRUE
DIM strCSVFolder,strCSVFile,strNetDomParams
DIM objShell,cn,rs

' ************** Setup ************** 
' Folder where CSV File is located
' CSV file should have 1st field = oldname, 2nd field = newname with no header row
strCSVFolder = "C:\Temp\"
' CSV filename
strCSVFile = "test.csv"
' Additional parameters to pass to NetDom command
strNetDomParams = " /userd:DOMAIN\ADMINISTRATOR /passwordd:PASSWORD /usero:DOMAIN\ADMINISTRATOR /passwordo:PASSWORD /force "

'************************************ 

SET objShell = wscript.CREATEOBJECT("wscript.shell")

' Setup ADO Connection to CSV file
SET cn = CREATEOBJECT("ADODB.Connection")
SET rs = CREATEOBJECT("ADODB.Recordset")

cn.Open "Provider=Microsoft.Jet.OLEDB.4.0;" & _
          "Data Source=" & strCSVFolder & ";" & _
          "Extended Properties=""text;HDR=NO;FMT=Delimited"""

rs.Open "SELECT * FROM [" & strCSVFile & "]", _
          cn, adOpenStatic, adLockOptimistic, adCmdText

DO until rs.eof
    DIM strOldName, strNewName, strCmd,intReturn
    strOldName = rs(0)
    strNewName = rs(1)
    strCmd = "cmd.exe /C netdom renamecomputer " & strOldName & " /newname:" & strNewName & strNetDomParams

    intReturn = objShell.Run(strCmd,intWindowStyle,blnWait)

    IF intReturn = 0 THEN
        wscript.echo "Renamed '" & strOldName & "' to '" & strNewName & "'"
    ELSE
        wscript.echo "Error renaming '" & strOldName & "' to '" & strNewName & "'" 
    END IF

    rs.movenext
LOOP

答案2

进入 cmd 行总是很麻烦(另外你还得确保 netdom 在那里),处理 VB 中的引号等等。

可能更容易直接使用本机并使用 WMI,而且作为额外奖励,它应该可以在大多数地方使用。这也会加入域(您可以删除该部分),但您可以只保留重命名部分和重新启动部分,这样就好了。

这是我从另一个网站窃取的未经测试的代码。

Const JOIN_DOMAIN = 1
Const ACCT_CREATE = 2

Dim sCmpName
Dim sUser, sPassword, sDomain, sOU

sUser = "<domainuserid>"
sPassword = "<domainpassword>"
sDomain = "<mydomain>"
sOU = "<ou=myou,dc=mydomain,dc=com>"

sCmpName = InputBox("Enter the new computer name:", "Computer Name")
If sCmpName = "" Then
  Wscript.Echo "Exiting script."
  Wscript.Quit
End If

Dim oWMI, oCmp, oOS, sReturn

Set oWMI = GetObject("winmgmts:\\.\root\cimv2")
For Each oCmp in oWMI.InstancesOf("Win32_ComputerSystem")
  sReturn = oCmp.Rename(sCmpName)
  If sReturn <> 0 Then
    Wscript.Echo "Rename failed.  Error = " & Err.Number & _
      vbcrlf & "Exiting script."
  Else
    Wscript.Echo "Rename successful."
    sReturn = oCmp.JoinDomainOrWorkgroup(sDomain, sPassword, _ 
      sDomain & "\" & sUser, sOU, JOIN_DOMAIN+ACCT_CREATE)
    If sReturn <> 0 Then
      Wscript.Echo "Join domain failed.  Error = " & Err.Number & _
        vbcrlf & "Exiting script."
Else
  Wscript.Echo "Join domain successful."
End If
Wscript.Echo "Rebooting computer..."
For Each oOS in oWMI.InstancesOf("Win32_OperatingSystem")
  sReturn = oOS.Reboot
Next
End If
Next

答案3

如果你尝试部署虚拟机映像:你刚刚尝试运行sysprep^(1)?! 这还会强制(据我所知)系统重置 SID 信息,从而防止出现 WSUS 问题等等!?

相关内容