Solved

Task Scheduler running a .cmd

Posted on 2013-02-03
15
1,242 Views
Last Modified: 2013-02-10
Hi All,

We have resently moved from windows server 2003 to windows server 2008 service pack 1.

In Windows server 2008 I'm using task scheduler to run a .cmd script.Just as it was in window server 2003.

The first command in the script is to run another .cmd script.  Lets call it myenv.cmd.

Myenv.cmd sets the environment variables for 5 other scripts that also need to be scheduled and is called once from each of those 5 scrpts.

If I run it from the command line it works fine. If I run it through the scheduler or let the scheduler run it, the myenv.cmd runs but the variables on not set for the calling cmd script.

If I move all the setup commands from myenv.cmd to the calling cmd script it also works. I really don't want to add all the set up code to each of the 5 calling scripts. Is there another way?

Calling.cmd

:: this is my calling script
:: the first thing it does is call another cmd to set up environment variables.
call f:\myenv

:: do some more stuff
::
0
Comment
Question by:meagain0707
  • 9
  • 5
15 Comments
 
LVL 77

Expert Comment

by:arnold
ID: 38849832
One thing to consider is that the handling of the variables within a batch file are treated as local, have not checked at the time of writing, but an option you might consider is to define the variables as global.
The following may help illustrate.
http://www.computing.net/answers/programming/set-global-variable-in-batch-file/17671.html
0
 
LVL 80

Expert Comment

by:David Johnson, CD, MVP
ID: 38849867
In task scheduler must check run with highest priviledges
setenv.vbs
Option Explicit
Dim wshShell, wshSystemEnv
'Set  Environment Variables
Set WshShell = CreateObject( "WScript.Shell" )
Set wshSystemEnv = wshShell.Environment( "SYSTEM" )
' Display the current value
' Set the environment variable
wshSystemEnv( "MyVariable1" ) = "Variable1"
wshSystemEnv( "MyVariable2" ) = "Variable2"
wshSystemEnv( "MyVariable3" ) = "Variable3"
' Display the result
WScript.Echo "MyVariable1=" & wshSystemEnv( "MyVariable1" )
WScript.Echo "MyVariable2=" & wshSystemEnv( "MyVariable2" )
WScript.Echo "MyVariable3=" & wshSystemEnv( "MyVariable3" )
wshShell.Run "%comspec% /K C:\temp\checkenv.bat", 1, True
' Delete the environment variables
wshSystemEnv.Remove( "MyVariable1" )
wshSystemEnv.Remove( "MyVariable2" )
wshSystemEnv.Remove( "MyVariable3" )
' Display the result once more
WScript.Echo "MyVariable1=" & wshSystemEnv( "MyVariable1" )
WScript.Echo "MyVariable2=" & wshSystemEnv( "MyVariable2" )
WScript.Echo "MyVariable3=" & wshSystemEnv( "MyVariable3" )
Set wshSystemEnv = Nothing
Set wshShell     = Nothing
]

Open in new window


checkenv.bat
Echo MyVariable1 %MyVariable1%
Echo MyVariable2 %MyVariable2%
Echo MyVariable3 %MyVariable3%
pause
exit

Open in new window

0
 

Author Comment

by:meagain0707
ID: 38853738
ve3ofa,

Hello, and thanks for your response.

I have the check box checked for run with highest priviledges and alway did.

I tried running the script you entered above manually and it doesn't set the environment variables. I have attached a screen shot.

When I run the cmd file within the cmd as explained above it does work. But with your vbs script it won't even run manually. Please let me know if I'm missing something.

Thank you
0
Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

 
LVL 80

Expert Comment

by:David Johnson, CD, MVP
ID: 38853761
wshShell.Run "%comspec% /K C:\temp\checkenv.bat", 1, True

Did you see this?
0
 

Author Comment

by:meagain0707
ID: 38853779
Hi ve3ofa,

Yes I did, when it runs the second cmd window appears and the variables don't have the values.

I don't know .vbs and I'm very weak with scripting period.
In this command you are telling it to run the checkenv.bat.

I don't know what the %comspec% means or the /k, also what the 1 and true for?
0
 
