Ibra86

Check-Activation-Status-wmi

May 1st, 2024
645
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Batch 21.38 KB | None | 0 0
  1. <!-- : class="re0">Begin batch script
  2. @setlocal DisableDelayedExpansion
  3. @echo off
  4. :: change to 1 to use VBScript to access WMI
  5. :: automatically enabled if wmic.exe is not installed
  6. set WMI_VBS=0
  7.  
  8. :: change to 1 to use Windows PowerShell to access WMI
  9. :: automatically enabled if wmic.exe and VBScript are not installed
  10. set WMI_PS=0
  11.  
  12. :: WMI_PS takes precedence over wmic.exe and WMI_VBS
  13. :: WMI_VBS takes precedence over wmic.exe
  14. @cls
  15. set _args=
  16. set _args=%*
  17. if not defined _args goto :NoProgArgs
  18. for %%A in (%_args%) do (
  19. if /i "%%A"=="-wow" set _rel1=1
  20. if /i "%%A"=="-arm" set _rel2=1
  21. )
  22. :NoProgArgs
  23. set "_cmdf=%~f0"
  24. if exist "%SystemRoot%\Sysnative\cmd.exe" if not defined _rel1 (
  25. setlocal EnableDelayedExpansion
  26. start %SystemRoot%\Sysnative\cmd.exe /c ""!_cmdf!" -wow"
  27. exit /b
  28. )
  29. if exist "%SystemRoot%\SysArm32\cmd.exe" if /i %PROCESSOR_ARCHITECTURE%==AMD64 if not defined _rel2 (
  30. setlocal EnableDelayedExpansion
  31. start %SystemRoot%\SysArm32\cmd.exe /c ""!_cmdf!" -arm"
  32. exit /b
  33. )
  34. color 07
  35. set "SysPath=%SystemRoot%\System32"
  36. set "Path=%SystemRoot%\System32;%SystemRoot%\System32\Wbem;%SystemRoot%\System32\WindowsPowerShell\v1.0\"
  37. if exist "%SystemRoot%\Sysnative\reg.exe" (
  38. set "SysPath=%SystemRoot%\Sysnative"
  39. set "Path=%SystemRoot%\Sysnative;%SystemRoot%\Sysnative\Wbem;%SystemRoot%\Sysnative\WindowsPowerShell\v1.0\;%Path%"
  40. )
  41. set "line2=************************************************************"
  42. set "line3=____________________________________________________________"
  43. set "_err===== ERROR ===="
  44. set "_psc=powershell -nop -c"
  45. set _acc=WMIC
  46.  
  47. set _cwmi=0
  48. for %%# in (wmic.exe) do @if not "%%~$PATH:#"=="" (
  49. wmic path Win32_ComputerSystem get CreationClassName /value 2>nul | find /i "ComputerSystem" 1>nul && set _cwmi=1
  50. )
  51.  
  52. set _prsh=1
  53. for %%# in (powershell.exe) do @if "%%~$PATH:#"=="" set _prsh=0
  54. if not exist "%SystemRoot%\System32\WindowsPowerShell\v1.0\powershell.exe" set _prsh=0
  55.  
  56. set _WSH=1
  57. reg query "HKCU\SOFTWARE\Microsoft\Windows Script Host\Settings" /v Enabled 2>nul | find /i "0x0" 1>nul && (set _WSH=0)
  58. reg query HKU\S-1-5-19 1>nul 2>nul && (
  59. reg query "HKLM\SOFTWARE\Microsoft\Windows Script Host\Settings" /v Enabled 2>nul | find /i "0x0" 1>nul && (set _WSH=0)
  60. )
  61.  
  62. if %_cwmi% EQU 0 if %WMI_PS% EQU 0 if %_WSH% EQU 1 if exist "%SysPath%\vbscript.dll" set WMI_VBS=1
  63. if %_cwmi% EQU 0 if %WMI_VBS% EQU 0 if %_prsh% EQU 1 set WMI_PS=1
  64. if %_cwmi% EQU 0 if %WMI_VBS% EQU 0 if %WMI_PS% EQU 0 goto :E_WMI
  65.  
  66. if %WMI_VBS% NEQ 0 if %WMI_PS% EQU 0 (
  67. if %_WSH% EQU 0 goto :E_WSH
  68. if not exist "%SysPath%\vbscript.dll" goto :E_VBS
  69. set _cwmi=0
  70. set _acc=VBS
  71. )
  72.  
  73. if %WMI_PS% NEQ 0 (
  74. if %_prsh% EQU 0 goto :E_PWS
  75. 2>nul %_psc% $ExecutionContext.SessionState.LanguageMode | find /i "Full" 1>nul || goto :E_PLM
  76. set _cwmi=0
  77. set WMI_VBS=0
  78. set _acc=PS
  79. )
  80.  
  81. title Check Activation Status [%_acc%]
  82.  
  83. set "_csg=cscript.exe //NoLogo //Job:WmiMulti "%~nx0?.wsf""
  84. set "_csq=cscript.exe //NoLogo //Job:WmiQuery "%~nx0?.wsf""
  85. set "_csx=cscript.exe //NoLogo //Job:XPDT "%~nx0?.wsf""
  86.  
  87. set wslp=SoftwareLicensingProduct
  88. set wsls=SoftwareLicensingService
  89. set oslp=OfficeSoftwareProtectionProduct
  90. set osls=OfficeSoftwareProtectionService
  91. set winApp=55c92734-d682-4d71-983e-d6ec3f16059f
  92. set o14App=59a52881-a989-479d-af46-f275c6370663
  93. set o15App=0ff1ce15-a989-479d-af46-f275c6370663
  94. for %%# in (wspp_get,ospp_get,cW1nd0ws,sppw,c0ff1ce15,sppo,osppsvc,ospp14,ospp15) do set "%%#="
  95. for /f "tokens=6 delims=[]. " %%# in ('ver') do set winbuild=%%#
  96. set "wspp_get=Description, DiscoveredKeyManagementServiceMachineName, DiscoveredKeyManagementServiceMachinePort, EvaluationEndDate, GracePeriodRemaining, ID, KeyManagementServiceMachine, KeyManagementServicePort, KeyManagementServiceProductKeyID, LicenseStatus, LicenseStatusReason, Name, PartialProductKey, ProductKeyID, VLActivationInterval, VLRenewalInterval"
  97. set "ospp_get=%wspp_get%"
  98. if %winbuild% GEQ 9200 set "wspp_get=%wspp_get%, KeyManagementServiceLookupDomain, VLActivationTypeEnabled"
  99. if %winbuild% GEQ 9600 set "wspp_get=%wspp_get%, DiscoveredKeyManagementServiceMachineIpAddress, ProductKeyChannel"
  100. set "_work=%~dp0"
  101. set "_batf=%~f0"
  102. set "_batp=%_batf:'=''%"
  103. set "_Local=%LocalAppData%"
  104. set _vNextLs=0
  105. setlocal EnableDelayedExpansion
  106. dir /b /s /a:-d "!_Local!\Microsoft\Office\Licenses\*" 1>nul 2>nul && set _vNextLs=1
  107. dir /b /s /a:-d "!ProgramData!\Microsoft\Office\Licenses\*" 1>nul 2>nul && set _vNextLs=1
  108. pushd "!_work!"
  109. setlocal DisableDelayedExpansion
  110. if %winbuild% LSS 9200 if not exist "%SystemRoot%\servicing\Packages\Microsoft-Windows-PowerShell-WTR-Package~*.mum" set _vNextLs=0
  111.  
  112. set OsppHook=1
  113. sc query osppsvc >nul 2>&1
  114. if %errorlevel% EQU 1060 set OsppHook=0
  115.  
  116. net start sppsvc /y >nul 2>&1
  117. call :casWpkey %wslp% %winApp% cW1nd0ws sppw
  118. if %winbuild% GEQ 9200 call :casWpkey %wslp% %o15App% c0ff1ce15 sppo
  119. if %OsppHook% NEQ 0 (
  120. net start osppsvc /y >nul 2>&1
  121. call :casWpkey %oslp% %o14App% osppsvc ospp14
  122. if %winbuild% LSS 9200 call :casWpkey %oslp% %o15App% osppsvc ospp15
  123. )
  124.  
  125. echo %line2%
  126. echo ***                   Windows Status                     ***
  127. echo %line2%
  128. if not defined cW1nd0ws (
  129. echo.
  130. echo Error: product key not found.
  131. goto :casWcon
  132. )
  133. set winID=1
  134. call :casWqr %wslp% %winApp% ID
  135. for /f "tokens=2 delims==" %%# in ('%_qr%') do (
  136.   set "chkID=%%#"
  137.   call :casWdet "%wslp%" "%wsls%" "%wspp_get%"
  138.   call :casWout
  139.   echo %line3%
  140.   echo.
  141. )
  142.  
  143. :casWcon
  144. set winID=0
  145. set verbose=1
  146. if not defined c0ff1ce15 (
  147. if defined osppsvc goto :casWospp
  148. goto :casWend
  149. )
  150. echo %line2%
  151. echo ***                   Office Status                      ***
  152. echo %line2%
  153. call :casWqr %wslp% %o15App% ID
  154. for /f "tokens=2 delims==" %%# in ('%_qr%') do (
  155.   set "chkID=%%#"
  156.   call :casWdet "%wslp%" "%wsls%" "%wspp_get%"
  157.   call :casWout
  158.   echo %line3%
  159.   echo.
  160. )
  161. set verbose=0
  162. if defined osppsvc goto :casWospp
  163. goto :casWend
  164.  
  165. :casWospp
  166. if %verbose% EQU 1 (
  167. echo %line2%
  168. echo ***                   Office Status                      ***
  169. echo %line2%
  170. )
  171. call :casWqr %oslp% %o15App% ID
  172. if defined ospp15 for /f "tokens=2 delims==" %%# in ('%_qr%') do (
  173.   set "chkID=%%#"
  174.   call :casWdet "%oslp%" "%osls%" "%ospp_get%"
  175.   call :casWout
  176.   echo %line3%
  177.   echo.
  178. )
  179. call :casWqr %oslp% %o14App% ID
  180. if defined ospp14 for /f "tokens=2 delims==" %%# in ('%_qr%') do (
  181.   set "chkID=%%#"
  182.   call :casWdet "%oslp%" "%osls%" "%ospp_get%"
  183.   call :casWout
  184.   echo %line3%
  185.   echo.
  186. )
  187. goto :casWend
  188.  
  189. :casWpkey
  190. call :casWqr %1 %2 ID
  191. %_qr% 2>nul | findstr /i ID 1>nul && (set %3=1&set %4=1)
  192. exit /b
  193.  
  194. :casWdet
  195. for %%# in (%~3) do set "%%#="
  196. if /i %~1==%oslp% for %%# in (DiscoveredKeyManagementServiceMachineIpAddress, KeyManagementServiceLookupDomain, ProductKeyChannel, VLActivationTypeEnabled) do set "%%#="
  197. set "cKmsClient="
  198. set "cTblClient="
  199. set "cAvmClient="
  200. set "ExpireMsg="
  201. set "_xpr="
  202. call :casWml "%~1" "%chkID%" "%~3"
  203. for /f "tokens=* delims=" %%# in ('%_qr%') do set "%%#"
  204.  
  205. set /a _gpr=(GracePeriodRemaining+1440-1)/1440
  206. echo %Description%| findstr /i VOLUME_KMSCLIENT 1>nul && (set cKmsClient=1&set _mTag=Volume)
  207. echo %Description%| findstr /i TIMEBASED_ 1>nul && (set cTblClient=1&set _mTag=Timebased)
  208. echo %Description%| findstr /i VIRTUAL_MACHINE_ACTIVATION 1>nul && (set cAvmClient=1&set _mTag=Automatic VM)
  209. cmd /c exit /b %LicenseStatusReason%
  210. set "LicenseReason=%=ExitCode%"
  211. set "LicenseMsg=Time remaining: %GracePeriodRemaining% minute(s) (%_gpr% day(s))"
  212. if %_gpr% GEQ 1 if %_WSH% EQU 1 if exist "%SysPath%\vbscript.dll" (
  213. for /f "tokens=* delims=" %%# in ('%_csx% %GracePeriodRemaining%') do set "_xpr=%%#"
  214. )
  215. if %_gpr% GEQ 1 if %_prsh% EQU 1 if not defined _xpr (
  216. for /f "tokens=* delims=" %%# in ('%_psc% "$([DateTime]::Now.addMinutes(%GracePeriodRemaining%)).ToString('yyyy-MM-dd hh:mm:ss tt')" 2^>nul') do set "_xpr=%%#"
  217. title Check Activation Status [%_acc%]
  218. )
  219.  
  220. if %LicenseStatus% EQU 0 (
  221. set "License=Unlicensed"
  222. set "LicenseMsg="
  223. )
  224. if %LicenseStatus% EQU 1 (
  225. set "License=Licensed"
  226. set "LicenseMsg="
  227. if %GracePeriodRemaining% EQU 0 (
  228.   if %winID% EQU 1 (set "ExpireMsg=The machine is permanently activated.") else (set "ExpireMsg=The product is permanently activated.")
  229.   ) else (
  230.   set "LicenseMsg=%_mTag% activation expiration: %GracePeriodRemaining% minute(s) (%_gpr% day(s))"
  231.   if defined _xpr set "ExpireMsg=%_mTag% activation will expire %_xpr%"
  232.   )
  233. )
  234. if %LicenseStatus% EQU 2 (
  235. set "License=Initial grace period"
  236. if defined _xpr set "ExpireMsg=Initial grace period ends %_xpr%"
  237. )
  238. if %LicenseStatus% EQU 3 (
  239. set "License=Additional grace period (KMS license expired or hardware out of tolerance)"
  240. if defined _xpr set "ExpireMsg=Additional grace period ends %_xpr%"
  241. )
  242. if %LicenseStatus% EQU 4 (
  243. set "License=Non-genuine grace period."
  244. if defined _xpr set "ExpireMsg=Non-genuine grace period ends %_xpr%"
  245. )
  246. if %LicenseStatus% EQU 6 (
  247. set "License=Extended grace period"
  248. if defined _xpr set "ExpireMsg=Extended grace period ends %_xpr%"
  249. )
  250. if %LicenseStatus% EQU 5 (
  251. set "License=Notification"
  252.   if "%LicenseReason%"=="C004F200" (set "LicenseMsg=Notification Reason: 0xC004F200 (non-genuine)."
  253.   ) else if "%LicenseReason%"=="C004F009" (set "LicenseMsg=Notification Reason: 0xC004F009 (grace time expired)."
  254.   ) else (set "LicenseMsg=Notification Reason: 0x%LicenseReason%"
  255.   )
  256. )
  257. if %LicenseStatus% GTR 6 (
  258. set "License=Unknown"
  259. set "LicenseMsg="
  260. )
  261. if not defined cKmsClient exit /b
  262.  
  263. if %KeyManagementServicePort%==0 set KeyManagementServicePort=1688
  264. set "KmsReg=Registered KMS machine name: %KeyManagementServiceMachine%:%KeyManagementServicePort%"
  265. if "%KeyManagementServiceMachine%"=="" set "KmsReg=Registered KMS machine name: KMS name not available"
  266.  
  267. if %DiscoveredKeyManagementServiceMachinePort%==0 set DiscoveredKeyManagementServiceMachinePort=1688
  268. set "KmsDns=KMS machine name from DNS: %DiscoveredKeyManagementServiceMachineName%:%DiscoveredKeyManagementServiceMachinePort%"
  269. if "%DiscoveredKeyManagementServiceMachineName%"=="" set "KmsDns=DNS auto-discovery: KMS name not available"
  270.  
  271. call :casWml "%~2" "ClientMachineID, KeyManagementServiceHostCaching"
  272. for /f "tokens=* delims=" %%# in ('%_qr%') do set "%%#"
  273. if /i %KeyManagementServiceHostCaching%==True (set KeyManagementServiceHostCaching=Enabled) else (set KeyManagementServiceHostCaching=Disabled)
  274.  
  275. if %winbuild% LSS 9200 exit /b
  276. if /i %~1==%oslp% exit /b
  277.  
  278. if "%KeyManagementServiceLookupDomain%"=="" set "KeyManagementServiceLookupDomain="
  279.  
  280. if %VLActivationTypeEnabled% EQU 3 (
  281. set VLActivationType=Token
  282. ) else if %VLActivationTypeEnabled% EQU 2 (
  283. set VLActivationType=KMS
  284. ) else if %VLActivationTypeEnabled% EQU 1 (
  285. set VLActivationType=AD
  286. ) else (
  287. set VLActivationType=All
  288. )
  289.  
  290. if %winbuild% LSS 9600 exit /b
  291. if "%DiscoveredKeyManagementServiceMachineIpAddress%"=="" set "DiscoveredKeyManagementServiceMachineIpAddress=not available"
  292. exit /b
  293.  
  294. :casWout
  295. echo.
  296. echo Name: %Name%
  297. echo Description: %Description%
  298. echo Activation ID: %ID%
  299. echo Extended PID: %ProductKeyID%
  300. if defined ProductKeyChannel echo Product Key Channel: %ProductKeyChannel%
  301. echo Partial Product Key: %PartialProductKey%
  302. echo License Status: %License%
  303. if defined LicenseMsg echo %LicenseMsg%
  304. if not %LicenseStatus%==0 if not %EvaluationEndDate:~0,8%==16010101 echo Evaluation End Date: %EvaluationEndDate:~0,4%-%EvaluationEndDate:~4,2%-%EvaluationEndDate:~6,2% %EvaluationEndDate:~8,2%:%EvaluationEndDate:~10,2% UTC
  305. if not defined cKmsClient (
  306. if defined ExpireMsg echo.&echo.    %ExpireMsg%
  307. exit /b
  308. )
  309. if defined VLActivationTypeEnabled echo Configured Activation Type: %VLActivationType%
  310. echo.
  311. if not %LicenseStatus%==1 (
  312. echo Please activate the product in order to update KMS client information values.
  313. exit /b
  314. )
  315. echo Most recent activation information:
  316. echo Key Management Service client information
  317. echo.    Client Machine ID (CMID): %ClientMachineID%
  318. echo.    %KmsDns%
  319. echo.    %KmsReg%
  320. if defined DiscoveredKeyManagementServiceMachineIpAddress echo.    KMS machine IP address: %DiscoveredKeyManagementServiceMachineIpAddress%
  321. echo.    KMS machine extended PID: %KeyManagementServiceProductKeyID%
  322. echo.    Activation interval: %VLActivationInterval% minutes
  323. echo.    Renewal interval: %VLRenewalInterval% minutes
  324. echo.    KMS host caching: %KeyManagementServiceHostCaching%
  325. if defined KeyManagementServiceLookupDomain echo.    KMS SRV record lookup domain: %KeyManagementServiceLookupDomain%
  326. if defined ExpireMsg echo.&echo.    %ExpireMsg%
  327. exit /b
  328.  
  329. :casWend
  330. if %_vNextLs% EQU 1 if %_prsh% EQU 1 (
  331. echo %line2%
  332. echo ***                  Office vNext Status                 ***
  333. echo %line2%
  334. setlocal EnableDelayedExpansion
  335. %_psc% "$f=[IO.File]::ReadAllText('!_batp!') -split ':vNextDiag\:.*';iex ($f[1])"
  336. title Check Activation Status [%_acc%]
  337. echo %line3%
  338. echo.
  339. )
  340. goto :casWexit
  341.  
  342. :E_VBS
  343. echo %_err%
  344. echo VBScript engine is not installed.
  345. echo It is required for this script to work.
  346. goto :casWexit
  347.  
  348. :E_WSH
  349. echo %_err%
  350. echo Windows Script Host is disabled.
  351. echo It is required for this script to work.
  352. goto :casWexit
  353.  
  354. :E_PWS
  355. echo %_err%
  356. echo Windows PowerShell is not installed.
  357. echo It is required for this script to work.
  358. goto :casWexit
  359.  
  360. :E_PLM
  361. echo %_err%
  362. echo Windows PowerShell is not properly responding.
  363. echo check if it is working, and not locked in Constrained Language Mode.
  364. goto :casWexit
  365.  
  366. :E_WMI
  367. echo %_err%
  368. echo This script require one of these to work:
  369. echo wmic.exe tool
  370. echo VBScript engine
  371. echo Windows PowerShell
  372. goto :casWexit
  373.  
  374. :casWexit
  375. echo.
  376. echo Press any key to exit.
  377. pause >nul
  378. exit /b
  379.  
  380. :casWqr
  381. if %_cwmi% EQU 1 (
  382. set "_qr=wmic path %1 where "ApplicationID='%2' and PartialProductKey is not null" get %3 /value"
  383. exit /b
  384. )
  385. if %WMI_VBS% NEQ 0 (
  386. set "_qr=%_csq% %1 "ApplicationID='%2' and PartialProductKey is not null" %3"
  387. exit /b
  388. )
  389. set _qr=%_psc% "(([WMISEARCHER]'SELECT %3 FROM %1 WHERE ApplicationID=''%2'' AND PartialProductKey IS NOT NULL').Get()).Properties | %% {$_.Name+'='+$_.Value}"
  390. exit /b
  391.  
  392. :casWml
  393. set "_mlxt="
  394. set "_mlxt=%~3"
  395. if %_cwmi% EQU 1 (
  396. set "_qr="wmic path %~1 get %~2 /value" ^| findstr ^="
  397. if defined _mlxt set "_qr="wmic path %~1 where ID='%~2' get %~3 /value" ^| findstr ^="
  398. exit /b
  399. )
  400. if %WMI_VBS% NEQ 0 (
  401. set "_qr=%_csg% %~1 "%~2""
  402. if defined _mlxt set "_qr=%_csg% %~1 "ID='%~2'" "%~3""
  403. exit /b
  404. )
  405. set _qr=%_psc% "(([WMISEARCHER]'SELECT %~2 FROM %~1').Get()).Properties | %% {$_.Name+'='+$_.Value}"
  406. if defined _mlxt set _qr=%_psc% "(([WMISEARCHER]'SELECT %~3 FROM %~1 WHERE ID=''%~2''').Get()).Properties | %% {$_.Name+'='+$_.Value}"
  407. exit /b
  408.  
  409. :vNextDiag:
  410. function PrintModePerPridFromRegistry
  411. {
  412.     $vNextRegkey = "HKCU:\SOFTWARE\Microsoft\Office\16.0\Common\Licensing\LicensingNext"
  413.     $vNextPrids = Get-Item -Path $vNextRegkey -ErrorAction Ignore | Select-Object -ExpandProperty 'property' | Where-Object -FilterScript {$_.ToLower() -like "*retail" -or $_.ToLower() -like "*volume"}
  414.     If ($vNextPrids -Eq $null)
  415.     {
  416.         Write-Host "No registry keys found."
  417.         Return
  418.     }
  419.     $vNextPrids | ForEach `
  420.     {
  421.         $mode = (Get-ItemProperty -Path $vNextRegkey -Name $_).$_
  422.         Switch ($mode)
  423.         {
  424.             2 { $mode = "vNext"; Break }
  425.             3 { $mode = "Device"; Break }
  426.             Default { $mode = "Legacy"; Break }
  427.         }
  428.         Write-Host $_ = $mode
  429.     }
  430. }
  431. function PrintSharedComputerLicensing
  432. {
  433.     $scaRegKey = "HKLM:\SOFTWARE\Microsoft\Office\ClickToRun\Configuration"
  434.     $scaValue = Get-ItemProperty -Path $scaRegKey -ErrorAction Ignore | Select-Object -ExpandProperty "SharedComputerLicensing" -ErrorAction Ignore
  435.     $scaRegKey2 = "HKLM:\SOFTWARE\Microsoft\Office\16.0\Common\Licensing"
  436.     $scaValue2 = Get-ItemProperty -Path $scaRegKey2 -ErrorAction Ignore | Select-Object -ExpandProperty "SharedComputerLicensing" -ErrorAction Ignore
  437.     $scaPolicyKey = "HKLM:\SOFTWARE\Policies\Microsoft\Office\16.0\Common\Licensing"
  438.     $scaPolicyValue = Get-ItemProperty -Path $scaPolicyKey -ErrorAction Ignore | Select-Object -ExpandProperty "SharedComputerLicensing" -ErrorAction Ignore
  439.     If ($scaValue -Eq $null -And $scaValue2 -Eq $null -And $scaPolicyValue -Eq $null)
  440.     {
  441.         Write-Host "No registry keys found."
  442.         Return
  443.     }
  444.     $scaModeValue = $scaValue -Or $scaValue2 -Or $scaPolicyValue
  445.     If ($scaModeValue -Eq 0)
  446.     {
  447.         $scaMode = "Disabled"
  448.     }
  449.     If ($scaModeValue -Eq 1)
  450.     {
  451.         $scaMode = "Enabled"
  452.     }
  453.     Write-Host "SharedComputerLicensing" = $scaMode
  454.     Write-Host
  455.     $tokenFiles = $null
  456.     $tokenPath = "${env:LOCALAPPDATA}\Microsoft\Office\16.0\Licensing"
  457.     If (Test-Path $tokenPath)
  458.     {
  459.         $tokenFiles = Get-ChildItem -Path $tokenPath -Recurse -File -Filter "*authString*"
  460.     }
  461.     If ($tokenFiles.length -Eq 0)
  462.     {
  463.         Write-Host "No tokens found."
  464.         Return
  465.     }
  466.     $tokenFiles | ForEach `
  467.     {
  468.         $tokenParts = (Get-Content -Encoding Unicode -Path $_.FullName).Split('_')
  469.         $output = [PSCustomObject] `
  470.             @{
  471.                 ACID = $tokenParts[0];
  472.                 User = $tokenParts[3]
  473.                 NotBefore = $tokenParts[4];
  474.                 NotAfter = $tokenParts[5];
  475.             } | ConvertTo-Json
  476.         Write-Host $output
  477.     }
  478. }
  479. function PrintLicensesInformation
  480. {
  481.     Param(
  482.         [ValidateSet("NUL", "Device")]
  483.         [String]$mode
  484.     )
  485.     If ($mode -Eq "NUL")
  486.     {
  487.         $licensePath = "${env:LOCALAPPDATA}\Microsoft\Office\Licenses"
  488.     }
  489.     ElseIf ($mode -Eq "Device")
  490.     {
  491.         $licensePath = "${env:PROGRAMDATA}\Microsoft\Office\Licenses"
  492.     }
  493.     $licenseFiles = $null
  494.     If (Test-Path $licensePath)
  495.     {
  496.         $licenseFiles = Get-ChildItem -Path $licensePath -Recurse -File
  497.     }
  498.     If ($licenseFiles.length -Eq 0)
  499.     {
  500.         Write-Host "No licenses found."
  501.         Return
  502.     }
  503.     $licenseFiles | ForEach `
  504.     {
  505.         $license = (Get-Content -Encoding Unicode $_.FullName | ConvertFrom-Json).License
  506.         $decodedLicense = [System.Text.Encoding]::UTF8.GetString([System.Convert]::FromBase64String($license)) | ConvertFrom-Json
  507.         $licenseType = $decodedLicense.LicenseType
  508.         If ($null -Ne $decodedLicense.ExpiresOn)
  509.         {
  510.             $expiry = [DateTime]::Parse($decodedLicense.ExpiresOn, $null, 48)
  511.         }
  512.         Else
  513.         {
  514.             $expiry = New-Object DateTime
  515.         }
  516.         $licenseState = $null
  517.         If ((Get-Date) -Gt (Get-Date $decodedLicense.MetaData.NotAfter))
  518.         {
  519.             $licenseState = "RFM"
  520.         }
  521.         ElseIf ((Get-Date) -Lt (Get-Date $expiry))
  522.         {
  523.             $licenseState = "Licensed"
  524.         }
  525.         Else
  526.         {
  527.             $licenseState = "Grace"
  528.         }
  529.         if ($mode -Eq "NUL")
  530.         {
  531.             $output = [PSCustomObject] `
  532.             @{
  533.                 Version = $_.Directory.Name
  534.                 Type = "User|${licenseType}";
  535.                 Product = $decodedLicense.ProductReleaseId;
  536.                 Acid = $decodedLicense.Acid;
  537.                 LicenseState = $licenseState;
  538.                 EntitlementStatus = $decodedLicense.Status;
  539.                 EntitlementExpiration = $decodedLicense.ExpiresOn;
  540.                 ReasonCode = $decodedLicense.ReasonCode;
  541.                 NotBefore = $decodedLicense.Metadata.NotBefore;
  542.                 NotAfter = $decodedLicense.Metadata.NotAfter;
  543.                 NextRenewal = $decodedLicense.Metadata.RenewAfter;
  544.                 TenantId = $decodedLicense.Metadata.TenantId;
  545.             } | ConvertTo-Json
  546.         }
  547.         ElseIf ($mode -Eq "Device")
  548.         {
  549.             $output = [PSCustomObject] `
  550.             @{
  551.                 Version = $_.Directory.Name
  552.                 Type = "Device|${licenseType}";
  553.                 Product = $decodedLicense.ProductReleaseId;
  554.                 Acid = $decodedLicense.Acid;
  555.                 DeviceId = $decodedLicense.Metadata.DeviceId;
  556.                 LicenseState = $licenseState;
  557.                 EntitlementStatus = $decodedLicense.Status;
  558.                 EntitlementExpiration = $decodedLicense.ExpiresOn;
  559.                 ReasonCode = $decodedLicense.ReasonCode;
  560.                 NotBefore = $decodedLicense.Metadata.NotBefore;
  561.                 NotAfter = $decodedLicense.Metadata.NotAfter;
  562.                 NextRenewal = $decodedLicense.Metadata.RenewAfter;
  563.                 TenantId = $decodedLicense.Metadata.TenantId;
  564.             } | ConvertTo-Json
  565.         }
  566.         Write-Output $output
  567.     }
  568. }
  569.     Write-Host
  570.     Write-Host "========== Mode per ProductReleaseId =========="
  571.     Write-Host
  572. PrintModePerPridFromRegistry
  573.     Write-Host
  574.     Write-Host "========== Shared Computer Licensing =========="
  575.     Write-Host
  576. PrintSharedComputerLicensing
  577.     Write-Host
  578.     Write-Host "========== vNext licenses =========="
  579.     Write-Host
  580. PrintLicensesInformation -Mode "NUL"
  581.     Write-Host
  582.     Write-Host "========== Device licenses =========="
  583.     Write-Host
  584. PrintLicensesInformation -Mode "Device"
  585. :vNextDiag:
  586.  
  587. ----- Begin wsf script --->
  588. <package>
  589.    <job id="WmiQuery">
  590.       <script language="VBScript">
  591.          If WScript.Arguments.Count = 3 Then
  592.             wExc = "Select " & WScript.Arguments.Item(2) & " from " & WScript.Arguments.Item(0) & " where " & WScript.Arguments.Item(1)
  593.             wGet = WScript.Arguments.Item(2)
  594.          Else
  595.             wExc = "Select " & WScript.Arguments.Item(1) & " from " & WScript.Arguments.Item(0)
  596.             wGet = WScript.Arguments.Item(1)
  597.          End If
  598.          Set objCol = GetObject("winmgmts:\\.\root\CIMV2").ExecQuery(wExc,,48)
  599.          For Each objItm in objCol
  600.             For each Prop in objItm.Properties_
  601.                If LCase(Prop.Name) = LCase(wGet) Then
  602.                   WScript.Echo Prop.Name & "=" & Prop.Value
  603.                   Exit For
  604.                End If
  605.             Next
  606.          Next
  607.       </script>
  608.    </job>
  609.    <job id="WmiMulti">
  610.       <script language="VBScript">
  611.          If WScript.Arguments.Count = 3 Then
  612.             wExc = "Select " & WScript.Arguments.Item(2) & " from " & WScript.Arguments.Item(0) & " where " & WScript.Arguments.Item(1)
  613.          Else
  614.             wExc = "Select " & WScript.Arguments.Item(1) & " from " & WScript.Arguments.Item(0)
  615.          End If
  616.          Set objCol = GetObject("winmgmts:\\.\root\CIMV2").ExecQuery(wExc,,48)
  617.          For Each objItm in objCol
  618.             For each Prop in objItm.Properties_
  619.                WScript.Echo Prop.Name & "=" & Prop.Value
  620.             Next
  621.          Next
  622.       </script>
  623.    </job>
  624.    <job id="XPDT">
  625.       <script language="VBScript">
  626.          WScript.Echo DateAdd("n", WScript.Arguments.Item(0), Now)
  627.       </script>
  628.    </job>
  629. </package>
Add Comment
Please, Sign In to add comment