Solved

Running Subroutine with RunAs

Posted on 2011-09-28
7
388 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Script to copy or move mouse-selected collection of files plus targets referenced by shortcuts (.lnk) The purpose of this article is to help illuminate the real challenges and options available (where they may exist) for utilizing simple scriptin…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
How to Install VMware Tools in Red Hat Enterprise Linux 6.4 (RHEL 6.4) Step-by-Step Tutorial

732 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