无法使用自动PowerShell脚本加入域 - “无法更新密码”

时间:2015-08-18 15:34:20

标签: ansible amazon-cloudformation

我正在尝试使用Powershell脚本向域添加主机。当通过CloudFormation或Ansible调用脚本时,脚本失败并显示以下错误。当我在主机上手动运行它时,它会成功。

我怀疑我做错了用户(我手动运行管理员)所以我试图强制它一直以管理员身份运行。不幸的是,这也没有用。

之前有没有人见过这个问题?

错误:

> [DEBUG] Command 4-add-to-domain output: Add-Computer : Computer
> 'WIN-xxxxx' failed to join domain 
> 
> 'aws.cloud.bp.com' from its current workgroup 'WORKGROUP' with
> following error 
> 
> message: Unable to update the password. The value provided as the
> current 
> 
> password is incorrect.
> 
> At line:1 char:1
> 
> + Add-Computer -DomainName $domain -Credential $credential -OUPath $ouPath 
> 
> -Restar ...
> 
> + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
> 
> ~~~
> 
>     + CategoryInfo          : OperationStopped: (WIN-K9DU7TO9331:String) [Add- 
> 
>    Computer], InvalidOperationException
> 
>     + FullyQualifiedErrorId : FailToJoinDomainFromWorkgroup,Microsoft.PowerShe 
> 
>    ll.Commands.AddComputerCommand

PS1:

if ((gwmi win32_computersystem).partofdomain -eq $true)
{ 
    write-host "already in domain" 
}
else
{
    $domain = $domainname
    $password = $password | ConvertTo-SecureString -asPlainText -Force
    $username = $uid
    $credential = New-Object System.Management.Automation.PSCredential($username,$password)
    $ouPath = $oupath
    $cmd = 'Add-Computer -DomainName $domain -Credential $credential -OUPath $ouPath -Restart'
    $runas = [Security.Principal.WindowsPrincipal][Security.Principal.WindowsIdentity]::GetCurrent()

    if (-NOT ([Security.Principal.WindowsPrincipal][Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole] "Administrator"))
    {   
        $log = "not running as admin"
        $log | out-file -Filepath $logger -append
    } else {
        $log = "running as admin, about to run $cmd"
        $log | out-file -Filepath $logger -append
        Invoke-Expression -Command $cmd

    }
}

3 个答案:

答案 0 :(得分:2)

取自:http://www.gi-architects.co.uk/2017/01/powershell-add-computer-error-when-executed-remotely/

问题的根源是(鉴于您的密码是正确的)当以交互方式运行时,域是预先附加的,因此您只需要提供用户。但是在非交互式环境中,域名不是这样的,这是一个非常简单的修复,请确保您包含短域名,如“contoso \ DMAdmin”或完整的FQDN“DMAdmin@contoso.com。

答案 1 :(得分:0)

您是否在没有ConvertTo-SecureString的情况下尝试过密码?只是为了测试它?当我尝试远程更改我的域上的本地管理员密码时,我遇到了一些问题。从我可以收集的内容,当您将其转换为安全字符串然后将其指定为变量时,实际密码似乎会丢失。现在,我是powershell的真正新手,但我只能在直接调用ConvertTo-SecureString时才能工作,而不是作为变量。

我不确定为什么当你手动运行它时会发挥作用,但这就是我首先尝试并尝试隔离问题的方法。如果我离开,请随时纠正我。

答案 2 :(得分:0)

我有一个要求,需要将 azure VMS 加入我们的 AD 域。我创建了一个 Powershell 脚本并将其存储在一个存储帐户中。我使用自定义脚本扩展作为将调用脚本的 ARM 模板中的 VM 资源。出于某种原因,我的 ARM 模板配置了 VM,但它没有加入域。该脚本有一个名为 DomainAdminUsername 的参数,它将采用域管理员的用户名。为了使它工作,我使用 domain\username 而不是 username 作为它的值。有效。我发现它引用了本地管理员帐户来运行脚本。在提供用户名时,我们必须明确使用域前缀。