Solved

Running Subroutine with RunAs

Posted on 2011-09-28
7
393 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
Industry Leaders: 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

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!

Question has a verified solution.

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

This script will sweep a range of IP addresses (class c only, 255.255.255.0) and report to a log the version of office installed. What it does: 1.)      Creates log file in the directory the script is run from (if it doesn't already exist) 2.)      Sweep…
With User Account Control (UAC) enabled in Windows 7, one needs to open an elevated Command Prompt in order to run scripts under administrative privileges. Although the elevated Command Prompt accomplishes the task, the question How to run as script…
This video Micro Tutorial shows how to password-protect PDF files with free software. Many software products can do this, such as Adobe Acrobat (but not Adobe Reader), Nuance PaperPort, and Nuance Power PDF, but they are not free products. This vide…
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…

705 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