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

点点圈 提交于 2020-01-03 11:57:23

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!