1
votes

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?

1
You're replacing the WMI object in $computer with the computername string ($computer = $computer.name). The string doesn't have a method JoinDomainOrWorkGroup or Rename, only the WMI object does. Please do not get creative. Stick to what was suggested until you get it to work.Ansgar Wiechers

1 Answers

1
votes

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