0

I need when I log into Windows account of standard user, VBS file should be run automatically.

I know I can create a new string value in HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Run with data like

wscript.exe D:\my-script.vbs

But the operation that should be run in VBS file when I log into user account requires admin privs

How to resolve the task?

2 Answers 2

2

Using Windows Task Scheduler, you could create a task that runs at login of every user, with elevated privileges.

  • As admin, open the taskscheduler taskschd.msc

  • From the Action menu, select Create Task

  • On the General tab, select the user account that will run your task. This can be a normal user, or a system account such as SYSTEM or NETWORK. If you need elevated privileges, check Run with highest privileges

Task - general

  • On the Triggers tab, click New, set Begin the task to At log on, and Settings to Any user

New trigger

0
0

Just click on Windows key + R to open the Run Box and type shell:Common Startup and hit Enter

A special folder will be open for you.

Copy your vbscript inside it ! and Voila !


EDIT:

You can add this sub at the beginning of your vbscript in order to be run as Admin rights, and If UAC is enabled on the computer, something like this should work.


Call RunAsAdmin
'-------------------------------------------------------------------------------------------------------
Sub RunAsAdmin()
    If Not WScript.Arguments.Named.Exists("elevate") Then
        CreateObject("Shell.Application").ShellExecute WScript.FullName _
        , chr(34) & WScript.ScriptFullName & chr(34) & " /elevate", "", "runas", 1
        WScript.Quit
    End If
End Sub
'-------------------------------------------------------------------------------------------------------
' You actual code goes here
Msgbox "If UAC is enabled on the computer, something like this should work",vbinformation+vbSystemModal,"Test"

You can create a shortcut on the startup user folder and execute your action with admin privileges like this vbscript :


Const Title = "Create a shortcut on startup user folder for the current vbscript that can be executed with Admin privileges"
Call RunAsAdmin()
Create_Shortcut Array("Startup","MyvbscriptName",WScript.ScriptFullName)
'Create_Shortcut Array("AllUsersStartup","MyvbscriptName",WScript.ScriptFullName)
'-------------------------------------------------------------------------------------------------------
' You actual code goes here
Msgbox "If UAC is enabled on the computer, something like this should work",vbinformation+vbSystemModal,Title
'-------------------------------------------------------------------------------------------------------
Sub Create_Shortcut(rArgs) 
    Dim objShell,objShortCut,ObjShortcutPath,ShortcutName,ShortcutPath,ShortcutLocation
    Dim TargetPath,Arguments,IconLocation,Description,HotKey
    Set objShell = CreateObject("WScript.Shell")
    If UBound(rArgs) > 1 Then
        ShortcutLocation       = cstr(rArgs(0))
        ShortcutPath           = objShell.SpecialFolders(ShortcutLocation)
        ShortcutName           = cstr(rArgs(1))
        Set objShortCut        = objShell.CreateShortcut(ShortcutPath & "\" & ShortcutName & ".lnk")
        TargetPath             = objShell.ExpandEnvironmentStrings(rArgs(2))
        objShortCut.TargetPath = TargetPath 
        If ShortcutPath = "" Then 
            MsgBox "Error The Shortcut Path Does Not Exsists On Your System."_
            ,vbCritical+vbSystemModal,Title
            wscript.quit(1)
        End If
    End If

    If UBound(rArgs) > 2 Then
        Arguments = cstr(rArgs(3))
        objShortCut.Arguments = Arguments
    End If

    If UBound(rArgs) > 3 Then
        IconLocation = cstr(rArgs(4))
        ObjShortCut.IconLocation = IconLocation
    End If

    If UBound(rArgs) > 4 Then
        Description = cstr(rArgs(5))
        ObjShortCut.Description = Description
    End If
    
    If UBound(rArgs) > 5 Then
        HotKey = cstr(rArgs(6))
        ObjShortCut.HotKey = HotKey
    End If
    objShortCut.Save
    On Error Resume Next
    If Err.Number <> 0 Then
        ShowError() 
    Else 
        'objShell.Popup "The Shortcut "& chr(34) & ShortcutName & chr(34) &" is created Successfully !"& vbcrlf &_
        '"On " & chr(34) & ShortcutPath & chr(34),5,Title,vbInformation+vbSystemModal
    End If
End Sub
'-------------------------------------------------------------------------------------------------------
Sub ShowError()
    ErrDetail = "Description : " & Err.Description & vbCrlf & _
    "Error number : " & Err.Number & vbCrlf & _
    "Error source : " & Err.Source
    MsgBox ErrDetail,vbCritical+vbSystemModal,Title
    Err.clear
End Sub
'-------------------------------------------------------------------------------------------------------
Sub RunAsAdmin()
    If Not WScript.Arguments.Named.Exists("elevate") Then
        CreateObject("Shell.Application").ShellExecute WScript.FullName _
        , chr(34) & WScript.ScriptFullName & chr(34) & " /elevate", "", "runas", 1
        WScript.Quit
    End If
End Sub
'-------------------------------------------------------------------------------------------------------
8
  • As far as I know, this will run the script as the current user, not with admin privileges
    – Berend
    Commented Aug 12, 2022 at 7:20
  • Shell:Startup is for the current user; but shell:Common Startup is for all users and needs admins priveliges !
    – Hackoo
    Commented Aug 12, 2022 at 7:24
  • No, you do need admin privileges to put something in that folder. But whatever is there will run as the current user
    – Berend
    Commented Aug 12, 2022 at 7:28
  • May be i didn't understand very well the OP Request ? he tells that his vbscript needs an action that will be run as admin rights ? in this case i suggest to add a function to run his vbscript with admin rights and create a shortcut in this folder !
    – Hackoo
    Commented Aug 12, 2022 at 7:40
  • 1
    That may work, I haven't tried. I suspect it will ask the user for the admin password, correct (i.e. the same as going into the advanced properties of a shortcut, and checking 'Run as admin')? The question doesn't really say if that's okay or not.
    – Berend
    Commented Aug 12, 2022 at 9:13

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .