问题
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. )
回答1:
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
回答2:
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
来源:https://stackoverflow.com/questions/42844331/how-to-set-environment-variables-per-site-on-iis-using-appcmd-exe