数据未从PowerShell脚本正确导出到.csv文件。

huangapple go评论106阅读模式
英文:

Data not exporting correctly into .csv from Powershell script

问题

以下是代码的翻译部分:

尽管我可以将所有数据显示在终端上;但在导出时,主机名列填充为 System.Object[]。我需要添加两列,但在之前的尝试中,它们也填充了,例如:
oUTPUT_eXAMPLE

检索主机信息

$hosts = Get-VMHost

初始化一个空数组以存储数据

$serviceTags = @()

遍历每个主机

foreach ($hostsname in $hosts)
{
# 检索数据中心、主机名和型号
$mydatacenter = Get-Datacenter -VMHost $hosts
write-host "已完成数据中心..."
$vhostName = $hosts.Name
write-host "已完成主机..."
$model = $hosts.ExtensionData.Summary.Hardware.Model
write-host "已完成型号..."
$serviceTag = ($hostsname| Get-View).Hardware.SystemInfo.OtherIdentifyingInfo |
Where-Object { $_.IdentifierType.Key -eq "ServiceTag" } |
Select-Object -ExpandProperty IdentifierValue
write-host "已完成ServiceTag"

创建一个包含数据中心、主机名、型号和服务标签的自定义对象

  1. $serviceTagInfo = [PSCustomObject]@{
  2. DataCenter = $mydatacenter
  3. HostName = $vhostName
  4. Model = $model
  5. ServiceTag = $serviceTag
  6. }
  7. # 将对象添加到数组中
  8. $serviceTags += $serviceTagInfo

}

导出数据到CSV

$serviceTags | Export-Csv -Path "c:\Users\myuser\Documents\ServiceTags2.csv" -NoTypeInformation

英文:

Though I can display all the data to the terminal; when it exports, the Hostname columns fills with
System.Object[]. I need to add to more columns, but in previous attemps thy also populated with
Example:
oUTPUT_eXAMPLE

Retrieve host information

  1. $hosts = Get-VMHost

Initialize an empty array to store the data

  1. $serviceTags = @()

Loop through each host

  1. foreach ($hostsname in $hosts)
  2. {
  3. **#Retrieve Datacenter, hostName and Model**
  4. $mydatacenter = Get-Datacenter -VMHost $hosts
  5. write-host "Completed Datacenter..."
  6. $vhostName = $hosts.Name
  7. write-host "Completed Hosts..."
  8. $model = $hosts.ExtensionData.Summary.Hardware.Model
  9. write-host "Completed Model..."
  10. $serviceTag = ($hostsname| Get-View).Hardware.SystemInfo.OtherIdentifyingInfo |
  11. Where-Object { $_.IdentifierType.Key -eq "ServiceTag" } |
  12. Select-Object -ExpandProperty IdentifierValue
  13. write-host "Completed ServiceTag"
  14. # Create a custom object with Datacenter, host name Model and service tag
  15. $serviceTagInfo = [PSCustomObject]@{
  16. DataCenter = $mydatacenter
  17. HostName = $vhostName
  18. Model = $model
  19. ServiceTag = $serviceTag
  20. }
  21. # Add the object to the array
  22. $serviceTags += $serviceTagInfo
  23. }
  24. # Export the data to CSV
  25. $serviceTags | Export-Csv -Path "c:\Users\myuser\Documents\ServiceTags2.csv" -NoTypeInformation

答案1

得分: 1

$vhostName = $hosts.Name

该行代码通过将$hosts数组的Name字段展开为所有名称的字符串数组来工作。它等同于

$vhostName = $hosts | foreach {$_.Name}

这被称为成员访问枚举

由于$vhostName是一个数组,因此在输出中它会扩展为System.Object[]。由于您已经在遍历$hosts,只需使用当前的$hostname结构:

$vhostName = $hostname.Name

英文:

The line

  1. $vhostName = $hosts.Name

works by expanding the Name field of the $hosts array into an array of strings of all the names. It's equivalent to

  1. $vhostName = $hosts | foreach {$_.Name}

It's called member-access-enumeration.

Since $vhostName is an array it gets expanded to System.Object[] in your output. Since your already iterating through $hosts just use the current $hostname struct:

  1. $vhostName = $hostname.Name

huangapple
  • 本文由 发表于 2023年6月1日 07:13:15
  • 转载请务必保留本文链接:https://go.coder-hub.com/76377807.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定