none
VBS Script input variable and use with wmic to apply value to registry

    General discussion

  • I'm attempting to re-write a script to change the last logged on user using a previous script as below:

    '--------------
    'Start of UAC workaround code

    If WScript.Arguments.length =0 Then
      Set objShell = CreateObject("Shell.Application")

      objShell.ShellExecute "wscript.exe", Chr(34) & _
      WScript.ScriptFullName & Chr(34) & " uac", "", "runas", 1
    Else

    '--------------
    'Start of code

        dim WSHShell
        Set WSHShell = Wscript.CreateObject("WScript.Shell")
        dim strRegKey
        strComputerName = wshShell.ExpandEnvironmentStrings( "%COMPUTERNAME%" )
        strRegKey = "HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Authentication\LogonUI\"

        StrUser = InputBox("Please enter in username," & vbCrLf & vbCrLf & "e.g. joe.local", "Set Last logged on", "UserName")
        StrDomain = InputBox("Please enter in domain for logon," & vbCrLf & vbCrLf & "e.g. DOMAIN", "Set Logon Domain OR leave blank if a local user account", "Domain.local")

        If StrDomain = "" then
             StrDomain = "."
           Else
        End If

        wshShell.RegWrite strRegKey & "LastLoggedOnUser", StrDomain & "\" & StrUser, "REG_SZ"
        wshShell.RegWrite strRegKey & "LastLoggedOnSAMUser", StrDomain & "\" & StrUser, "REG_SZ"


        WScript.Echo "Setup Completed. Please restart the computer to complete the process


    '--------------
    'End of code

    '--------------
    'End of UAC workaround code

    I need to use the StrUser Variable in a couple of wmic queries as Windows 10 needs more information to do the change!:

    wmic useraccount where name=StrUser get sid
    wmic useraccount where name=StrUser get fullname

    The outputs from those queries I want to then apply to a couple of registry keys. I don't know how to pass the variable to the query or capture the result?

    Tuesday, November 6, 2018 2:09 PM

All replies

  • Sorry but we don't customize code on request.

    WMIC is not a script it is a system utility program.

    You can call WMI from VBScript easily and avoid using WMIC.

    With either you have to call the DC to get a domain account SID.

    With PowerShell you can do this with only a few lines.

    Start here: Microsoft Virtual Academy - Getting Started with Microsoft PowerShell

    VBScript WMI to get user account:

    Set objWMIService = GetObject("root\CIMV2")
    Set colItems = objWMIService.ExecQuery("Select * from Win32_UserAccount Where Name ='" & strUsername "'")
    For Each objItem in colItems
    	WScript.Echo "AccountType: " & objItem.AccountType
    	WScript.Echo "Caption: " & objItem.Caption
    	WScript.Echo "Description: " & objItem.Description
    	WScript.Echo "Disabled: " & objItem.Disabled
    	WScript.Echo "Domain: " & objItem.Domain
    	WScript.Echo "FullName: " & objItem.FullName
    	WScript.Echo "InstallDate: " & objItem.InstallDate
    	WScript.Echo "LocalAccount: " & objItem.LocalAccount
    	WScript.Echo "Lockout: " & objItem.Lockout
    	WScript.Echo "Name: " & objItem.Name
    	WScript.Echo "PasswordChangeable: " & objItem.PasswordChangeable
    	WScript.Echo "PasswordExpires: " & objItem.PasswordExpires
    	WScript.Echo "PasswordRequired: " & objItem.PasswordRequired
    	WScript.Echo "SID: " & objItem.SID
    	WScript.Echo "SIDType: " & objItem.SIDType
    	WScript.Echo "Status: " & objItem.Status
    Next
    

    PowerShell WMI to get user account:

    $username = 'jsmith'
    Get-WmiObject Win32_UserAccount -Filter "Name='$username'"


    \_(ツ)_/

    Tuesday, November 6, 2018 2:34 PM
  • Of course,  that's a great help, I can def work on that.

    Thanks for the info!

    m

    Tuesday, November 6, 2018 2:54 PM