Notification

Icon
Error

Using PsExec and slmgr.vbs to Activate Windows - Activate Windows remotely and silently using slmgr.vbs

Posted: Friday, January 9, 2015 11:35:07 PM(UTC)
keys_it

keys_it

Member Original PosterPosts: 31
11
Like
Hello all,

I thought I share a great custom action that uses a Vbscript that I created to aid in our OEM activation process if one of my tech's forget to activate Windows after using MDT to image a machine as we use OEM versions of Windows Pro. We have a mixed Win 7/8.1 environment so this was the need for using PsExec since a Win 8 slmgr.vbs script will not activate a Win 7 machine and vise versa. We utilize "Comments" page to record the OEM product key as part of our SOP when we acquire new computers to make a record of the OEM key and to easily copy and paste if we need to activate after an new image is applied. For OEM computers that shipped with Win 8 please visit this site to find out how to record the OEM key since I know people are going to ask.

This will even work for MAK keys as we use it activate our servers with the correct MAK after deployments.

In short, this script does the following: uninstalls current key, installs new key that you typed in, activates online, and then displays the licensing status for that computer.

You will need the following in your Lansweeper server's action path:
PsExec
activateWindows.vbs (create from the code below)

Here is the code:
Code:

'Pass computer name from Lansweeper to script
strComputer = WScript.Arguments(0)
 
'Instantiate variables 
Set objScript = CreateObject("WScript.Shell")
Set objRegExp = CreateObject("Vbscript.RegExp")
 
' Define Regular expression
objRegExp.Global = True
objRegExp.Pattern = "^[a-z0-9]{5}(-[a-z0-9]{5}){4}$"
objRegExp.IgnoreCase = True
 
Do
    ' Input new product key
    sKeyLoop = 0
    Dim sProductKey
    sProductKey = InputBox("Please enter the Product Key for this computer with the -'s: ", "New Product Key", "")
    If trim(sProductKey) = "" Then 'If blank
        Wscript.Quit
    End If
    sProductKey = Replace(sProductKey, " ", "")
    sProductKey = trim(UCase(sProductKey))
     
    'Tests to see if valid product key format
    If objRegExp.Test(sProductKey) Then
        'Continues script
        Call ActivateWindows()
    Else
        Wscript.Echo ("Invalid entry.")
        sKeyLoop = 0
    End If
Loop While sKeyLoop = 0
 
'Function for activating windows using PsExec and slmgr.vbs
Function ActivateWindows()
    sCmdUninstall = "\\<servername>\lansweeper$\PSTools\PsExec.exe \\" & strComputer & " cscript.exe ""C:\windows\system32\slmgr.vbs"" /upk"
    sCmdInstall = "\\<servername>\lansweeper$\PSTools\PsExec.exe \\" & strComputer & "  cscript.exe ""C:\windows\system32\slmgr.vbs"" /ipk " & sProductKey
    sCmdActivate = "\\<servername>\lansweeper$\PSTools\PsExec.exe \\" & strComputer & "  cscript.exe ""C:\windows\system32\slmgr.vbs"" /ato"
    sCmdViewLicense = "\\<servername>\lansweeper$\PsExec.exe \\" & strComputer & "  cscript.exe ""C:\windows\system32\slmgr.vbs"" /dli"
     
    'Run cmds
    objScript.Run sCmdUninstall, 0, True
    objScript.Run sCmdInstall, 0, True
    objScript.Run sCmdActivate, 0, True
    objScript.Run ("%comspec% /K " & sCmdViewLicense)
    Wscript.Quit
End Function

Here is the action code for the Asset Pages:
Code:
Code:

{actionpath}activateWindows.vbs "{computer}"


I hope you enjoy Dancing !

Keys_IT
TheITGuy
#1TheITGuy Member Posts: 32  
posted: 2/16/2016 11:03:57 PM(UTC)
Dude, sweet script! Almost 5k views and only 1 like? (now 2) C'mon people!!! ;)
TheITGuy
#2TheITGuy Member Posts: 32  
posted: 2/25/2016 3:19:14 PM(UTC)
This also worked for me on Windows 10!
King Dad
#3King Dad Member Posts: 10  
posted: 4/29/2016 5:01:59 PM(UTC)
Dumb question but I am not much of a script guy. I get the pop up asking for the Key and I enter it but it says invalid entry. We are using MAK keys. I am confused about the "Please enter the product Key with the -s:". Does that mean my entry should be "-s:XXXXX-XXXXX-XXXXX-XXXXX" without the quotes? I tried it all different ways, but can't seem to get it to work. Oh, and this is for Windows 7 Enterprise.
TheITGuy
#4TheITGuy Member Posts: 32  
posted: 4/29/2016 5:34:56 PM(UTC)
Don't use quotes. And use the dashes between the numbers. They just abbreviated it by saying -s. Remove the -s from the key.
King Dad
#5King Dad Member Posts: 10  
posted: 4/29/2016 6:04:50 PM(UTC)
Sorry for my ignorance on this. Here is the error I am getting "Line 42/ Char 5/ The Network path was not found" and here is the script:


