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?
$computer
with the computername string ($computer = $computer.name
). The string doesn't have a methodJoinDomainOrWorkGroup
orRename
, only the WMI object does. Please do not get creative. Stick to what was suggested until you get it to work. – Ansgar Wiechers