Powershell 将多行中的文本组合成CSV文件的每一行
我正在努力清理这些文件的输出Powershell 将多行中的文本组合成CSV文件的每一行,powershell,csv,scripting,text-parsing,Powershell,Csv,Scripting,Text Parsing,我正在努力清理这些文件的输出 cscript 'C:\Program Files (x86)\Microsoft Office\Office16\ospp.vbs' /dstatus 我只想显示许可证和密钥的最后5个字符,例如 Office16 HomeBusinessr_Retail3版,7H67X 我有一些东西,但还不能完全拼凑起来 $comp = "Computername" $OfficLice = "C:\share\OfficLice.csv" $localtemp = "C:\s
cscript 'C:\Program Files (x86)\Microsoft Office\Office16\ospp.vbs' /dstatus
我只想显示许可证和密钥的最后5个字符,例如
Office16 HomeBusinessr_Retail3版,7H67X
我有一些东西,但还不能完全拼凑起来
$comp = "Computername"
$OfficLice = "C:\share\OfficLice.csv"
$localtemp = "C:\share\OfficLiceTemp.csv"
cscript 'C:\Program Files (x86)\Microsoft Office\Office16\ospp.vbs' /dstatus $comp | add-content $localtemp
$lic = get-content $localtemp | where {$_ -like "LICENSE NAME*"}
$key = get-content $localtemp | where {$_ -like "Last 5*"}
$key = foreach ($item in $key){
($item -split ' ')[7]}
foreach ($item in $lic){
($item -split ' ')[4] | add-content -Path $OfficLice.("License Name")
}
foreach ($item in $key){
$item | add-content -Path $OfficLice.Key
}
这是我要清理的东西的副本
Microsoft (R) Windows Script Host Version 5.812
Copyright (C) Microsoft Corporation. All rights reserved.
---Processing--------------------------
---------------------------------------
PRODUCT ID: 00333-59056-17787-AA631
SKU ID: 522f8458-7d49-42ff-a93b-670f6b6176ae
LICENSE NAME: Office 16, Office16HomeBusinessR_Retail3 edition
LICENSE DESCRIPTION: Office 16, RETAIL channel
LICENSE STATUS: ---LICENSED---
Last 5 characters of installed product key: 7H67X
---------------------------------------
PRODUCT ID: 00340-00000-00000-AA482
SKU ID: 971cd368-f2e1-49c1-aedd-330909ce18b6
LICENSE NAME: Office 16, Office16SkypeforBusinessEntryR_PrepidBypass edition
LICENSE DESCRIPTION: Office 16, RETAIL(Free) channel
LICENSE STATUS: ---LICENSED---
ERROR CODE: 0x4004FC05 (for information purposes only as the status is licensed)
ERROR DESCRIPTION: The Software Licensing Service reported that the application has a perpetual grace period.
Last 5 characters of installed product key: HT9YM
---------------------------------------
PRODUCT ID: 00198-20000-00000-AA054
SKU ID: ff693bf4-0276-4ddb-bb42-74ef1a0c9f4d
LICENSE NAME: Office 15, OfficeLyncEntryR_PrepidBypass edition
LICENSE DESCRIPTION: Office 15, RETAIL(Free) channel
LICENSE STATUS: ---LICENSED---
ERROR CODE: 0x4004FC05 (for information purposes only as the status is licensed)
ERROR DESCRIPTION: The Software Licensing Service reported that the application has a perpetual grace period.
Last 5 characters of installed product key: BPW98
---------------------------------------
---------------------------------------
---Exiting-----------------------------
下面是一个简化的解决方案,它使用
switch
语句处理带有正则表达式匹配的输入对象数组的能力(另外,switch
也明显快于管道解决方案):
请注意,导出Csv
默认使用ASCII编码;如果需要,使用-Encoding
参数
使用示例输入,生成的CSV文件如下所示:
"Lic","Key"
"Office 16, Office16HomeBusinessR_Retail3 edition","7H67X"
"Office 16, Office16SkypeforBusinessEntryR_PrepidBypass edition","HT9YM"
"Office 15, OfficeLyncEntryR_PrepidBypass edition","BPW98"
运行外部switch-regex(…)
命令并分别处理每个结果输出行cscript
- 由于
,分支条件被解释为正则表达式-regex
- 与使用
操作符类似,实际捕获的正则表达式记录在自动-match
变量中,该变量的索引$Matches
项包含捕获的正则表达式(1
)中的第一个捕获组-在本例中,是许可证名称和许可证密钥,分别(…)
- 第一个分支处理程序使用许可证名称初始化每个输出对象,第二个分支处理程序添加许可证密钥信息,然后输出对象
语句的总输出是所有输出对象的数组,这些对象的元素被逐个传递到switch
Export Csv
- 请注意,将
语句封装在开关
中(即通过脚本块调用)对于能够在管道中使用它是必要的&{…}
- 请注意,将
$comp = "Computername"
$OfficLice = "C:\share\OfficLice.csv"
$localtemp = cscript 'C:\Program Files (x86)\Microsoft Office\Office16\ospp.vbs' /dstatus $comp
$filteredOutput = $localtemp | Where-Object {($_ -like "License Name*") -or ($_ -like "Last 5*")}
$licenseInformation = @()
for ($i = 0; $i -lt $filteredOutput.Count; $i = $i + 2) {
$combinedLicenseInformation = [PSCustomObject]@{
LicenseName = ($filteredOutput[$i] -split ' ')[4];
Last5Characters = ($filteredOutput[$i + 1] -split ' ')[7]
}
$licenseInformation += $combinedLicenseInformation
}
$licenseInformation | Export-Csv -Path $OfficLice -NoTypeInformation
祝你周末愉快
$comp = "Computername"
$OfficLice = "C:\share\OfficLice.csv"
$localtemp = cscript 'C:\Program Files (x86)\Microsoft Office\Office16\ospp.vbs' /dstatus $comp
$filteredOutput = $localtemp | Where-Object {($_ -like "License Name*") -or ($_ -like "Last 5*")}
$licenseInformation = @()
for ($i = 0; $i -lt $filteredOutput.Count; $i = $i + 2) {
$combinedLicenseInformation = [PSCustomObject]@{
LicenseName = ($filteredOutput[$i] -split ' ')[4];
Last5Characters = ($filteredOutput[$i + 1] -split ' ')[7]
}
$licenseInformation += $combinedLicenseInformation
}
$licenseInformation | Export-Csv -Path $OfficLice -NoTypeInformation