【问题标题】:Issue Adding PC to domain and changing name at the same time问题将PC添加到域并同时更改名称
【发布时间】: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 对象。该字符串没有方法JoinDomainOrWorkGroupRename,只有WMI 对象有。请不要有创意。坚持所建议的,直到你得到它的工作。

标签: powershell active-directory


【解决方案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)

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

【讨论】:

  • 非常感谢您的回复。不幸的是,-newname 参数仍然存在问题错误 - 添加计算机:找不到与参数名称“NewName”匹配的参数。在 line:1 char:42 + Add-Computer -DomainName $domain -NewName
  • Powershell v2 已安装。据我所知,需要版本 3 才能识别失败的参数。我会测试一下
  • @user3364233 是的,可能是版本问题。查看更新的答案。
  • 升级到 v3 - 脚本有效,但我被建议避免,因为 PS 的全球升级可能会影响我们在整个企业中运行的许多脚本。我尝试了 V2 脚本,但我也遇到了失败 - - 你有直接的电子邮件或 PM 来发送消息 - 对于 cmets 部分来说太大了吗??
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2022-01-13
  • 2018-09-14
  • 2017-01-29
  • 2012-08-12
  • 1970-01-01
  • 1970-01-01
  • 2013-03-11
相关资源
最近更新 更多