我正在尝试简化将 PC 添加到域的过程。到目前为止,我可以更改 PC 名称、重新启动并将 PC 添加到特定 OU,然后再次重新启动。
我和OP有同样的问题这个话题 https://stackoverflow.com/questions/6217799/rename-computer-and-join-to-domain-in-one-step-with-powershell,我尝试遵循建议,但出现错误。
我指的是目标计算机名,但它说无法找到该参数,即使我在脚本中之前设置了它。
错误“Add-Computer:找不到与参数名称“computername”匹配的参数。位于 C:\RenameComputer.ps1:10 char:47”
我的代码如下:
$computername = get-wmiobject win32_computersystem
$computername
$name = read-host -Prompt "Please enter computer name you want to use:"
$computername.rename($name)
$domain = "Domain"
$username = read-host -Prompt "Please enter Admin Account - domain\id:"
$password = read-host -Prompt "Please enter Admin Password" -AsSecureString
$credential = New-Object System.Management.Automation.PSCredential($username,$password)
Add-Computer -DomainName $domain -computername $computername -newname $name -Credential $credential -OUPath "OU=Sub Container,OU=Parent Container,DC=Domain,DC=com"
另外,如果 PC 存在于 OU 中,或者在 PC 重建之前未从域中删除,如果我将新计算机添加到域中,然后将其重命名为已存在的对象,我希望它会抛出错误,有什么办法可以避免这种情况吗?
LATEST
非常感谢您的回复
测试下面提供的 PS2 脚本并修改它以适合我们的 OU 后,我收到此错误
Get-WmiObject : Invalid query
At C:\RenameComputer.ps1:8 char:26
+ $computer = Get-WmiObject <<<< -Class Win32_ComputerSystem `
+ CategoryInfo : InvalidOperation: (:) [Get-WmiObject], Managemen
tException
+ FullyQualifiedErrorId : GetWMIManagementException,Microsoft.PowerShell.C
ommands.GetWmiObjectCommand
You cannot call a method on a null-valued expression.
At C:\RenameComputer.ps1:12 char:32
+ $computer.JoinDomainOrWorkGroup <<<< ($domain, $password, $username, $ou, 3,
$true)
+ CategoryInfo : InvalidOperation: (JoinDomainOrWorkGroup:String)
[], RuntimeException
+ FullyQualifiedErrorId : InvokeMethodOnNull
You cannot call a method on a null-valued expression.
At C:\RenameComputer.ps1:13 char:17
+ $computer.Rename <<<< ($newname)
+ CategoryInfo : InvalidOperation: (Rename:String) [], RuntimeExc
eption
+ FullyQualifiedErrorId : InvokeMethodOnNull
然后我修改了代码 $computer = Get-WmiObject 代码来读取
$domain = "Domain"
$ou = "OU=Sub Container,OU=Parent,DC=Domain,DC=com"
$newname = read-host -Prompt "Please enter computer name you want to use:"
$username = read-host -Prompt "Please enter Admin Account - domain\id:"
$password = read-host -Prompt "Please enter Admin Password"
$computer = Get-WmiObject Win32_ComputerSystem
$computer = $computer.name
$computer.JoinDomainOrWorkGroup($domain, $password, $username, $ou, 3, $true)
$computer.Rename($newname)
#Restart-Computer
我收到这个错误
Method invocation failed because [System.String] doesn't contain a method named
'JoinDomainOrWorkGroup'.
At C:\RenameComputer.ps1:11 char:32
+ $computer.JoinDomainOrWorkGroup <<<< ($domain, $password, $username, $ou, 3,
$true)
+ CategoryInfo : InvalidOperation: (JoinDomainOrWorkGroup:String)
[], RuntimeException
+ FullyQualifiedErrorId : MethodNotFound
Method invocation failed because [System.String] doesn't contain a method named
'Rename'.
At C:\RenameComputer.ps1:12 char:17
+ $computer.Rename <<<< ($newname)
+ CategoryInfo : InvalidOperation: (Rename:String) [], RuntimeExc
eption
+ FullyQualifiedErrorId : MethodNotFound
我现在不应该使用“添加计算机”方法吗?