I'm trying to clean up the output of
cscript 'C:\Program Files (x86)\Microsoft Office\Office16\ospp.vbs' /dstatus
I want to display only the license and the last 5 chars of the key such as
Office16HomeBusinessR_Retail3 edition , 7H67X
I've got pieces of it, but can't quite bring it together.
$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
}
Here's a copy of what I'm trying to clean up
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-----------------------------
Here's a streamlined solution that uses the switch
statement's ability to process an array of input objects with regex matching (as a bonus, switch
is also noticeably faster than a pipeline solution):
$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
Note that Export-Csv
uses ASCII encoding by default; use the -Encoding
parameter, if needed.
With your sample input, the resulting CSV file looks like this:
"Lic","Key"
"Office 16, Office16HomeBusinessR_Retail3 edition","7H67X"
"Office 16, Office16SkypeforBusinessEntryR_PrepidBypass edition","HT9YM"
"Office 15, OfficeLyncEntryR_PrepidBypass edition","BPW98"
switch -regex (...)
runs the external cscript
command and processes each resulting output line individually.
Due to -regex
, the branch conditionals are interpreted as regular expressions.
Similar to using the -match
operator, what the regular expression actually captured is recorded in the automatic $Matches
variable, whose entry with index 1
contains what the 1st capture group inside the regex ((...)
) captured - in our case, the license name and the license key, respectively.
The 1st branch handler initializes each output object with the license name, and the 2nd one adds the license key information, after which the object is output.
The overall output from the switch
statement is the array of all output objects, whose elements are passed one by one through to Export-Csv
.
switch
statement in & { ... }
- i.e., invocation via a script block - is necessary for being able to use it in a pipeline.even thou I don't really like the way you try to solve this particular case, here is your code updated and hopefully providing expected results.
$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
Have a great weekend!
User contributions licensed under CC BY-SA 3.0