使用 PowerShell 一步重命名计算机并加入域 [英] Rename computer and join to domain in one step with PowerShell

查看:219
本文介绍了使用 PowerShell 一步重命名计算机并加入域的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

目标:在运行 Windows Server 2008 R2 的计算机上,使用 PowerShell 2.0 来:

Goal: On a computer running Windows Server 2008 R2, use PowerShell 2.0 to:

  1. 重命名计算机
  2. 将计算机加入域

条件:第 1 步和第 2 步必须一起执行,即它们之间不能重新启动计算机

Condition: Steps 1 and 2 must be performed together, i.e., without a computer restart between them

这些是我为每个步骤创建的 PowerShell 函数.

These are the PowerShell functions I've created for each step.

根据我在 Internet 上的研究,PowerShell 2.0 在发布前有一个名为 Rename-Computer 的内置 cmdlet,但由于未知原因在 CTP 3 中被删除.我的版本使用 WMI.

According to my Internet research, PowerShell 2.0 at one point before release had a built-in cmdlet called Rename-Computer, but it was removed for reasons unknown in CTP 3. My version uses WMI.

function Rename-Computer
{
    param ( [Parameter(Mandatory=$true)][string]$name )

    process
    {
        try
        {
            $computer = Get-WmiObject -Class Win32_ComputerSystem
            $result = $computer.Rename($name)

            switch($result.ReturnValue)
            {       
                0 { Write-Host "Success" }
                5 
                {
                    Write-Error "You need administrative rights to execute this cmdlet" 
                    exit
                }
                default 
                {
                    Write-Host "Error - return value of " $result.ReturnValue
                    exit
                }
            }
        }
        catch
        {
            Write-Host "Exception occurred in Rename-Computer " $Error
        }
    }
}

将计算机加入域

如您所见,此函数实际上只是内置 cmdlet Add-Computer 的包装器,用于收集域名并创建一些要使用的凭据.

Join Computer to Domain

As you can see, this function is really just a wrapper for the built-in cmdlet Add-Computer that gathers the domain name and creates some credentials to use.

function Join-ComputerToDomain
{
    param ( [Parameter(Mandatory=$true)][string]$domain )

    process
    {
        try
        {
            $_domainCredential = $Host.UI.PromptForCredential("Enter domain credentials", "Enter domain credentials to be used when joining computer to the domain", "", "NetBiosUserName")
            Add-Computer -DomainName $_domain -cred $_domainCredential
        }
        catch
        {
            Write-Error "Exception occurred in Join-ComputerToDomain " $Error
        }
    }
}

我尝试过的步骤

尝试 1

  1. 调用重命名计算机
  2. 调用Join-ComputerToDomain
  3. 重启

结果: Rename-Computer 的输出表明名称已更改,但重启后名称更改,但计算机加入到域

Result: Output from Rename-Computer indicates that name was changed, but after restart, name did not change, but computer was joined to domain

  1. 调用Join-ComputerToDomain
  2. 调用重命名计算机
  3. 重启

结果: Rename-Computer 的返回值为 1326(登录失败:未知用户名或错误密码).我认为这是因为一旦加入域,重命名就需要域凭据.我尝试在 Rename-Computer 中通过 Get-WmiObject 调用使用凭据,但它引发了一个关于无法在本地系统上使用不同凭据的错误.

Result: Return value from Rename-Computer is 1326 (Logon failure: unknown user name or bad password). I assume this is because domain credentials are required for the rename once it's joined to the domain. I attempted to use credentials with the Get-WmiObject call in Rename-Computer, but it threw an error about not being able to use different credentials on the local system.

  1. 调用重命名计算机
  2. 重启
  3. 调用Join-ComputerToDomain
  4. 重启

结果:一切正常,但需要额外重启.有效,但我想取消第 2 步的重新启动.

Result: Everything works as expected, but extra restart required. Works but I want to eliminate the restart at step 2.

推荐答案

你可以直接使用Add-Computer,有一个参数-NewName"

You can just use Add-Computer, there is a parameter for "-NewName"

示例:Add-Computer -DomainName MYLAB.Local -ComputerName TARGETCOMPUTER -newname NewTARGETCOMPUTER

您可能还想检查参数-OPTIONS"

You might want to check also the parameter "-OPTIONS"

http://technet.microsoft.com/en-us/library/hh849798.aspx

这篇关于使用 PowerShell 一步重命名计算机并加入域的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