需要从列表中获取运行计算机名称的脚本并在广告中验证操作系统信息

问题描述 投票:0回答:1
 getting this error when running script 

Get-ADComputer:变量:在表达式中找到“computerName”:$computerName 未定义。 行:15 字符:20

  • ... mpDetails = Get-ADComputer -Filter {Name -eq $computerName} -Properti ...
  •             ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    
+ CategoryInfo          : InvalidArgument: (:) [Get-ADComputer], ArgumentException
+ FullyQualifiedErrorId :     ActiveDirectoryCmdlet:System.ArgumentException,Microsoft.ActiveDirectory.Management.Commands.GetADComputer

Get-ADComputer : Variable: 'computerName' found in expression: $computerName is not defined.
At line:15 char:20
  • ... mpDetails = Get-ADComputer -Filter {Name -eq $computerName} -Properti ...

  •             ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    
    • CategoryInfo:InvalidArgument:(:) [Get-ADComputer],ArgumentException
    • FullyQualifiedErrorId:ActiveDirectoryCmdlet:System.ArgumentException,Microsoft.ActiveDirectory.Management.Commands.GetADComputer

    指定 Excel 文件的路径

    $excelFilePath =“C:\Scripts\ADcomputerinfo.xlsx”

    加载 Excel 数据(假设列标题为“ComputerName”)

    $excelData = 导入-Csv -Path $excelFilePath

    初始化一个空数组来存储结果

    $计算机信息 = @()

    循环遍历 Excel 数据中的每一行

    foreach($excelData 中的$row){

    your text
    $computerName = $row.ComputerName

    从AD检索计算机信息

    $compDetails = Get-ADComputer -Filter {Name -eq $computerName} -Properties OperatingSystem、OperatingSystemVersion

    创建具有相关属性的自定义对象

    $compObject = [PSCustomObject]@{ 计算机名称 = $compDetails.Name 操作系统 = $compDetails.OperatingSystem 操作系统版本 = $compDetails.OperatingSystemVersion }

    将自定义对象添加到数组中

    $computerInfo += $compObject }

    将结果导出到新的 Excel 文件

    $outputFilePath = "C:\Scripts\ADComputerInfooutput.xlsx" $计算机信息 |导出-Csv -路径 $outputFilePath -NoTypeInformation

    Write-Host“计算机信息导出到$outputFilePath”

powershell
1个回答
0
投票

您不能对 .xlsx 文件使用“import-csv”,它需要采用 csv 格式。 继续使用 Excel 并将列表导出为 .csv,或将文档本身另存为 .csv

stackoverflow 上不允许使用人工智能生成的问题/答案。 临时政策:禁止生成式人工智能(例如 ChatGPT)

发布的代码有很多错误。

过渡时期的低努力响应。

# OSInfo for Computers in .CSV file
    # Import computer info from CSV
    $Computers    = Import-csv "C:\temp\ComputersFromExcel.CSV"
    # Loop through each computername in the "Name" column 
    $computerInfo = Foreach ($computername in $Computers.Name){
        # Get-adcomputer info for $computername. "-Properties *" to return all properties.
        Get-adcomputer $Computername -properties * | Select Name,DNSHostname,OperatingSystemVersion 
    }
    # Export-csv to save to a file. -NoTypeInformation removes unnecessary file info from the start of the csv file.
    $computerInfo | Export-csv "C:\temp\ADComputerInfo.csv" -NoTypeInformation

# Alternative - Export OS info for all computers in AD
    Get-adcomputer -filter * -properties * | Select Name,DNSHostname,OperatingSystemVersion | Export-csv "C:\temp\ADComputerInfo.csv" -NoTypeInformation
© www.soinside.com 2019 - 2024. All rights reserved.