• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 146
  • Last Modified:

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

0
hellblazeruk
Asked:
hellblazeruk
2 Solutions
 
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
 
Bill PrewCommented:
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: CompTIA Cloud+

The CompTIA Cloud+ Basic training course will teach you about cloud concepts and models, data storage, networking, and network infrastructure.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now