我正在尝试简化将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方法吗?
答案 0 :(得分:1)
您可能收到错误,因为$computername
是WMI对象,而不是字符串。
这应该足以更改本地计算机的名称并将其加入域:
$domain = 'Domain'
$ou = 'OU=Sub Container,OU=Parent Container,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' -AsSecureString
$cred = New-Object Management.Automation.PSCredential($username,$password)
Add-Computer -DomainName $domain -NewName $newname -Credential $cred -OUPath $ou
Reboot-Computer
请注意,PowerShell v2似乎不支持-NewName
参数。如果你坚持使用该版本,我建议使用WMI进行操作:
$domain = 'Domain'
$ou = 'OU=Sub Container,OU=Parent Container,DC=Domain,DC=com'
$username = read-host -Prompt 'Please enter Admin Account - domain\id:'
$password = read-host -Prompt 'Please enter Admin Password'
$computer = Get-WmiObject -Class Win32_ComputerSystem `
-Filter "Name = '$env:COMPUTERNAME'" `
-Impersonation Impersonate -EnableAllPrivileges
$computer.JoinDomainOrWorkGroup($domain, $password, $username, $ou, 3)
可以用相同的方式重命名计算机:
$newname = read-host -Prompt 'Please enter computer name you want to use:'
$computer = Get-WmiObject -Class Win32_ComputerSystem `
-Filter "Name = '$env:COMPUTERNAME'" `
-Impersonation Impersonate -EnableAllPrivileges
$computer.Rename($newname)
我认为在这两个步骤之间需要重新启动(至少我无法在一个步骤中重命名和加入测试框)。