从文本文件创建 CSV 文件
Create a CSV file from text file
我的脚本输出任何托管网站的文本文件。文本文件如下所示:
Default
Settings
Layouts
Stackexchangelogon
我需要使用以下 headers:
将它们放入 CSV 文件中
- 类型
- 基地
- 状态
对于 Type,我需要网站名称,Base,我需要它有 IIS,对于 Status,我需要有 Monitored。
最终输出类似于,
Type Base Status
Default IIS Monitored
Settings IIS Monitored
Layouts IIS Monitored
Stackexchangelogon IIS Monitored
我已经在网上搜索过,但仍然对如何实现这一点感到困惑。由于 Windows 2003 主机,它需要在 PowerShell 2.0 上工作。
这是我目前所拥有的 -
$website = Get-content "D:\Websites.txt"
$CSV = @()
$row = New-Object System.Object
$website | ForEach-Object {
$row | Add-Member -MemberType NoteProperty -Force -Name "Type" -Value $_
$row | Add-Member -MemberType NoteProperty -Force -Name "Base" -Value "IIS"
$row | Add-Member -MemberType NoteProperty -Force -Name "Status" -Value "Monitored"
$CSV += $row
}
但它并没有添加每个网站。我得到了列表中最后一个的重复。结果:
Type Base Status
---- ---- ------
Stackexchangelogon IIS Monitored
Stackexchangelogon IIS Monitored
Stackexchangelogon IIS Monitored
Stackexchangelogon IIS Monitored
有什么想法吗?
使用-
解决
$website = Get-content "D:\Websites.txt"
$CSV = @()
$website | ForEach-Object {
$row = New-Object System.Object
$row | Add-Member -MemberType NoteProperty -Force -Name "Type" -Value $_
$row | Add-Member -MemberType NoteProperty -Force -Name "Base" -Value "IIS"
$row | Add-Member -MemberType NoteProperty -Force -Name "Status" -Value "Monitored"
$CSV += $row
}
$CSV | Export-Csv "d:\test.csv" -NoTypeInformation
您问题中的代码不起作用,因为您在循环外创建 $row
并仅在循环内修改这个对象。 $CSV += $row
然后将 reference 附加到数组的对象。因此,您的数组中充满了对同一对象的引用,并且输出显示了循环后该单个对象的最终状态。
有几种方法可以避免这种情况,例如通过每次迭代创建一个新对象(就像您在 中所做的那样),或者通过附加 clone数组的对象:
$CSV += $row.PSObject.Copy()
但是,这两种方法都不是最优的,因为它们没有正确使用管道。使用 calculated properties:
可以大大简化您的代码
Get-Content 'D:\Websites.txt' |
Select-Object @{n='Type';e={$_}}, @{n='Base';e={'IIS'}},
@{n='Status';e={'Monitored'}} |
Export-Csv 'D:\test.csv' -NoType
我的脚本输出任何托管网站的文本文件。文本文件如下所示:
Default Settings Layouts Stackexchangelogon
我需要使用以下 headers:
将它们放入 CSV 文件中- 类型
- 基地
- 状态
对于 Type,我需要网站名称,Base,我需要它有 IIS,对于 Status,我需要有 Monitored。
最终输出类似于,
Type Base Status Default IIS Monitored Settings IIS Monitored Layouts IIS Monitored Stackexchangelogon IIS Monitored
我已经在网上搜索过,但仍然对如何实现这一点感到困惑。由于 Windows 2003 主机,它需要在 PowerShell 2.0 上工作。
这是我目前所拥有的 -
$website = Get-content "D:\Websites.txt"
$CSV = @()
$row = New-Object System.Object
$website | ForEach-Object {
$row | Add-Member -MemberType NoteProperty -Force -Name "Type" -Value $_
$row | Add-Member -MemberType NoteProperty -Force -Name "Base" -Value "IIS"
$row | Add-Member -MemberType NoteProperty -Force -Name "Status" -Value "Monitored"
$CSV += $row
}
但它并没有添加每个网站。我得到了列表中最后一个的重复。结果:
Type Base Status ---- ---- ------ Stackexchangelogon IIS Monitored Stackexchangelogon IIS Monitored Stackexchangelogon IIS Monitored Stackexchangelogon IIS Monitored
有什么想法吗?
使用-
解决$website = Get-content "D:\Websites.txt"
$CSV = @()
$website | ForEach-Object {
$row = New-Object System.Object
$row | Add-Member -MemberType NoteProperty -Force -Name "Type" -Value $_
$row | Add-Member -MemberType NoteProperty -Force -Name "Base" -Value "IIS"
$row | Add-Member -MemberType NoteProperty -Force -Name "Status" -Value "Monitored"
$CSV += $row
}
$CSV | Export-Csv "d:\test.csv" -NoTypeInformation
您问题中的代码不起作用,因为您在循环外创建 $row
并仅在循环内修改这个对象。 $CSV += $row
然后将 reference 附加到数组的对象。因此,您的数组中充满了对同一对象的引用,并且输出显示了循环后该单个对象的最终状态。
有几种方法可以避免这种情况,例如通过每次迭代创建一个新对象(就像您在
$CSV += $row.PSObject.Copy()
但是,这两种方法都不是最优的,因为它们没有正确使用管道。使用 calculated properties:
可以大大简化您的代码Get-Content 'D:\Websites.txt' |
Select-Object @{n='Type';e={$_}}, @{n='Base';e={'IIS'}},
@{n='Status';e={'Monitored'}} |
Export-Csv 'D:\test.csv' -NoType