运行命令时,cscript "C:\Program Files\Microsoft Office\Office16\OSPP.VBS" /dstatus
您会得到以下输出(为保护隐私而隐藏的密钥):
Microsoft (R) Windows Script Host Version 5.812
Copyright (C) Microsoft Corporation. All rights reserved.
---Processing--------------------------
---------------------------------------
PRODUCT ID: 00403-90046-04636-AAOEM
SKU ID: 1ba73ccc-3b8a-42b7-9936-0fb1863ea6ec
LICENSE NAME: Office 19, Office19HomeBusiness2019R_OEM_Perp edition
LICENSE DESCRIPTION: Office 19, RETAIL channel
LICENSE STATUS: ---LICENSED---
Last 5 characters of installed product key: XXXXX
---------------------------------------
PRODUCT ID: 00200-70000-00000-AA691
SKU ID: d7279dd0-e175-49fe-a623-8fc2fc00afc4
LICENSE NAME: Office 16, Office16O365HomePremR_Grace edition
LICENSE DESCRIPTION: Office 16, RETAIL(Grace) channel
LICENSE STATUS: ---NOTIFICATIONS---
ERROR CODE: 0xC004F009
ERROR DESCRIPTION: The Software Licensing Service reported that the grace period expired.
Last 5 characters of installed product key: XXXXX
---------------------------------------
---------------------------------------
---Exiting-----------------------------
我正在尝试从命令输出中获取产品密钥的五位数字,以便可以在很多设备上运行它。我有这个脚本,它工作得很好,只是它获取的是宽限期的产品密钥,而不是实际的许可证。我试了一下,试图让它直接抓取“LICENSED”后面的一行,但搞不懂。
我目前拥有的:
@echo off
for /f "tokens=2 delims=:" %%G IN ('cscript "C:\Program Files\Microsoft Office\Office16\OSPP.VBS" /dstatus ^| findstr /C:"Last 5 characters of installed product key:"') do ( set "key=%%G" )
set "key=%key: =%"
echo %key%
答案1
文件内的这些命令.bat
将环境变量设置
last
为最后 5 个字符:
@echo off
for /f "tokens=*" %%I in ('cscript "C:\Program Files\Microsoft Office\Office16\OSPP.VBS" /dstatus ^| findstr /B "Last 5 characters of installed product key"') do for %%A in (%%~I) do set last=%%A
echo %last%
答案2
@echo off
for /f tokens^=2delims^=: %%i in ('
cscript "%ProgramFiles%\Microsoft Office\Office16\OSPP.VBS" /dstatus ^| find /i "key:"
')do set "_key=%%~i" && call set "_key=%%_key: =%%" && goto %:^)
%:ˆ)
echo\%_key%
尝试使用call set "_key=%%_key: =%%"
和&& goto :label