Solved

Running Subroutine with RunAs

Posted on 2011-09-28
7
385 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
Independent Software Vendors: 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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Suggested Solutions

Not long ago I saw a question in the VB Script forum that I thought would not take much time. You can read that question (Question ID  (http://www.experts-exchange.com/Programming/Languages/Visual_Basic/VB_Script/Q_28455246.html)28455246) Here (http…
Deploying a Microsoft Access application in a Citrix environment is not difficult but takes a few steps. However, Citrix system people are often of little help, as they typically know next to nothing about Access. The script provided here will take …

749 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