简体   繁体   中英

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

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

您可能已经想出来了,但这种格式应该有效:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM