【问题标题】:Moving client to a workgroup when the domain machine account doesn't exist当域计算机帐户不存在时将客户端移动到工作组
【发布时间】:2014-05-07 17:56:19
【问题描述】:

我正在我们的测试环境中进行一些自动化,我们有 powershell 脚本来将 Windows 客户端加入域或工作组。

如果域中不存在客户端的计算机帐户,我在尝试将 Windows 7 客户端从域移动到工作组时遇到问题。

代码如下:

$User = administrator
$Password = ConvertTo-SecureString "<password>" -AsPlainText -Force
$DomainCred = New-Object System.Management.Automation.PSCredential $User, $Password
remove-computer -credential $DomainCred -force -passthru -verbose

这是返回的错误:

VERBOSE: Performing operation "Remove-Computer" on Target "localhost".

Remove-Computer: This command cannot be executed on target computer ('xxx')
due to following error: No mapping between account names and security IDs was done.
At line :1 char:16
+ remove-computer <<<<  -credential $DomainCred -force -passthru -verbose
    + CategoryInfo          : InvalidOperation: (xxx:String) [Remove-Computer],
    InvalidOperationException
    + FullyQualifiedErrorId : InvalidOperationException,Microsoft.Powershell.
    Commands.RemoveComputerCommand

但是,如果我使用 GUI(计算机属性、高级系统设置、计算机名称、更改...)尝试此操作,它会提示输入凭据并成功。

如何将此操作复制到 powershell 命令中,以便实用地完成?

【问题讨论】:

    标签: windows powershell dns active-directory powershell-2.0


    【解决方案1】:

    试试Add-Computer,像这样(未经测试):

    Add-Computer -WorkgroupName "WORKGROUP" -Force
    

    AFAIK Add-ComputerRemove-Computer 之间的唯一区别是 Remove-Computer 还会禁用计算机帐户,这可能会给您此错误,因为计算机帐户不存在。

    【讨论】:

    • 我试过这个,但得到了同样的错误信息。 (此外,Windows 7 上的 Powershell 不支持 -Force 标志)
    【解决方案2】:

    我有两个选择。

    选项 01

    $Workgroup = "CL-01" #IF you want to add computer to domain edit here(Domain name)
    $Password = "Password" | ConvertTo-SecureString -asPlainText -Force
    $Username = "$Workgroup\Username"
    $Credential = New-Object System.Management.Automation.PSCredential($Username,$Password)
    Add-Computer -WorkGroup $Workgroup -Credential $credential
    Restart-Computer -Force
    

    选项 2 以及为什么选项 2 在脚本中存储密码不是一个有利的选项,所以我建议采用选项 2

    $Workgroup = "CL-01"#IF you want to add computer to domain edit here(Domain name)
    $Password = Read-Host -Prompt "Enter password for $user" -AsSecureString
    $Username = "$Workgroup\Username" 
    $credential = New-Object System.Management.Automation.PSCredential($Username,$Password)
    Add-Computer -WorkGroup $Workgroup -Credential $credential
    Restart-Computer -Force
    

    注意:以管理员身份运行所有脚本!!

    希望这会有所帮助!干杯!!

    【讨论】:

    • 我尝试了选项 1,但在尝试使用工作组时收到权限被拒绝(无论我提供什么凭据)。我也尝试了域选项,但收到了我最初收到的错误消息(没有完成映射)。还有什么想法吗?
    • 回答第一个问题(如果您使用的帐户没有域管理员权限或本地管理员),将计算机从域添加到工作组时使用本地管理员凭据而不是域另一个原因是您必须使用没有本地管理员权限的帐户。因此,请始终使用正确的凭据。
    • 第二个问题的答案如下原因 - 在客户端计算机上重命名了 SAM 帐户名称。 - 域成员计算机上不存在帐户。 - SAM 帐户名与其域帐户名不同。 - 客户端正在运行使用与域控制器不同的默认语言的多语言用户界面包 (MUI)。再检查一遍。祝你有美好的一天:) 注意:我在这个网站上发布的所有内容都是由我自己测试的,因此我可以确保它可以 100% 工作:) 更多信息:support.microsoft.com/kb/890737
    猜你喜欢
    • 2023-01-11
    • 2018-11-02
    • 2021-01-14
    • 2012-12-12
    • 1970-01-01
    • 2010-11-28
    • 2015-04-22
    • 1970-01-01
    • 2013-03-17
    相关资源
    最近更新 更多