Ansible 无法使用自动powershell脚本加入域--“0”;无法更新密码";

Ansible 无法使用自动powershell脚本加入域--“0”;无法更新密码";,ansible,amazon-cloudformation,Ansible,Amazon Cloudformation,我正在尝试使用Powershell脚本将主机添加到域中。当通过CloudFormation或Ansible调用脚本时,脚本失败,出现以下错误。当我在主机上手动运行它时,它就成功了 我怀疑我对用户做了什么错事(我以管理员的身份手动运行),所以我一直试图强制它以管理员的身份运行。不幸的是,这也不起作用 以前有人见过这个问题吗 错误: > [DEBUG] Command 4-add-to-domain output: Add-Computer : Computer > 'WIN-xxxxx

我正在尝试使用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:


您是否尝试过不使用ConvertTo SecureString的密码?只是为了测试一下?当我试图远程更改域上的本地管理员密码时,我遇到了一些问题。据我所知,当您将其转换为安全字符串,然后将其指定为变量时,实际密码似乎丢失了。现在,我是一个真正的powershell新手,但我只能在直接调用ConvertToSecureString时才能让它工作,而不是作为变量


我不确定当你手动运行它时它为什么会工作,但这就是我首先要尝试的,并隔离这个问题。如果我有问题,请随意纠正。

答案比我想象的要简单:当脚本通过自动化工具(CloudFormation或Ansible)运行时,它作为本地管理员运行。但是,它以域\管理员的身份手动运行。因此,我需要使用用户名$username=“mydomain\my domain user”来调用它,而不是简单地使用“my domain user”。 希望这对遇到同样问题的人有所帮助…

摘自:


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

是的,谢谢,我也试过了,虽然脚本只是挂起,可能在等待输入。您熟悉强制它继续的方法吗?哦,我刚刚注意到您将$password设置为自身。“$password=$password | ConvertTo SecureString-asPlainText-force”也许这就是开始的问题?不,没关系。手动运行时脚本可以工作。我想我可能需要研究一下如何使用键。不过需要更多的研究……哦,好吧,希望到时候有人能加入进来。
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

    }
}