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

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

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"

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

$serviceTagInfo = [PSCustomObject]@{
   DataCenter = $mydatacenter
   HostName = $vhostName
   Model = $model
   ServiceTag = $serviceTag
   }

# 将对象添加到数组中
$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

$hosts = Get-VMHost

Initialize an empty array to store the data

$serviceTags = @()

Loop through each host

foreach ($hostsname in $hosts) 
 {
    **#Retrieve Datacenter, hostName and Model**
    $mydatacenter = Get-Datacenter -VMHost $hosts
    write-host "Completed Datacenter..."
    $vhostName = $hosts.Name
    write-host "Completed Hosts..."
    $model = $hosts.ExtensionData.Summary.Hardware.Model
    write-host "Completed Model..."   
    $serviceTag = ($hostsname| Get-View).Hardware.SystemInfo.OtherIdentifyingInfo |
        Where-Object { $_.IdentifierType.Key -eq "ServiceTag" } |
        Select-Object -ExpandProperty IdentifierValue
    write-host "Completed ServiceTag"

   # Create a custom object with Datacenter, host name Model and service tag

    $serviceTagInfo = [PSCustomObject]@{
       DataCenter = $mydatacenter
       HostName = $vhostName
       Model = $model
       ServiceTag = $serviceTag
       }

    # Add the object to the array
    $serviceTags += $serviceTagInfo

}
# Export the data to CSV
$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

$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

$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:

$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:

确定