Strange psexec or maybe cmd issues

Jay_2

2[H]4U
Joined
Mar 20, 2006
Messages
3,583
I'm not sure if this is a psexec issues or maybe just the way cmd.exe handles this

If I run
cmd.exe /c "set uname=tester"

then type this to get the output of that above I get

Echo %uname%
%uname%

I expect to get

Echo %uname%
tester

Can anyone see what I am doing wrong?
 
I'm not sure if this is a psexec issues or maybe just the way cmd.exe handles this

If I run
cmd.exe /c "set uname=tester"

then type this to get the output of that above I get

Echo %uname%
%uname%

I expect to get

Echo %uname%
tester

Can anyone see what I am doing wrong?

Well, I guess you'll have to be more specific about what you're actually doing here, but it looks like the problem is this:

The 'set' command in batch only applies to the session in which it is executed, i.e., in that particularly command prompt window. Executing cmd.exe /c "set uname=tester" is going to open up a new cmd.exe window (so a new session), run the command "set uname=tester", and once the command is done executing, the window is going to close and any session variables will be lost.

Are you trying to launch a command prompt window that already has the variable uname set so that you can then continue to use that prompt, or are you trying to remotely set a system variable that will persist across all cmd.exe sessions?
 
Isn't this a scoping issue? Variables assigned with set are only available in the local cmd context. cmd /c implies that you're executing the command prompt and closing that process immediately after. Since that variable was local to that newly spawned cmd context, it is no longer valid in your existing context.

What you need to do is use the setx command, which will store the variable to the master environment in the registry.

Try something like this:

Code:
cmd.exe /c "setx uname tester"
echo uname

Now you should be able to read that variable from any cmd context. Note the slightly different syntax between set and setx.

Two things of note with setx, since you mentioned that you're using psexec:

1. setx can connect to a remote computer
2. variables created with setx are not valid for the existing cmd context (only future contexts)
 
Last edited:
Back
Top