getting this error when running script
Get-ADComputer:变量:在表达式中找到“computerName”:$computerName 未定义。 行:15 字符:20
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ 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 ...
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
$excelFilePath =“C:\Scripts\ADcomputerinfo.xlsx”
$excelData = 导入-Csv -Path $excelFilePath
$计算机信息 = @()
foreach($excelData 中的$row){
your text
$computerName = $row.ComputerName
$compDetails = Get-ADComputer -Filter {Name -eq $computerName} -Properties OperatingSystem、OperatingSystemVersion
$compObject = [PSCustomObject]@{ 计算机名称 = $compDetails.Name 操作系统 = $compDetails.OperatingSystem 操作系统版本 = $compDetails.OperatingSystemVersion }
$computerInfo += $compObject }
$outputFilePath = "C:\Scripts\ADComputerInfooutput.xlsx" $计算机信息 |导出-Csv -路径 $outputFilePath -NoTypeInformation
Write-Host“计算机信息导出到$outputFilePath”
您不能对 .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