LVL 80

Expert Comment

by:David Johnson, CD, MVP
ID: 38853855
Works here but it is SLOW in setting the Environmental Variables (runs at below normal priority)

http://www.screencast.com/t/CHJagGJ9pe2a
wshShell.Run "%comspec% /K C:\temp\checkenv.bat", 1, True

%comspec% /K loads the current command processor

,1,
       Activates and displays a window. If the window is minimized or maximized, the system restores it to its original size and position. An application should specify this flag when displaying the window for the first time

,True  -- waits for the called program to close
0
 

Author Comment

by:meagain0707
ID: 38853878
ve3ofa,

Hello again and thanks for doing that and your explanation.I have added another screen shot. I thought that when you run the checkenv.bat we should see the
"Variable1"  "Variable2"
"Variable3"
0
 

Author Comment

by:meagain0707
ID: 38853884
Hi ve3ofa,

Okay I watched it again and it flashed by very quickly. Okay it works for you but not for me.
0
 

Author Comment

by:meagain0707
ID: 38853951
Hi Ve3ofa,

I changed the variables to why1,why2 why3, so I could see clearly, its getting late.

I commented out the delete lines
wshSystemEnv.Remove( "MyVariable1" )
wshSystemEnv.Remove( "MyVariable2" )
wshSystemEnv.Remove( "MyVariable3" )

The first time I ran, the variables why1,why2,why3 did not show up in the display from checkenv.bat but did appear in the windows as why1, why2 and why 3.

Then I ran again with no changes and the variables are displayed, please see attachement.
0
 
LVL 80

Expert Comment

by:David Johnson, CD, MVP
ID: 38853960
I'm never getting any images or attachments!
0
 

Author Comment

by:meagain0707
ID: 38853972
Sorry here was the first attachment.
expert.doc
0
 

Author Comment

by:meagain0707
ID: 38853974
Ve3ofa,

It's attached here.
expert2.doc
0
 

Author Comment

by:meagain0707
ID: 38853979
Ve3ofa,


Are  you on service pack 1 or 2, I hope this isn't a bug from service pack 1?
0
 
LVL 80

Accepted Solution

by:
David Johnson, CD, MVP earned 500 total points
ID: 38858327
task scheduler command line

c:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe -NoProfile -File c:\scripts\setenv.ps1 -ExecutionPolicy Unrestricted

setenv.ps1
Write-output("Setting Variables")
#set MyVariable1 "Test #1"
Set-Item -path env:MyVariable1 -value ("Test #1") 
Set-Item -path env:MyVariable2 -value ("Test #2") 
Set-Item -path env:MyVariable3 -value ("Test #3") 

Write-output("Display the result")
$env:MyVariable1
$env:MyVariable2
$env:MyVariable3
c:\test\checkenv.bat
Set-Item -path env:MyVariable1 -value ("") 
Set-Item -path env:MyVariable2 -value ("") 
Set-Item -path env:MyVariable3 -value ("") 
Write-output("Afer removal")
$env:MyVariable1
$env:MyVariable2
$env:MyVariable3

Open in new window


checkenv.bat
Echo MyVariable1 is: %MyVariable1%
Echo MyVariable2 is: %MyVariable2%
Echo MyVariable3 is: %MyVariable3%
pause

Open in new window

http://www.screencast.com/t/J3Fv0insvI
0
 

Author Closing Comment

by:meagain0707
ID: 38873975
Thanks for your help. It was my set up, but trying to run your scripts helped me find it.
I just had to enter the start in optional under the action tab.
0

Featured Post

What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

Question has a verified solution.

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

Citrix XenApp, Internet Explorer 11 set to Enterprise Mode and using central hosted sites.xml file.
Know what services you can and cannot, should and should not combine on your server.
This tutorial will give a short introduction and overview of Backup Exec 2012 and how to navigate and perform basic functions. Click on the Backup Exec button in the upper left corner. From here, are global settings for the application such as conne…
This tutorial will walk an individual through the steps necessary to join and promote the first Windows Server 2012 domain controller into an Active Directory environment running on Windows Server 2008. Determine the location of the FSMO roles by lo…

809 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