重新命名多台PC',然后从CSV文件加入域

问题描述

我需要能够重命名,然后从一个CSV文件中加入域,多台PC。

例如CSV的内容:-

oldpcname1,newpcname1
oldpcname2,newpcname2

等。

代码如下:-

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

我遇到的问题是访问被拒绝&WMI错误,该脚本似乎尝试执行应有的操作,但被阻止了。我从具有完全管理员权限的DC运行,没有防火墙,可以浏览测试VMS的C $(测试环境中的1个DC和2个工作站)

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],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],Microsoft.PowerShell.Commands.AddComputerCommand

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)