【问题标题】:How to remotely rename a list of computers via PowerShell?如何通过 PowerShell 远程重命名计算机列表?
【发布时间】:2017-04-06 00:53:39
【问题描述】:

我有大量需要重命名的 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
}

生产:

重命名计算机:无法将“System.Object[]”转换为类型 参数“ComputerName”需要“System.String”。指定的 方法不受支持。在 \siat-d​​s0\appdeploy\LabPacks\rename-computers\rename-siat.ps1:4 字符:35 + 重命名-计算机 -ComputerName $computers.OldName -NewName $computers.NewName ... +~~~~~~~~~~~~~~~~~~ + CategoryInfo : InvalidArgument: (:) [Rename-Computer], ParameterBindingException + FullyQualifiedErrorId : CannotConvertArgument,Microsoft.PowerShell.Commands.RenameComputerCommand

我在其他地方看到过类似的关闭线程,但没有提及我收到的错误。

【问题讨论】:

    标签: powershell csv batch-processing


    【解决方案1】:

    您错误地在循环中使用了 collection 变量 $computers 而不是 loop-iteration 变量 $oldname,并且因为 @ 987654324@ 扩展为名称的数组,而不是单个名称,您得到了您看到的错误。

    也就是说,您根本不需要循环 - 一条管道就可以了:

    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 的输出;有关详细信息和程序化替代方案,请参阅我的 this answer

    【讨论】:

      【解决方案2】:

      您正在迭代但不使用单数:

      而不是这个:

      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,并将执行循环内的活动。

      您应该在循环中使用单数 $oldname 来一一迭代。

      【讨论】:

        【解决方案3】:

        Bulk rename computers in AD Powershell 批量重命名 AD 中的计算机,并测试 PC 是否在线以及是否已采用新名称并记录“未重命名”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
            }
        
        }
        

        【讨论】:

        • 是的,我忘记翻译了
        猜你喜欢
        • 2021-04-10
        • 2017-06-12
        • 1970-01-01
        • 2015-03-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多