Re naming multiple PC' and then Joining a Domain from a CSV File

0

I need to be able to re name and then join a domain, multiple PC's from a CSV file.

Content of CSV is for example :-

oldpcname1,newpcname1
oldpcname2,newpcname2

etc..

The code is as follows :-

$File = "C:\computer_names.csv"
$domain = "MBTEST.LOCAL"
$password = "password" | ConvertTo-SecureString -asPlainText -Force
$username = "$domain\Administrator"
$user2 = "administrator"
$pass2 = "pa55w0rd" | ConvertTo-SecureString -asPlainText -Force
$lcred = New-Object System.Management.Automation.PsCredential($user2, $pass2)
$credential = New-Object System.Management.Automation.PSCredential($username,$password)
 
 
$computerList= Import-Csv -Path $File `
                          -Delimiter "," `
                          -Header OldName,NewName
 
foreach ($Computer in $computerList)
{
Rename-Computer -ComputerName $Computer.OldName -NewName $Computer.NewName -LocalCredential $lcred 
Add-Computer -ComputerName $Computer.OldName -Domain 'MBTEST.local' -Domaincredential $credential -LocalCredential $lcred -Restart 
Start-Sleep -seconds 5 
}

Problem I am having is access denied & WMI error's, the script seems to try and do what it is supposed to but is getting blocked. I am running from the DC with full blown admin rights, no firewalls on, can browse the C$ of the test VMS (1 DC & 2 Workstations in test env.)

Rename-Computer : Cannot establish the WMI connection to the computer 'bongo4' with the following error message: Access is denied. (Exception from HRESULT: 0x80070005 (E_ACCESSDENIED)).
At C:\SHARED\Batch_Rename_PC_and_Join.ps1:20 char:1
+ Rename-Computer -ComputerName $Computer.OldName -NewName $Computer.Ne ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : OperationStopped: (bongo4:String) [Rename-Computer], InvalidOperationException
    + FullyQualifiedErrorId : RenameComputerException,Microsoft.PowerShell.Commands.RenameComputerCommand
 
Add-Computer : Cannot establish the WMI connection to the computer 'bongo4' with the following error message: Access is denied. (Exception from HRESULT: 0x80070005 (E_ACCESSDENIED)).
At C:\SHARED\Batch_Rename_PC_and_Join.ps1:21 char:1
+ Add-Computer -ComputerName $Computer.OldName -Domain 'ICTMBTST.local' ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : OperationStopped: (bongo4:String) [Add-Computer], InvalidOperationException
    + FullyQualifiedErrorId : AddComputerException,Microsoft.PowerShell.Commands.AddComputerCommand
 
Rename-Computer : Cannot establish the WMI connection to the computer 'bongo3' with the following error message: The RPC server is unavailable. (Exception from HRESULT: 0x800706BA).
At C:\SHARED\Batch_Rename_PC_and_Join.ps1:20 char:1
+ Rename-Computer -ComputerName $Computer.OldName -NewName $Computer.Ne ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : OperationStopped: (bongo3:String) [Rename-Computer], InvalidOperationException
    + FullyQualifiedErrorId : RenameComputerException,Microsoft.PowerShell.Commands.RenameComputerCommand
 
Add-Computer : Cannot establish the WMI connection to the computer 'bongo3' with the following error message: Access is denied. (Exception from HRESULT: 0x80070005 (E_ACCESSDENIED)).
At C:\SHARED\Batch_Rename_PC_and_Join.ps1:21 char:1
+ Add-Computer -ComputerName $Computer.OldName -Domain 'ICTMBTST.local' ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : OperationStopped: (bongo3:String) [Add-Computer], InvalidOperationException
    + FullyQualifiedErrorId : AddComputerException,Microsoft.PowerShell.Commands.AddComputerCommand
windows
powershell
dns
hostname
asked on Stack Overflow Sep 6, 2020 by Tika9o9 • edited Sep 6, 2020 by Tika9o9

0 Answers

Nobody has answered this question yet.


User contributions licensed under CC BY-SA 3.0