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

2

8

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

Beckford answered 16/3, 2017 at 20:37 Comment(0)
J
14

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
Jaguar answered 22/6, 2017 at 1:21 Comment(6)
This works, but if you're calling from VSTS this will add multiple items to the config over time, so beware.Akan
You can use appcmd.exe set config "my-website" -section:system.webServer/aspNetCore /~environmentVariables /commit:site to clear the environment variables.Kareenkarel
@Kareenkarel /~environmentVariables inserts <clear /> tag but does not clear the list. So now I have clear and environmentVariable tags repeated under the <environmentVariables> <clear /> <environmentVariable name="test" /> <clear /><environmentVariable name="test" />Chloroplast
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.Chloroplast
Just to add on @Chloroplast answer, if you do commit on the apphost in the set config command, you should sldo commit on the apphost in the clear config commandBarytone
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?Disciplinant
L
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
Lymphocytosis answered 14/4, 2018 at 20:9 Comment(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.Akan

© 2022 - 2024 — McMap. All rights reserved.