It is very easy to set environment variables per site on IIS Manager:
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. )
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 .
To start Appcmd.exe At the Command Prompt, type cd %windir%\system32\inetsrv, and then press ENTER.
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.
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.
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
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With