use variable from .bat in .vsb

Hi,

I have the following batch file to stop / start a service running on a server

@echo off
echo "Enter Username" 
set /p USER=

echo "ENTER Password"
set /p PASS=

C:\psservice\psservice \\DC01 -u %USER% -p %PASS% stop "Gateway Service"

C:\psservice\psservice \\DC01 -u %USER% -p %PASS% start "Gateway Service"

cscript /nologo emailrestart.vbs

Open in new window


can the USER variable be used in the emailrestart.vbs as the from address?

'Script to send email

SMTPServer = "exchangesrv01"
Recipient = " "
From = " "
Subject = "Restart"
Message = "The Gateway Service has been restarted"

GenericSendmail SMTPserver, From, Recipient, Subject, Message

Sub GenericSendmail (SMTPserver, From, Recipient, Subject, Message)

set msg = WScript.CreateObject("CDO.Message")
msg.From = From
msg.To = Recipient
msg.Subject = Subject
msg.TextBody = Message
msg.Configuration.Fields ("http://schemas.microsoft.com/cdo/configuration/smtpserver") = SMTPServer
msg.Configuration.Fields("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2
msg.Configuration.Fields.Update
msg.Send
End Sub

Open in new window

hellblazerukAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

oBdACommented:
Sure:
'Script to send email
Set objShell = WScript.CreateObject ("WScript.Shell")
strUser = objShell.ExpandEnvironmentStrings("%USER%")

SMTPServer = "exchangesrv01"
Recipient = " "
From = strUser & "@domain.com"
Subject = "Restart"
Message = "The Gateway Service has been restarted"

GenericSendmail SMTPserver, From, Recipient, Subject, Message

Sub GenericSendmail (SMTPserver, From, Recipient, Subject, Message)
	set msg = WScript.CreateObject("CDO.Message")
	msg.From = From
	msg.To = Recipient
	msg.Subject = Subject
	msg.TextBody = Message
	msg.Configuration.Fields ("http://schemas.microsoft.com/cdo/configuration/smtpserver") = SMTPServer
	msg.Configuration.Fields("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2
	msg.Configuration.Fields.Update
	msg.Send
End Sub

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Bill PrewIT / Software Engineering ConsultantCommented:
Certainly reading the environment variable in the VBS is one approach, and will work.

I would suggest though that it might be better to pass the email address into the VBS via the command line rather than an environment variable.  Makes it easier to reuse the VBS code, and test it, etc.  And a little simpler than messing with the environemnt (although the VBS code is pretty simple in either case).

At any rate, here is an alternative to consider.

@echo off
echo "Enter Username" 
set /p USER=

echo "ENTER Password"
set /p PASS=

C:\psservice\psservice \\DC01 -u %USER% -p %PASS% stop "Gateway Service"

C:\psservice\psservice \\DC01 -u %USER% -p %PASS% start "Gateway Service"

cscript /nologo emailrestart.vbs %USER%

Open in new window


'Script to send email

' Get from address from command line
If WScript.Arguments.Count > 0 Then
    From = Wscript.Arguments(0)
Else
    WScript.StdErr.WriteLine "No sender email specified, can not send email."
    WScript.Quit
End If

SMTPServer = "exchangesrv01"
Recipient = " "
Subject = "Restart"
Message = "The Gateway Service has been restarted"

GenericSendmail SMTPserver, From, Recipient, Subject, Message

Sub GenericSendmail (SMTPserver, From, Recipient, Subject, Message)
    set msg = WScript.CreateObject("CDO.Message")
    msg.From = From
    msg.To = Recipient
    msg.Subject = Subject
    msg.TextBody = Message
    msg.Configuration.Fields ("http://schemas.microsoft.com/cdo/configuration/smtpserver") = SMTPServer
    msg.Configuration.Fields("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2
    msg.Configuration.Fields.Update
    msg.Send
End Sub

Open in new window

~bp
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
VB Script

From novice to tech pro — start learning today.