gpt4 book ai didi

powershell - 组合多行文本以形成 CSV 文件的每一行

转载 作者:行者123 更新时间:2023-12-02 23:38:19 29 4
gpt4 key购买 nike

我正在尝试清理的输出

cscript 'C:\Program Files (x86)\Microsoft Office\Office16\ospp.vbs' /dstatus

我只想显示许可证和 key 的最后 5 个字符,例如

Office16HomeBusinessR_Retail3 edition , 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 也明显比管道解决方案快):

$comp = "Computername"
$OfficLice = "C:\share\OfficLice.csv"

& {
switch -regex (cscript 'C:\Program Files (x86)\Microsoft Office\Office16\ospp.vbs' /dstatus $comp) {
'^LICENSE NAME: (.*)' {
# Create a new custom object, filled with just the license name
# for now.
$o = [pscustomobject] @{
Lic = $Matches[1]
Key = ''
}
}
'^Last 5 characters of installed product key: (.*)' {
# Add the license key info...
$o.Key = $Matches[1]
# ... and output the complete object
$o
}
}
} | Export-Csv -NoTypeInformation $OfficLice

请注意 Export-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包含正则表达式 ( (...) ) 中的第一个捕获组捕获的内容 - 在我们的例子中,分别是许可证名称和许可证 key 。
  • 第一个分支处理程序使用许可证名称初始化每个输出对象,第二个分支处理程序添加许可证 key 信息,然后输出对象。
  • switch 的总输出statement 是所有输出对象的数组,其元素一一传递到 Export-Csv .
  • 请注意,附上 switch & { ... } 中的声明- 即通过脚本 block 调用 - 是能够在管道中使用它所必需的。
  • 关于powershell - 组合多行文本以形成 CSV 文件的每一行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49077772/

    29 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com