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
Solved

Running Subroutine with RunAs

Posted on 2011-09-28
7
376 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
Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

 

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

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 …
When you see single cell contains number and text, and you have to get any date out of it seems like cracking our heads.
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

808 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