我在文本文件中有以下几百个条目,如下所示:
DisplayName : John, Smith
UPN : MY3043241@domain.local
Status : DeviceOk
DeviceID : ApplC39HJ3JPDTF9
DeviceEnableOutboundSMS : False
DeviceMobileOperator :
DeviceAccessState : Allowed
DeviceAccessStateReason : Global
DeviceAccessControlRule :
DeviceType : iPhone
DeviceUserAgent : Apple-iPhone4C1/902.206
DeviceModel : iPhone
... about 1500 entries of the above with blank line between each.
我希望从上面创建一个具有以下标题的表:DisplayName,UPN,Status,DeviceID,DeviceEnableOutboundSMS,DeviceMobileOperator,DeviceAccessState,DeviceAccessStateReason,DeviceAccessControlRule,DeviceType,DeviceUserAgent,DeviceModel
问题是,有没有一种工具或简单的方法可以在excel或其他应用程序中做到这一点。我知道写一个简单的算法很容易,不幸的是我不能走这条路。Powershell是一个选择,但我不擅长,所以如果你有任何关于如何处理这条路线的建议,请告诉我
虽然我是Powershell一行程序的忠实粉丝,但这对尝试学习或开始使用它的人没有多大帮助。在企业环境中获得认可更是如此。
我写了一个cmdlet,包括文档,让你开始。
function Import-UserDevice {
Param
(
# Path of the text file we are importing records from.
[string] $Path
)
if (-not (Test-Path -Path $Path)) { throw "Data file not found: $Path" }
# Use the StreamReader for efficiency.
$reader = [System.IO.File]::OpenText($Path)
# Create the initial record.
$entry = New-Object -TypeName psobject
while(-not $reader.EndOfStream) {
# Trimming is necessary to remove empty spaces.
$line = $reader.ReadLine().Trim()
# An empty line would indicate we need to start a new record.
if ($line.Length -le 0 -and -not $reader.EndOfStream) {
# Output the completed record and prepare a new record.
$entry
$entry = New-Object -TypeName psobject
continue
}
# Split the line through ':' to get properties names and values.
$entry | Add-Member -MemberType NoteProperty -Name $line.Split(':')[0].Trim() -Value $line.Split(':')[1].Trim()
}
# Output the residual record.
$entry
# Close the file.
$reader.Close()
}
下面是一个如何使用它将记录导出到CSV的示例。
Import-UserDevice -Path C:tempdata.txt | Export-Csv C:TEMPreport.csv -NoTypeInformation
Powershell在这里回答。。。我使用了一个测试文件C:\Temp\test.txt,其中包含:
DisplayName : John, Smith
UPN : MY3043241@domain.local
Status : DeviceOk
DeviceID : ApplC39HJ3JPDTF9
DeviceEnableOutboundSMS : False
DeviceMobileOperator :
DeviceAccessState : Allowed
DeviceAccessStateReason : Global
DeviceAccessControlRule :
DeviceType : iPhone
DeviceUserAgent : Apple-iPhone4C1/902.206
DeviceModel : iPhone
DisplayName : Mary, Anderson
UPN : AR456789@domain.local
Status : DeviceOk
DeviceID : ApplC39HJ3JPDTF8
DeviceEnableOutboundSMS : False
DeviceMobileOperator :
DeviceAccessState : Allowed
DeviceAccessStateReason : Global
DeviceAccessControlRule :
DeviceType : iPhone
DeviceUserAgent : Apple-iPhone4C1/902.206
DeviceModel : iPhone
这样我就可以解析多个记录。然后,我针对这个脚本运行它,该脚本创建了一个空数组$users
,一次获取该文件的内容13行(12个字段+空行)。然后它创建一个没有属性的自定义对象。然后,对于13行中的每一行,如果该行不为空,它将为我们刚刚创建的对象创建一个新属性,其中名称是:
之前的所有内容,值是它之后的所有内容(从名称和值的末尾删除空格)。然后它将该对象添加到数组中。
$users=@()
gc c:temptest.log -ReadCount 13|%{
$User = new-object psobject
$_|?{!([string]::IsNullOrEmpty($_))}|%{
Add-Member -InputObject $User -MemberType NoteProperty -Name ($_.Split(":")[0].TrimEnd(" ")) -Value ($_.Split(":")[1].TrimEnd(" "))
}
$users+=$User
}
一旦填充了数组$Users
,就可以执行以下操作:
$Users | Export-CSV C:TempNewFile.csv -notypeinfo
这会给你一个你期望的CSV。
对于一个只有几百条记录的输入文件,我可能会读取整个文件,将其拆分为空行,将文本块拆分为换行符,将行拆分为冒号。有点像这样:
$infile = 'C:pathtoinput.txt'
$outfile = 'C:pathtooutput.csv'
[IO.File]::ReadAllText($infile).Trim() -split "`r`n`r`n" | % {
$o = New-Object -Type PSObject
$_.Trim() -split "`r`n" | % {
$a = "$_ :" -split 's*:s*'
$o | Add-Member -Type NoteProperty -Name $a[0] -Value $a[1]
}
$o
} | Export-Csv $outfile -NoType