Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set environment variables per site on IIS using appcmd.exe?

It is very easy to set environment variables per site on IIS Manager:

IIS

I looking for a way to do it using appcmd.exe so I can include this in my install script.

The closest I got was this:

C:\>C:\Windows\System32\inetsrv\appcmd.exe set config "dashboard" -section:system.webServer/aspNetCore /environmentVariables.[name='foo',value='bar'] /commit:apphost

-> dashboard is my site's name.

But this command returns this error:

ERROR ( message:Cannot find requested collection element. )

like image 255
andrecarlucci Avatar asked Mar 16 '17 20:03

andrecarlucci


People also ask

How do I set an environment variable in IIS?

Go to your application in IIS and choose Configuration Editor . Choose Applicationhost. config ... in From combobox. Right click on enviromentVariables element, select 'environmentVariables' element , then Edit Items .

How do I run AppCmd on IIS?

To start Appcmd.exe At the Command Prompt, type cd %windir%\system32\inetsrv, and then press ENTER.

What is AppCmd exe?

AppCmd.exe is the single command line tool for managing IIS 7 and above. It exposes all key server management functionality through a set of intuitive management objects that can be manipulated from the command line or from scripts.

How do I permanently set Environment Variables?

You can set an environment variable permanently by placing an export command in your Bash shell's startup script " ~/. bashrc " (or "~/. bash_profile ", or " ~/. profile ") of your home directory; or " /etc/profile " for system-wide operations.


2 Answers

You may have figured it out already, but this format should work:

appcmd.exe set config "dashboard" -section:system.webServer/aspNetCore /+"environmentVariables.[name='foo',value='bar']" /commit:apphost
like image 168
Adam Avatar answered Oct 19 '22 17:10

Adam


If you are using VSTS with release management. You can use an inline powershell script of max 500bytes. The following script will remove the variable before inserting to prevent adding the same entry everytime.

param($website,$var,$value,$Once=$false)
$cmd='c:\windows\system32\inetsrv\appcmd.exe'
$c=(&$cmd list config $website -section:system.webServer/aspNetCore)
if($c -like "*$var*" -and $Once -eq $true){return;}
if($c -like "*$var*"){&$cmd set config $website -section:system.webServer/aspNetCore /-"environmentVariables.[name='$var',value='$value']" /commit:apphost}
&$cmd set config $website -section:system.webServer/aspNetCore /+"environmentVariables.[name='$var',value='$value']" /commit:apphost
like image 37
Thom Kiesewetter Avatar answered Oct 19 '22 17:10

Thom Kiesewetter