0

I have a post-build script in Visual Studio that I am trying to run so that we can make remote deployments easier. Earlier in this script (you don't see it here), we hard-code the user in the net use execution below. So all we're doing here is having the user input the server name and database to deploy to and then connect to the network share of that server.

What I've found though is that the code below works fine only if you run it with Command Prompt. It opens up a new window and has the user put in the server and database and then the net use command will correctly show the server the user input, all in the same window.

If you try to use this in a batch file or in Visual Studio though, the net use command shows blank for the server name.

I appreciate any insight anyone might have on this!

START CMD /C "CALL SET /P SERVERNAME=Please Enter The Server Name: & CALL SET /P DATABASE=Please Enter The Database Name: & CALL CMD /C net use \\%SERVERNAME%\d$ /persistent:no * /user:%user%"
NPVinny
  • 1
  • 1
  • Why all the `start` and `call` - these are used for exception circumstances. Also It's all on 1 line for no good reason. Fix your code and see what happens. And dump all the `cmd /c`. –  Feb 05 '16 at 06:14
  • The CALLs were what I used to address the issue of that third statement not getting the value of %SERVERNAME% within Command Prompt in the first place. If I don't have those calls, then the variable still resolves to %SERVERNAME%. I have to have the start and/or cmd at the beginning for the Post Build event because if I don't then Visual Studio doesn't open up anything for user prompt and just hangs, which is a similar reason why it's all on one line. – NPVinny Feb 05 '16 at 19:16

1 Answers1

0

Follow advise of @bgalea. Where is %user% defined? Did you mean %username%? Of course this scheme will not work if you plan to have builds done by a build agent later.

The reason it is not working is that variables are expanded at load time. Consequently, %SERVERNAME% is expanded when the line is loaded...BEFORE the user has entered a value. Try changing to this:

SETLOCAL ENABLEDELAYEDEXPANSION SET /P SERVERNAME=Please Enter The Server Name: & SET /P DATABASE=Please Enter The Database Name: & \\!SERVERNAME!\d$ /persistent:no * /user:%username%"

That way you will be using the run time value of SERVERNAME.

RGuggisberg
  • 4,630
  • 2
  • 18
  • 27
  • %user% is a hard-coded variable set earlier in our Post-Build script (SET USER=avhost\vanderson_avctr). Your code works if I separate SETLOCAL ENABLEDELAYEDEXPANSION and the rest onto separate lines for the batch version, but for Visual Studio it still does what I had issues with before when trying to set a variable via user input, it just hangs and doesn't open anything. – NPVinny Feb 05 '16 at 19:17