Click here to Skip to main content
12,759,231 members (31,525 online)
Rate this:
Please Sign up or sign in to vote.
See more: Windows batch
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 6-Feb-13 18:58pm
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, 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.
Rate this: bad
Please Sign up or sign in to vote.

Solution 1

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...
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.
Rate this: bad
Please Sign up or sign in to vote.

Solution 2

Remove CMD.EXE part, it's totally redundant.


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\

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.

znyls 7-Feb-13 1:19am
Okay.. So, how should my batch file look like?
Just remove "CMD.EXE /k". Isn't that obvious? What it does? You need to start your program, not CMD.EXE...
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" ???
I assumed it was all correct, but I did not check it...
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.
Well, check with the documentation on makecert. Is that the problem?
znyls 7-Feb-13 1:42am
I can't eliminate CMD.exe because if i do. I wont be able to get to makecert.exe. I just tried.
I can only execute makecert using the VS Command prompt.
Not only you can, but you should. I told you exactly how.
But true, you need VS Command prompt, but this command prompt is not CMD.EXE...
znyls 7-Feb-13 1:51am
Oh..I'm sorry.. I got mixed up..I meant I cant eliminate #2.
My problem now is executing the makecert arguments on my batch file.
OK, tell me this: suppose, under VS Command Prompts, you type manually

makecert -sv SignRoot.pvk -cy authority -r sha1 -a -n \"CN=Certificate\" -ss my -sr localmachine certificate.cer

Will it work for you? This is not obvious from your question...
Is your question is how to automate it (make in one click)?

znyls 7-Feb-13 1:52am
Yes and yes..
Got it. Please see my updated answer, after [EDIT]. Problem solved.
Rate this: bad
Please Sign up or sign in to vote.

Solution 3

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.]
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)

    Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy | Mobile
Web02 | 2.8.170217.1 | Last Updated 7 Feb 2013
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100