'Pass computer name from Lansweeper to script
strComputer = WScript.Arguments(0)

'Instantiate variables
Set objScript = CreateObject("WScript.Shell")
Set objRegExp = CreateObject("Vbscript.RegExp")

' Define Regular expression
objRegExp.Global = True
objRegExp.Pattern = "^[a-z0-9]{5}(-[a-z0-9]{5}){4}$"
objRegExp.IgnoreCase = True

Do
' Input new product key
sKeyLoop = 0
Dim sProductKey
sProductKey = InputBox("Please enter the Product Key for this computer with the -'s: ", "New Product Key", "")
If trim(sProductKey) = "" Then 'If blank
Wscript.Quit
End If
sProductKey = Replace(sProductKey, " ", "")
sProductKey = trim(UCase(sProductKey))

'Tests to see if valid product key format
If objRegExp.Test(sProductKey) Then
'Continues script
Call ActivateWindows()
Else
Wscript.Echo ("Invalid entry.")
sKeyLoop = 0
End If
Loop While sKeyLoop = 0

'Function for activating windows using PsExec and slmgr.vbs
Function ActivateWindows()
sCmdUninstall = "\\<servername>\lansweeper$\PSTools\PsExec.exe \\" & strComputer & " cscript.exe ""C:\windows\system32\slmgr.vbs"" /upk"
sCmdInstall = "\\<servername>\lansweeper$\PSTools\PsExec.exe \\" & strComputer & " cscript.exe ""C:\windows\system32\slmgr.vbs"" /ipk " & sProductKey
sCmdActivate = "\\<servername>\lansweeper$\PSTools\PsExec.exe \\" & strComputer & " cscript.exe ""C:\windows\system32\slmgr.vbs"" /ato"
sCmdViewLicense = "\\<servername>\lansweeper$\PsExec.exe \\" & strComputer & " cscript.exe ""C:\windows\system32\slmgr.vbs"" /dli"

'Run cmds
objScript.Run sCmdUninstall, 0, True
objScript.Run sCmdInstall, 0, True
objScript.Run sCmdActivate, 0, True
objScript.Run ("%comspec% /K " & sCmdViewLicense)
Wscript.Quit
End Function
King Dad attached the following image(s):
Script error.png
TheITGuy
#6TheITGuy Member Posts: 32  
posted: 4/30/2016 1:57:50 AM(UTC)
Make sure that where it says in the script that you posted that you change where it says:
"\\<servername>\lansweeper$\PSTools\PsExec.exe
you change where it says servername to the actual name of the server (the one you correctly blocked out in the image you posted)

also make sure there is a folder called PSTools in that shared folder lansweeper$ and that psexec.exe is also in that folder.

Make that servername change in the 4 spots.

Also make sure that PsExec is in the lansweeper$ folder because that is the way the script calls for it in xCmdViewLicense.

If your still having issues, you might have to run Psexec on the computer your trying to run this as, i had that issue with trying to run the script from a Windows 10 machine but it worked after that.
King Dad
#7King Dad Member Posts: 10  
posted: 5/3/2016 5:55:32 PM(UTC)
Thanks for the help! Running PSExec on the Windows 10 unit first fixed my issue. Thanks again!
kevinjt4
#8kevinjt4 Member Posts: 1  
posted: 11/2/2017 7:34:19 PM(UTC)
How long does it take for this script to overwrite the current Windows Key? The script no longer errors, so it appears to work. I am able to run it, enter in the new key and click ok and the window closes. But the key never changes to the key I enter.

Active Discussions

Lansweeper Scan User Exclusion / Define User OU for active scanning
by  Almada   Go to last post Go to first unread
Last post: 9/17/2021 6:43:34 PM(UTC)
Lansweeper sqlServerId is changing
by  Ciro Bizelli  
Go to last post Go to first unread
Last post: 9/17/2021 3:40:34 PM(UTC)
Lansweeper Slow Loading Lansweeper
by  Maikel Vanroelen   Go to last post Go to first unread
Last post: 9/17/2021 9:25:31 AM(UTC)
Lansweeper Sync information
by  Jay-IT  
Go to last post Go to first unread
Last post: 9/16/2021 9:20:11 PM(UTC)
Lansweeper "Unknown" exclusion does not exclude Unknown assets
by  Almada   Go to last post Go to first unread
Last post: 9/16/2021 7:18:43 PM(UTC)
Lansweeper multiple scanning servers with granular permission
by  FrankSc  
Go to last post Go to first unread
Last post: 9/16/2021 6:29:54 PM(UTC)
Lansweeper Separate helpdesk websites?
by  JCochran   Go to last post Go to first unread
Last post: 9/15/2021 2:51:03 PM(UTC)
Lansweeper Remove Ads
by  FrankSc  
Go to last post Go to first unread
Last post: 9/15/2021 12:30:18 PM(UTC)