我正在我们的测试环境中进行一些自动化,我们有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命令中,以便可以实际完成?
答案 0 :(得分:0)
尝试Add-Computer
,就像这样(未经测试):
Add-Computer -WorkgroupName "WORKGROUP" -Force
AFAIK Add-Computer
和Remove-Computer
之间的唯一区别是Remove-Computer
也会禁用计算机帐户,这可能会因为计算机帐户不存在而导致此错误。
答案 1 :(得分:0)
我有两个选择。
选项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
注意:以管理员身份运行所有脚本!!
希望这会有所帮助!!干杯!!