出现将PC添加到域并同时更改名称的问题 [英] Issue Adding PC to domain and changing name at the same time

查看:131
本文介绍了出现将PC添加到域并同时更改名称的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试简化将PC添加到域的过程。到目前为止,我可以更改PC名称,重新启动并将PC添加到特定的OU,然后再次重新启动。



我和本主题,并且我尝试遵循



我指的是目标计算机名,但它说即使我在脚本的前面设置了参数也找不到该参数。


错误添加计算机:找不到与参数名称'computername'相匹配的参数。在C:\RenameComputer.ps1: 10 char:47


我的代码如下:

  $ computername = get-wmiobject win32_computersystem 
$ computername
$ name =读主机-提示请输入您要使用的计算机名称:
$ computername .rename($ name)
$ domain = Domain
$ username =读主机-提示请输入管理员帐户-domain\id:
$ password =读主机-提示请输入管理员密码 -AsSecureString
$ credential =新对象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重新建立之前没有从域中删除d,如果我将新计算机添加到域中,然后将其重命名为已经存在的对象,我希望它会引发错误,是否有避免的方法?



最新



非常感谢您的答复
测试了下面提供的PS2脚本,并对其进行了修改以适合我们的OU后,出现此错误

  Get-WmiObject:无效查询
在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

您不能在空值表达式上调用方法。
在C:\RenameComputer.ps1:12 char:32
+ $ computer.JoinDomainOrWorkGroup<<<< ($ domain,$ password,$ username,$ ou,3,
$ true)
+ CategoryInfo:InvalidOperation:(JoinDomainOrWorkGroup:String)
[],RuntimeException
+ FullyQualifiedErrorId :InvokeMethodOnNull

您不能在空值表达式上调用方法。
在C:\RenameComputer.ps1:13 char:17
+ $ computer.Rename<<<< ($ newname)
+ CategoryInfo:InvalidOperation :(重命名:String)[],RuntimeExc
eption
+ FullyQualifiedErrorId:InvokeMethodOnNull






然后我修改了代码$ computer = Get-WmiObject代码以读取

  $ domain = Domain 
$ ou = OU = Sub Container,OU = Parent,DC = Domain,DC = com

$ newname =读主机-Prompt请输入要使用的计算机名:
$ username =读主机-Prompt请输入管理员帐户-domain\id:
$密码=读取主机-提示请输入管理员密码

$ computer = Get-WmiObject Win32_ComputerSystem
$ computer = $ computer.name

$ computer。 JoinDomainOrWorkGroup($ domain,$ password,$ username,$ ou,3,$ true)
$ computer.Rename($ newname)

#重启计算机

我收到此错误

 方法调用失败,因为[System.String]不包含一种名为
JoinDomainOrWorkGroup的方法。
在C:\RenameComputer.ps1:11 char:32
+ $ computer.JoinDomainOrWorkGroup<<<< ($ domain,$ password,$ username,$ ou,3,
$ true)
+ CategoryInfo:InvalidOperation:(JoinDomainOrWorkGroup:String)
[],RuntimeException
+ FullyQualifiedErrorId :MethodNotFound

方法调用失败,因为[System.String]不包含名为
'Rename'的方法。
在C:\RenameComputer.ps1:12 char:17
+ $ computer.Rename<<<< ($ newname)
+ CategoryInfo:InvalidOperation :(重命名:String)[],RuntimeExc
eption
+ FullyQualifiedErrorId:MethodNotFound

此时我应该不使用Add-Computer方法吗?

解决方案

您可能会遇到错误,因为 $ computername 是WMI对象,而不是字符串。



这足以更改本地计算机的名称并将其加入域:

  $ domain ='Domain'
$ ou ='OU = Sub Container,OU = Parent Container,DC = Domain,DC = com'

$ newname =读取主机-提示'请输入您要使用的计算机名称:'
$用户名=读取主机-提示'请输入管理员帐户-domain\id:'
$ password =读取- host-提示'请输入管理员密码'-AsSecureString
$ cred =新对象管理.Automation.PSCredential($ username,$ pass word)

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 =读主机-提示'请输入管理员帐户-domain\id:'
$ password =读取主机-提示'请输入管理员密码'

$ computer = Get-WmiObject -Win32_ComputerSystem类`
-过滤器名称='$ env:COMPUTERNAME'`
-模仿模拟-EnableAllPrivileges

$ computer.JoinDomainOrWorkGroup($ domain,$ password,$ username,$ ou,3)

可以用相同的方式重命名计算机:



< pre class = lang-psh prettyprint-override> $ newname =读取主机-提示'请输入您要使用的计算机名称:'

$ computer = Get- WmiObject -Class Win32_ComputerSystem`
-Filter Name ='$ env:COMPUTERNAME'`
-模仿模拟-EnableAllPrivileges

$ computer.Rename($ newname)

我认为在这两个步骤之间需要重新启动(至少我无法在单个步骤中重命名并加入测试框)。


I am trying to simplify our process to add a PC to a domain. So far I can change a PC name, restart and add the PC to a specific OU and another restart.

I have the same problem as the OP in this topic, and I have tried to follow the suggestions yet I am getting an error.

I am referring to the target computername yet it says the parameter could not be found even though I set it earlier in the script.

Error "Add-Computer : A parameter cannot be found that matches parameter name 'computername'. At C:\RenameComputer.ps1:10 char:47"

My code is as follows:

$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"

Also, if the PC exists in OU, or was not deleted from the domain before a PC rebuild, if I add the new computer to a domain, then rename it to an object that is already there, I would expect it to throw an error, is there anyway to avoid that?

LATEST

Many thanks for your reply After testing the PS2 script provided below, and amending it to suit our OU's, I got this error

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


I then amended the code $computer = Get-WmiObject code to read

$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

and I get this error

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

Should I not be using the Add-Computer method at this point?

解决方案

You're probably getting the error, because $computername is a WMI object, not a string.

This should suffice for changing the name of the local computer and joining it to a domain:

$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

Note that PowerShell v2 doesn't seem to support the -NewName parameter. If you're stuck with that version I'd recommend using WMI for the operation:

$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)

The computer can be renamed in the same way:

$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)

I think a reboot is required between those two steps (at least I wasn't able to rename and join a test box in a single step).

这篇关于出现将PC添加到域并同时更改名称的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