Slmgr /dlv gives a list of licenses on any given computer. One is for the OS, one is called an "ESU" key, there are others depending on your system. I'm attempting to grab the first instance of an Activation ID that comes after the text "ESU", so I'm not grabbing the OS Activation key or any other for that matter.
With this code, I think it's grabbing the second instance of Activation ID after the text "ESU" or it's just grabbing the wrong key altogether. What am I doing wrong? Thank you in advance.
$Slmgr = cscript.exe C:\Windows\system32\slmgr.vbs /dlv
switch -Regex ($slmgr | Out-String){'(?s)ESU.+Activation ID: (.+?)(?=\r?\n)'{
[PSCustomObject]@{
ActivationID = $Matches[1]
}
}
}
UPDATE: This is the result of slmgr /dlv below. It was grabbing the first instance of the Activation ID from the OS Key, but now it's grabbing the correct key from ESU section using (?s)ESU.+?Activation ID:\s*([^\r\n]+) below in the comments.
http://imgur.com/gallery/BIJcyqM
But, the output is showing "Name", and "Value", where is I just need the # that is obfuscated alone to process it later.
http://imgur.com/gallery/gVPMeHM
Update
Apparently, on certain systems $Slmgr is an array, not a multiline string, so -match doesn’t work for a multiline regex.
I tried this, but it give me a bunch of additional info after the Activation key:
$Slmgr = cscript.exe C:\Windows\system32\slmgr.vbs /dlv
$Slmgr -match '(?s)ESU.+?Activation ID:\s*([^\r\n]+)'
$Slmgr -join "'r'n" -match '(?s)ESU.+?Activation ID:\s*([^\r\n]+)'
$ActivationID = $matches[1]
Looking at $ActivationID, it does have the Activation key in the beginning, then is shows “ ‘r ‘n “ that I have in my code, then a bunch of stuff after it as well like installation keys, certificate info. I think that my –join in my code is throwing something off.
Any ideas what I’m doing wrong or a better way of what I’m trying to accomplish?