Solved

Running Subroutine with RunAs

Posted on 2011-09-28
7
350 Views
Last Modified: 2012-05-12
I need to have this subroutine below to RunAs an admin username and password, I want to hard code the user and password into the subroutine:

'=================================================
' Timesheet Subroutine - Deletes timesheet on Allusers desktop
'=================================================
Sub TimeSheet
Dim WSHShell, objFSO, DesktopPath, strDesktop
Set WSHShell = WScript.CreateObject("WScript.Shell")
Set objFSO = CreateObject("Scripting.FileSystemObject")

' Read desktop path using WshSpecialFolders object
DesktopPath = WSHShell.SpecialFolders("AllUsersDesktop")

' Delete the Timesheet shortcut from desktop
objFSO.DeleteFile DesktopPath & "\Timesheet.LNK"

End Sub
0
Comment
Question by:jshilge
  • 4
  • 3
7 Comments
 
LVL 65

Expert Comment

by:RobSampson
ID: 36753426
Hi, you could use PSExec to shell out to a command that will delete that file for you.

Regards,

Rob.
Sub TimeSheet
	Dim objShell, objFSO
	Set objShell = CreateObject("WScript.Shell")
	Set objFSO = CreateObject("Scripting.FileSystemObject")
	
	strComputer = objShell.ExpandEnvironmentStrings("%COMPUTERNAME%")
	strPSExec = "C:\Tools\PSExec.exe"
	strUser = "domain\admin"
	strPassword = "password"
	strCommand = objFSO.GetFile(strPSExec).ShortPath & " -accepteula -u " & strUser & " -p " & strPassword & " \\" & strComputer & " cmd /c delete /y ""%allusersprofile%\desktop\TimeSheet.lnk"""
	objShell.Run strCommand, 0, True
End Sub

Open in new window

0
 

Author Comment

by:jshilge
ID: 36763983
Doesn't psexec need to be installed beforehand?
0
 
LVL 65

Expert Comment

by:RobSampson
ID: 36765374
Kind of.  PSExec is just a stand along executable, but it does install a very small service during its execution, but removes it once finished.  The EXE file is all you need.

Rob.
0
Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

 

Author Comment

by:jshilge
ID: 36816549
I forgot to mention that this logon script is located on 5 domain controllers all at different locations, if possible id like to not have it copy and install a file and then uninstall, the logon process would take very long.
0
 
LVL 65

Expert Comment

by:RobSampson
ID: 36818577
You can run the PSExec file from the Netlogon share if you want to.

Sub TimeSheet
	Dim objShell, objFSO
	Set objShell = CreateObject("WScript.Shell")
	Set objFSO = CreateObject("Scripting.FileSystemObject")
	
	strComputer = objShell.ExpandEnvironmentStrings("%COMPUTERNAME%")
	strPSExec = objShell.ExpandEnvironmentStrings("%LOGONSERVER%") & "\NetLogon\PSExec.exe"
	strUser = "domain\admin"
	strPassword = "password"
	strCommand = objFSO.GetFile(strPSExec).ShortPath & " -accepteula -u " & strUser & " -p " & strPassword & " \\" & strComputer & " cmd /c delete /y ""%allusersprofile%\desktop\TimeSheet.lnk"""
	objShell.Run strCommand, 0, True
End Sub

Open in new window


Rob.
0
 

Author Comment

by:jshilge
ID: 36945157
OK so instead of using a logon script with VBscript I figured out how to remove the shortcut with a group policy object. However, for some reason some computer will not alway automatically update, and even sometimes if a user with no admin rights does the command gpupdate.exe from command prompt, it doesnt always work, so is it possible to add a subroutine without psexec in my logon scripts to run gupdate with administrator rights?
0
 
LVL 65

Accepted Solution

by:
RobSampson earned 500 total points
ID: 36945415
If you put this in as a StartUp Script (not a Logon Script) it will run as the local SYSTEM account, and remove the shortcut.

Regards,

Rob.
Dim objFSO, objShell
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objShell = CreateObject("WScript.Shell")
strFile = objShell.ExpandEnvironmentStrings("%allusersprofile%") & "\Desktop\TimeSheet.lnk"
If objFSO.FileExists(strFile) = True Then objFSO.DeleteFile strFile, True

Open in new window

0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Unlike scripting languages such as C# where a semi-colon is used to indicate the end of a command, Microsoft's VBScript language relies on line breaks to determine when a command begins and ends. As you can imagine, this quickly results in messy cod…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

758 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now