Convert part of slmgr.vbs to vb.net

Discussion in 'Scripting' started by Stannieman, Sep 29, 2010.

  1. Stannieman

    Stannieman MDL Guru

    Sep 4, 2009
    2,232
    1,800
    90
    #1 Stannieman, Sep 29, 2010
    Last edited by a moderator: Apr 20, 2017
    I'm trying to rearm windows from within a vb.net app without the need of slmgr.vbs, so I'd like this code to be in vb.net:
    Code:
    Dim objService
    
        set objService = GetServiceObject("Version")
        On Error Resume Next
    
        objService.ReArmWindows()
        QuitIfError()
    
        LineOut GetResource("L_MsgRearm_1")
        LineOut GetResource("L_MsgRearm_2")
    So without all the carbage this should rearm:
    Code:
    Dim objService
    
        set objService = GetServiceObject("Version")
        
        objService.ReArmWindows()
    But pasting this in notepad and running as vbs throws the error:
    Types don't match: 'GetServiceObject'
    GetServiceObject also isn't recognized by vs.
    Is there anyone who know's how to do it?

    Thanks,
    Stannieman
     
    Stop hovering to collapse... Click to collapse... Hover to expand... Click to expand...
  2. ndog

    ndog MDL Novice

    Jul 18, 2009
    22
    8
    0
    #2 ndog, Mar 17, 2012
    Last edited by a moderator: Apr 20, 2017


    Look at slmgr.vbs again. There is a whole func you are missing

    Code:
    Function GetServiceObject(strQuery)
        Dim objService
        Dim colServices
    
        On Error Resume Next
           
        Set colServices = g_objWMIService.ExecQuery("SELECT " & strQuery & " FROM " & ServiceClass)
        QuitIfError()
    
        For each objService in colServices
            QuitIfError()
            Exit For
        Next
    
        set GetServiceObject = objService    
    End Function
    
     
  3. ndog

    ndog MDL Novice

    Jul 18, 2009
    22
    8
    0
    #3 ndog, Mar 17, 2012
    Last edited by a moderator: Apr 20, 2017
    Just incase anyone stumbles across this after googling like I did, here it a simple way to change the license key for windows 7 pro MAK without using slmgr.vbs

    I hope by looking at this code you will be able to determine what is the minimal amount of vbs code you need to make your own solution, whether it is vb.net or autoit etc and be able to get the relevant parts out of slmgr.vbs if you need to add extra functionality.

    activatewin7.vbs
    Code:
    Option Explicit
    
    Dim g_objWMIService, g_strComputer, g_strUserName, g_strPassword, strProductKey
    g_strComputer = "."
    
    strProductKey = "HYF8J-CVRMY-CM74G-RPHKF-PW487"
    
    private const ServiceClass = "SoftwareLicensingService"
    Set g_objWMIService = GetObject("winmgmts:\\" & g_strComputer & "\root\cimv2")
    
    InstallProductKey strProductKey
    
    
    Private Sub InstallProductKey(strProductKey)
        Dim objService, objProduct
    
        On Error Resume Next
    
        set objService = GetServiceObject("Version")
        strVersion = objService.Version
        objService.InstallProductKey(strProductKey)
    End Sub
    
    Function GetServiceObject(strQuery)
        Dim objService
        Dim colServices
    
        On Error Resume Next
           
        Set colServices = g_objWMIService.ExecQuery("SELECT " & strQuery & " FROM " & ServiceClass)
    
        For each objService in colServices
            Exit For
        Next
    
        set GetServiceObject = objService    
    End Function
    
    
    activate_test.cmd
    Code:
    @echo off&cls
    
    cscript "%~dp0activatewin7.vbs"
    cscript "%windir%\system32\slmgr.vbs" /dlv
    
    pause
    
    You change the strProductKey in the vbs script and then use the batch file to test it. The batch script runs your cd key changer and then it uses slmgr.vbs to verify if the cd key has changed.

    Good luck for any other future coders
     
  4. CODYQX4

    CODYQX4 MDL Developer

    Sep 4, 2009
    4,803
    45,234
    150
    It might be easier to use the WMICodeCreator to generate code to call the WMI functions (and debug and add to it), as the script is nothing more than an inteface to WMI, and a lot of slmgr/ospp script is not needed for the most basic functions.
     
    Stop hovering to collapse... Click to collapse... Hover to expand... Click to expand...