Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

RunAs with PassThrough Variables 1

Status
Not open for further replies.

tqeonline

MIS
Oct 5, 2009
304
US
Hey Guys - I hope this is an easy one, I couldn't find much searching.

I'm trying to run a Batch file via RunAs. That part works fine. The issue comes whenI try to add some variables (Start Date and End Date). It doesn't like them, won't even prompt me for a password.

Is there a way I can fix this? Or even just open up a run as CMD via VBS RunAs then use that same window to call the Batch with the Params?

Code Issues:
Code:
shell.Run "runas /user:<username> /netonly C:\Users\<username>\Desktop\MATS_Metrics_MC\MATS_Metrics.bat" '& fmtStartDate & fmtEndDate,1,True

context code:
Code:
Dim EndDate, StartDate, fmtEndDate, fmtStartDate, BatFileLoc, sPathToRunAs, Password, cmd

'Batch File to Run
BatFileLoc = "C:\Users\<username>\Desktop\MATS_Metrics_MC\MATS_Metrics.bat"

'Location to RunAs (to activate window)
sPathToRunAs = "C:\Windows\System32\runas.exe"

'Find the Last Friday
EndDate = DateAdd("d",-(Weekday(Date)+1),Date)

'StartDate would be the LastFriday -6 Days to get the Saturday before
StartDate = DateAdd("d",-6,EndDate)

'Format the dates Year, Month, Day (2013-09-04)
fmtEndDate = Year(EndDate) & "-" & Right(String(2,"0") & Month(EndDate),2) & "-" & Right(String(2,"0") & Day(EndDate),2)
fmtStartDate = Year(StartDate) & "-" & Right(String(2,"0") & Month(StartDate),2) & "-" & Right(String(2,"0") & Day(StartDate), 2)

'Enter Mooncake Password via InputBox
Password = inputbox("Enter Password","Password") & "~"

'Call the MDS Batch, Enter Password, Wait for it to complete
set shell=createobject("wscript.shell")

[COLOR=#CC0000]shell.Run "runas /user:<username> /netonly C:\Users\<username>\Desktop\MATS_Metrics_MC\MATS_Metrics.bat" '& fmtStartDate & fmtEndDate,1,True[/color]Wscript.Sleep 100

shell.SendKeys Password

- Matt

"If I must boast, I will boast of the things that show my weakness
 
shell.Run "runas /user:<username> /netonly ""C:\Users\<username>\Desktop\MATS_Metrics_MC\MATS_Metrics.bat " & fmtStartDate & " " & fmtEndDate & """", 1, True
 
Thanks Strong! I knew it was simple.

One correction though - Change the "True" to "False" - I have an input box prompting for password and if it is set to True the screen waits on the password, if you set it to false it doesn't wait and goes ahead and does the Send Keys.

Final version:
Code:
shell.Run "runas /user:<username> /netonly ""C:\Users\<username>\Desktop\MATS_Metrics_MC\MATS_Metrics.bat " & fmtStartDate & " " & fmtEndDate & """", 1, False

- Matt

"If I must boast, I will boast of the things that show my weakness
 
>One correction though - Change the "True" to "False"

I was just using your original code there.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top