Click here to Skip to main content
15,885,278 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I need to do this in a batch file:

1. Open cmd
2. Run VS Command Prompt via cmd
3. Execute this command
"makecert -sv SignRoot.pvk -cy authority -r sha1 -a -n \"CN=Certificate\" -ss my -sr localmachine certificate.cer"


So far, I've done 1 and 2, my problem is getting into #3.

Here's what I have so far.
start  cmd.exe /k "%comspec% /c  "C:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\vcvarsall.bat" x86"
Posted
Comments
PIEBALDconsult 7-Feb-13 0:25am    
I would likely eliminate step 2, what does it gain you?
Why would you run cmd from a batch file?
Why are you trying to run cmd from cmd?
On my system (Win 7 with VS 2010) I see makecert.exe in C:\Program Files\Microsoft SDKs\Windows\v7.0A\bin
I suggest writing a BAT file with just the one makecert call with the full path to the EXE and see how it goes.
znyls 7-Feb-13 1:18am    
Okay..so, if I eliminate #2.. How will my batch file look like?

Im so sorry, but this is my first time doing batch files.
znyls 7-Feb-13 1:48am    
I cant eliminate #2 because I cant execute makecert commands on cmd.exe, only in VS Command prompt.

I think what you want to do is open a command window with the VS2010 compiler environment defined and execute the 'makecert' stuff in it.

Rather than what you have described above, do this:

Click Start -> (all programs) -> Microsoft Visual Studio 2010 -> Visual Studio Tools -> Visual Studio Command Prompt (2010)

This will open the command window you want. You can then enter the 'makecert' stuff...
 
Share this answer
 
Comments
znyls 7-Feb-13 1:21am    
But I dont like it that way.. I want that the batch file will do everything for the user including the makecert stuff..
H.Brydon 7-Feb-13 12:46pm    
This is actually a different problem - see new solution for it.
Remove CMD.EXE part, it's totally redundant.

[EDIT]

Thank you for clarification. Now, not only getting rid of CMD.EXE mean removing redundant tool, it's also critical.

Visual Studio Prompt is interactive, something you don't want. It really is executed via %comspec% which is the same vary CMD.EXE already. You need to get rid of it, too. So what Visual Studio Prompt does? It simply sets up environment and working directory the way some utilities work correctly. Lame thing, by they way; I really hope Microsoft will be able to get rid of such approach in future.

So, all you need is to carry out the environment and then execute your command line, without CMD.EXE. Here is how:

Go to main menu and copy the Visual Studio Command Prompt .lnk file in some directory. Open its properties window (Alt+Enter) and copy two strings: a full name of the batch file from the field "Target" and a working directory from "Start in". The result depends on how you installed Visual Studio. It will be something like:
C:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\vcvarsall.bat
C:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\


You really want to unify then so write just the directory and file name:
C:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\
vcvarsall.bat


Modify these strings according to what you have.

Now, here is what you can do in your batch file:
set workingDir=C:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\
set vs=vcvarsall.bat

cd %workingDir%
call %vs%

:: your commands go here, such as:
makecert -sv SignRoot.pvk -cy authority -r sha1 -a -n \"CN=Certificate\" -ss my -sr localmachine certificate.cer


I've shown these def parts to help you to unify the batch for other commands and Visual Studio installations. You can also pass parameters to batch files to abstract our file names, for example:
:: ...
makecert -sv %1 -cy authority -r sha1 -a -n \"CN=Certificate\" -ss my -sr localmachine %2


This way, you can call your batch file with file parameters. This can be important, because you don't want to copy your files to working directory of Visual Studio Common Prompt, so you would need to copy their full path names:

yourBatchFile.bat /path/to/SignRoot.pvk /path/to/certificate.cer


And you can put this line in other batch file, for example. Be careful with path names containing blank spaces; they should come in ""; or avoid them.

—SA
 
Share this answer
 
v2
Comments
znyls 7-Feb-13 1:19am    
Okay.. So, how should my batch file look like?
Sergey Alexandrovich Kryukov 7-Feb-13 1:22am    
Just remove "CMD.EXE /k". Isn't that obvious? What it does? You need to start your program, not CMD.EXE...
—SA
znyls 7-Feb-13 1:26am    
how about my "-sv SignRoot.pvk -cy authority -r sha1 -a -n \"CN=Certificate\" -ss my -sr localmachine certificate.cer" ???
Sergey Alexandrovich Kryukov 7-Feb-13 1:29am    
I assumed it was all correct, but I did not check it...
—SA
znyls 7-Feb-13 1:33am    
I tried what you suggested.. I eliminated cmd.exe.
So, my bat file now will run makecert.exe but not the creation of the certificate.
It seems that you want to add the VS2010 environment to the current command window. Here is how to do it... Go to the "Visual Studio Command Prompt (2010)" menu described in Solution 1, but instead of clicking on it (ie. left click), do a right click, select properties. In the 'Target' edit control, you should see something like:

%comspec% /k "C:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\vcvarsall.bat" x86


or something similar. In your batch file, you want to add that line by itself, without the "%comspec% /k". Following this would be your makecert line.

Here is what your batch file should contain (with quotes fixed up):

call "C:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\vcvarsall.bat" x86
makecert -sv SignRoot.pvk -cy authority -r sha1 -a -n "CN=Certificate" -ss my -sr localmachine certificate.cer


[the makecert line is wrapping here; it should be on one line in your batch file.]
 
Share this answer
 
Comments
znyls 7-Feb-13 20:11pm    
This is how we did it yesterday. But thanks anyway. :)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900