如何通过 PowerShell 远程重命名计算机列表?
How to remotely rename a list of computers via PowerShell?
我有一大堆 Windows 10 个工作站需要重命名。我已经尝试 运行 下面的脚本,但出现了超出我当前 PS 水平的错误。
$computers = Import-Csv "c:\rename-computers\computers.csv"
foreach ($oldname in $computers){
#Write-Host "EmpID=" + $computers.NewName
Rename-Computer -ComputerName $computers.OldName -NewName $computers.NewName -DomainCredential hole\inwall -Force -Restart
}
产生:
Rename-Computer : Cannot convert 'System.Object[]' to the type
'System.String' required by parameter 'ComputerName'. Specified
method is not supported. At
\siat-ds0\appdeploy\LabPacks\rename-computers\rename-siat.ps1:4
char:35
+ Rename-Computer -ComputerName $computers.OldName -NewName $computers.NewName ...
+ ~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidArgument: (:) [Rename-Computer], ParameterBindingException
+ FullyQualifiedErrorId : CannotConvertArgument,Microsoft.PowerShell.Commands.RenameComputerCommand
我在其他地方看到过关于此主题的类似封闭线程,但没有提及我收到的错误。
您正在迭代但未使用单数:
而不是这个:
foreach ($oldname in $computers){
#Write-Host "EmpID=" + $computers.NewName
Rename-Computer -ComputerName $computers.OldName -NewName $computers.NewName -DomainCredential hole\inwall -Force -Restart
}
试试这个:
foreach ($oldname in $computers){
Rename-Computer -ComputerName $oldname.OldName -NewName $oldname.NewName -DomainCredential hole\inwall -Force -Restart
}
注: $oldname
在某一点保持一个值。所以$computers
中出现的计算机数量会一台接一台地来到$oldname
,并会在循环中执行activity。
你应该在循环中使用单数$oldname
来逐个迭代。
您错误地使用了 collection 变量 $computers
而不是 loop-iteration 变量 $oldname
在你的循环 中,并且由于 $computers.NewName
扩展为一个 数组 的名称而不是一个,你得到了你看到的错误。
就是说,您根本不需要循环 - 一条管道即可:
Import-Csv "c:\rename-computers\computers.csv" |
Rename-Computer -ComputerName { $_.OldName } -DomainCredential hole\inwall -Force -Restart
Rename-Computer
会将每个输入对象的 NewName
属性 隐式绑定到 -NewName
参数。
-ComputerName
参数,相比之下,必须告诉 属性 输入对象上要访问的内容,因为输入对象没有 ComputerName
属性。
这就是脚本块 { $_.OldName }
所做的,其中自动变量 $_
表示手头的输入对象。
要查看哪些参数接受管道输入,请检查
的输出
Get-Help -full Rename-Computer
;有关详细信息和编程替代方案,请参阅我的 。
Bulk rename computers in AD
Powershell 批量重命名 AD 中的计算机,测试电脑是否在线以及是否已使用新名称并记录“not-renamed”PC。
adc.csv
oldname,newname
WEDSKS0022,RKVKS0110
WEDSKS0117,RKVKS1413
Set-ExecutionPolicy -Scope CurrentUser -ExecutionPolicy Bypass -Force;
$computers = import-csv -Path ".\adc.csv"
$Credential = Get-Credential
$nisuprosli=".\notrenamed $(get-date -f dd-MM-yyyy-HHMM).csv"
$makecsv="oldname,newname" | Out-File $nisuprosli -Encoding utf8 -Append
foreach ($pc in $computers){
$IsOldPCalive=Test-Connection -ComputerName $pc.oldname -Quiet -Count 1 -ErrorAction SilentlyContinue
$IsNewPCalive=Test-Connection -ComputerName $pc.newname -Quiet -Count 1 -ErrorAction SilentlyContinue
if ($IsOldPCalive -eq $True -and $IsNewPCalive -eq $False) {
write-host "Rename PC $($pc.oldname) u $($pc.newname)" -ForegroundColor Cyan
Rename-computer -computername $pc.oldname -newname $pc.newname -domaincredential $Credential -PassThru -force -restart #-WhatIf
}
else {
write-host "PC $($pc.oldname) is not available or already exists $($pc.newname)" -ForegroundColor Yellow
$makecsv="$($pc.oldname),$($pc.newname)" | Out-File $nisuprosli -Encoding utf8 -Append
}
}
我有一大堆 Windows 10 个工作站需要重命名。我已经尝试 运行 下面的脚本,但出现了超出我当前 PS 水平的错误。
$computers = Import-Csv "c:\rename-computers\computers.csv"
foreach ($oldname in $computers){
#Write-Host "EmpID=" + $computers.NewName
Rename-Computer -ComputerName $computers.OldName -NewName $computers.NewName -DomainCredential hole\inwall -Force -Restart
}
产生:
Rename-Computer : Cannot convert 'System.Object[]' to the type 'System.String' required by parameter 'ComputerName'. Specified method is not supported. At \siat-ds0\appdeploy\LabPacks\rename-computers\rename-siat.ps1:4 char:35 + Rename-Computer -ComputerName $computers.OldName -NewName $computers.NewName ... + ~~~~~~~~~~~~~~~~~~ + CategoryInfo : InvalidArgument: (:) [Rename-Computer], ParameterBindingException + FullyQualifiedErrorId : CannotConvertArgument,Microsoft.PowerShell.Commands.RenameComputerCommand
我在其他地方看到过关于此主题的类似封闭线程,但没有提及我收到的错误。
您正在迭代但未使用单数:
而不是这个:
foreach ($oldname in $computers){
#Write-Host "EmpID=" + $computers.NewName
Rename-Computer -ComputerName $computers.OldName -NewName $computers.NewName -DomainCredential hole\inwall -Force -Restart
}
试试这个:
foreach ($oldname in $computers){
Rename-Computer -ComputerName $oldname.OldName -NewName $oldname.NewName -DomainCredential hole\inwall -Force -Restart
}
注: $oldname
在某一点保持一个值。所以$computers
中出现的计算机数量会一台接一台地来到$oldname
,并会在循环中执行activity。
你应该在循环中使用单数$oldname
来逐个迭代。
您错误地使用了 collection 变量 $computers
而不是 loop-iteration 变量 $oldname
在你的循环 中,并且由于 $computers.NewName
扩展为一个 数组 的名称而不是一个,你得到了你看到的错误。
就是说,您根本不需要循环 - 一条管道即可:
Import-Csv "c:\rename-computers\computers.csv" |
Rename-Computer -ComputerName { $_.OldName } -DomainCredential hole\inwall -Force -Restart
Rename-Computer
会将每个输入对象的 NewName
属性 隐式绑定到 -NewName
参数。
-ComputerName
参数,相比之下,必须告诉 属性 输入对象上要访问的内容,因为输入对象没有 ComputerName
属性。
这就是脚本块 { $_.OldName }
所做的,其中自动变量 $_
表示手头的输入对象。
要查看哪些参数接受管道输入,请检查
的输出
Get-Help -full Rename-Computer
;有关详细信息和编程替代方案,请参阅我的
Bulk rename computers in AD Powershell 批量重命名 AD 中的计算机,测试电脑是否在线以及是否已使用新名称并记录“not-renamed”PC。
adc.csv
oldname,newname
WEDSKS0022,RKVKS0110
WEDSKS0117,RKVKS1413
Set-ExecutionPolicy -Scope CurrentUser -ExecutionPolicy Bypass -Force;
$computers = import-csv -Path ".\adc.csv"
$Credential = Get-Credential
$nisuprosli=".\notrenamed $(get-date -f dd-MM-yyyy-HHMM).csv"
$makecsv="oldname,newname" | Out-File $nisuprosli -Encoding utf8 -Append
foreach ($pc in $computers){
$IsOldPCalive=Test-Connection -ComputerName $pc.oldname -Quiet -Count 1 -ErrorAction SilentlyContinue
$IsNewPCalive=Test-Connection -ComputerName $pc.newname -Quiet -Count 1 -ErrorAction SilentlyContinue
if ($IsOldPCalive -eq $True -and $IsNewPCalive -eq $False) {
write-host "Rename PC $($pc.oldname) u $($pc.newname)" -ForegroundColor Cyan
Rename-computer -computername $pc.oldname -newname $pc.newname -domaincredential $Credential -PassThru -force -restart #-WhatIf
}
else {
write-host "PC $($pc.oldname) is not available or already exists $($pc.newname)" -ForegroundColor Yellow
$makecsv="$($pc.oldname),$($pc.newname)" | Out-File $nisuprosli -Encoding utf8 -Append
}
}