SQLTeam.com | Weblogs | Forums

XP_CMDSHELL doesn't work on user environmental variables


#1

HELLO,

I am trying to run a batch file from xp_cmdshell.

It is being run in batch file which uses user created environment variables(user and system variables) and xp_cmdshell does not recognize those environment variable...

In sql client, if i give EXEC xp_cmdshell 'echo %PATH%'
Then it properly displays the content of path variable.

However, i create a env variable, say TEST and assign value as C:\Test and try to execute in xp_cmdshell
EXEC xp_cmdshell 'echo %TEST%'
Output is %TEST%
NULL.

As suggested in forums, I have also restarted the sql server service. Still it is not working.

Any suggestions are appreciated.

Thanks,
Amn


#2

%PATH% is a universal environment variable, only these types will be picked up by xp_cmdshell when you execute it. Any non-universal environment variable will have to be set in the batch that xp_cmdshell is running, and will not be durable/persisted between executions (you have to set them each time).