英文:
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
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论