PowerShell在Windows 7上的CSV文件中卡住
我想通过PowerShell脚本在Windows7上创建CSV文件。现在我有PowerShell在Windows 7上的CSV文件中卡住,powershell,csv,windows-7,Powershell,Csv,Windows 7,我想通过PowerShell脚本在Windows7上创建CSV文件。现在我有 $dd = @(Get-Date -Format d) $report = @() $index = 0 foreach ($Item in $dd) { [array]$report += [PSCustomObject]@{ Date = $dd[$index] } $index++ } $report | Export-Csv -Path "C:\Users\Abby\De
$dd = @(Get-Date -Format d)
$report = @()
$index = 0
foreach ($Item in $dd) {
[array]$report += [PSCustomObject]@{
Date = $dd[$index]
}
$index++
}
$report | Export-Csv -Path "C:\Users\Abby\Desktop\test.txt" -NoTypeInformation
运行代码后,我得到了如下结果:
"IsReadOnly","IsFixedSize","IsSynchronized","Keys","Values","SyncRoot","Count"
"False","False","False","System.Collections.Hashtable+KeyCollection","System.Collections.Hashtable+ValueCollection","System.Object","5"
"Date"
"11/3/2017"
“IsReadOnly”、“IsFixedSize”、“IsSynchronized”、“Keys”、“Values”、“SyncRoot”、“Count”
“False”、“False”、“False”、“System.Collections.Hashtable+KeyCollection”、“System.Collections.Hashtable+ValueCollection”、“System.Object”、“5”
但我想要一个合适的CSV文件,如下所示:
"IsReadOnly","IsFixedSize","IsSynchronized","Keys","Values","SyncRoot","Count"
"False","False","False","System.Collections.Hashtable+KeyCollection","System.Collections.Hashtable+ValueCollection","System.Object","5"
"Date"
"11/3/2017"
“日期”
"11/3/2017"
PowerShell可以吗?您发布的代码应该完全符合您的要求。但是,它使用的是PowerShell v3中引入的
[PSCustomObject]
类型加速器,而Windows7则使用PowerShell v2
您基本上有3种选择:
- 升级到PowerShell v3或更新版本
- 使用
cmdlet而不是新对象
类型加速器:[PSCustomObject]
$dd | ForEach-Object { New-Object -Type PSObject -Property @{ Date = $dd[$index] } } | Export-Csv ...
- 使用:
$dd
将只包含1个值,即当前日期。我删除了我的答案,因为你的答案更好。