我有这样的代码,使用csv文件的旧的和新的名称批量重命名我的域上的机器,但我的代码不承认我的csv文件中的值。任何想法如何解决?
csvfile = "C:\temp\rename.csv"
if (Test-Path $csvfile) {
Import-Csv $csvfile | ForEach-Object {
$oldName = $_.OldName
$newName = $_.NewName
if ($oldName -and $newName) {
Write-Host "Renaming computer from: $oldName to: $newName"
Rename-Computer -ComputerName $oldName -NewName $newName -DomainCredential niklas.pusey\username -Force -Restart
} else {
Write-Host "Error: CSV file contains empty values"
}
}
} else {
Write-Host "Error: CSV file not found"
}
我已经尝试过net cmdlet,但没有效果
CSV文件如下所示:
OldName,NewName
ComputerName,NewComputerName
我已经修改了逗号之间的空格,但这不起作用。ComputerName是我的计算机名,用于测试,因此它应该可以识别它。
1条答案
按热度按时间bwitn5fc1#
感谢您的更新。
一般来说,最好一次只写/运行一行代码,以确保在写更多代码之前得到预期的结果,这会使调试更加容易。
对我来说是的。
所以,你的用例的Q&D(快速和肮脏)程序步骤可能是这样的: