【发布时间】:2015-01-14 14:43:00
【问题描述】:
我正在尝试简化将 PC 添加到域的过程。到目前为止,我可以更改 PC 名称、重新启动并将 PC 添加到特定 OU 并再次重新启动。
我遇到了与this topic 中的 OP 相同的问题,我尝试按照建议进行操作,但出现错误。
我指的是目标计算机名,但它说即使我在脚本的前面设置了参数也找不到。
错误“添加计算机:找不到与参数名称'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 重建之前没有从域中删除,如果我将新计算机添加到域,然后将其重命名为已经存在的对象,我希望它抛出一个错误,有没有办法避免呢?
最新的
非常感谢您的回复 在测试了下面提供的 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
我现在不应该使用 Add-Computer 方法吗?
【问题讨论】:
-
您正在用计算机名字符串 (
$computer = $computer.name) 替换$computer中的 WMI 对象。该字符串没有方法JoinDomainOrWorkGroup或Rename,只有WMI 对象有。请不要有创意。坚持所建议的,直到你得到它的工作。
标签: powershell active-directory