7

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

andrecarlucci
  • 5,927
  • 7
  • 52
  • 58

2 Answers2

13

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
Adam
  • 582
  • 2
  • 12
  • 23
  • 4
    This works, but if you're calling from VSTS this will add multiple items to the config over time, so beware. – bryanbcook May 11 '18 at 21:55
  • 1
    You can use appcmd.exe set config "my-website" -section:system.webServer/aspNetCore /~environmentVariables /commit:site to clear the environment variables. – sduplooy Jan 03 '19 at 13:28
  • @sduplooy `/~environmentVariables` inserts `` tag but does not clear the list. So now I have clear and environmentVariable tags repeated under the `` ` ` – oleksa Sep 10 '19 at 10:01
  • 3
    I've found that `clear config "Default Web Site/$(webSiteName)" -section:system.webServer/aspNetCore /commit:site` does exactly what I need - it clears the whole aspNetCore node in the global web.config file for the $(webSiteName) variable value. – oleksa Sep 10 '19 at 12:36
  • Just to add on @oleksa answer, if you do commit on the apphost in the set config command, you should sldo commit on the apphost in the clear config command – Tiago Cardoso Oct 10 '19 at 12:27
  • command is looking good "appcmd.exe set config "dashboard" -section:system.webServer/aspNetCore /+"environmentVariables.[name='foo',value='bar']" /commit:apphost" how command would be if add/edit multiple environment variables? – Saad Awan Nov 19 '20 at 14:30
4

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
Thom Kiesewetter
  • 6,703
  • 3
  • 28
  • 41
  • 1
    I was able to drop this into a DeploymentGroup Inline PowerShell task in VSTS and set the ASPNETCORE_Environment with ease using this approach. – bryanbcook May 12 '18 at 12:58