我有以下的Powershell脚本,我只拉出前两列,因为无法正确分离其他列。我想强制我的导入总是分成7列,每个数据集都包含在内。
My Current PS Script:
$serverNames = Get-Content "C$\app\test\ServerList2.txt"
$ServerList = @()
foreach ($serverName in $serverNames) {
$ServerList += Import-Csv "C$\app\test\$serverName.txt" -Header "Extension","Server IP","TRUE","NIC","Comments","PCName" |
Select-Object *, @{n='Server Name';e={$serverName}}
}
$ServerList
我的两个示例文件: 样本1:
; sample entries
; StationID, MAC Address, Recording Enabled
; AddressType = MAC
; 5201, 00.0d.29.0b.cd.34, TRUE
; 5202, 00.0D.29.0B.D9.30, TRUE
; StationID, IP Address, Recording Enabled
AddressType = IP4
10000, 192.168.1.137, TRUE, 1 ; My Comments PCName
10001, 192.168.1.28, TRUE ;My Comments
10003, 192.168.1.63, TRUE,2 ; My Comments PCName
样本2:
; sample entries
; StationID, MAC Address, Recording Enabled
; AddressType = MAC
; 5201, 00.0d.29.0b.cd.34, TRUE
; 5202, 00.0D.29.0B.D9.30, TRUE
; StationID, IP Address, Recording Enabled
AddressType = IP4
10010, 192.168.1.29, TRUE,1 ; My Comments 4
10040, 192.168.1.7, TRUE ; My Comments 5 PCName
10100, 192.168.1.14, TRUE,2 ; My Comments 6 PCName
我关注的数据只是以数字开头的行。正如您所知,并非所有行都具有相同的分隔符,这增加了复杂性。我试图让输出看起来如下所示。
Extension IP Address TRUE NIC Comments PC Name Server Name
10000 192.168.1.137 TRUE 1 My Comments 1 PCName1 Server1
10001 192.168.1.28 TRUE My Comments 2 Server1
10003 192.168.1.63 TRUE 2 My Comments 3 PCName3 Server1
10010 192.168.1.29 TRUE 1 My Comments 4 Server2
10040 192.168.1.7 TRUE My Comments 5 PCName5 Server2
10100 192.168.1.14 TRUE 2 My Comments 6 PCName6 Server2
答案 0 :(得分:2)
Import-CSV
(以及一般的CSV格式)使用单个分隔符。如果您的文件有多个分隔符和“可选”字段(不是空值,但缺少值),那么它不是CSV。最好的方法是使用正则表达式创建自定义解析器。实施例
$regex = '^(?<Extension>.+?),\s?(?<IPAddress>.+?),\s+(?<TRUE>\w+?)(?:,\s?(?<NIC>\d))?\s+?;\s?(?<Comments>.+?)(?:\s{2,}(?<PCName>\w+))?$'
$serverNames = Get-Content "C$\app\test\ServerList2.txt"
$ServerList = @()
foreach ($serverName in $serverNames) {
Get-Content -Path "C$\app\test\$serverName.txt" | ForEach-Object {
if($_ -match $regex) {
$ServerList += New-Object -TypeName psobject -Property $Matches |
Select-Object -Property "Extension","IPAddress","TRUE","NIC","Comments","PCName", @{n='Server Name';e={$serverName}}
}
}
}
$ServerList
正则表达式的演示和解释:Regex101