首页 > 解决方案 > 如何制作脚本来检查计算机中的许可证?

问题描述

我正在尝试制作一个脚本来检查我的计算机中的许可证。我想得到这样的输出:

Name: Windows(R), Professional edition
 > PartialProductKey: 3V66T
 > LicenseStatus: 1
Name: Office 16, Office16ProPlusVL_KMS_Client edition
 > PartialProductKey: WFG99
 > LicenseStatus: 1

这是检查Name的代码:

for /f "tokens=2 delims==" %%b in ('"wmic path SoftwareLicensingProduct where (PartialProductKey is not null) get Name /value"') do (echo Name: %%b)

对于PartialProductKey:

for /f "tokens=2 delims==" %%b in ('"wmic path SoftwareLicensingProduct where (PartialProductKey is not null) get PartialProductKey /value"') do (echo PartialProductKey: %%b)

对于LicenseStatus

for /f "tokens=2 delims==" %%b in ('"wmic path SoftwareLicensingProduct where (PartialProductKey is not null) get LicenseStatus /value"') do (echo LicenseStatus: %%b)

但是如何在每个Name下方显示PartialProductKeyLicenseStatus?谢谢!

标签: batch-file

解决方案


wmic 对其输出做了一些奇怪的事情(它实际上是 and 之间的交互,添加了一个额外的before 。这种行为在 SO 中被广泛讨论,就像这里一样)。这就是为什么需要:WMICFOR /F\r\r\nfindstr /V /R "^$"

@echo off
setlocal EnableDelayedExpansion
FOR /F tokens^=*^ delims^=^ eol^= %%L in (
'"wmic path SoftwareLicensingProduct where (PartialProductKey is not null) get Name,PartialProductKey,LicenseStatus /format:list|findstr /V /R "^^$""'
) do (
    set "%%L"
    echo Name: !name!
    echo  ^> PartialProductKey: !PartialProductKey!
    echo  ^> LicenseStatus: !LicenseStatus!
)

推荐阅读