简体   繁体   English

如何使用appcmd.exe在IIS上为每个站点设置环境变量?

[英]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管理器上为每个站点设置环境变量非常容易:

IIS

I looking for a way to do it using appcmd.exe so I can include this in my install script. 我正在寻找使用appcmd.exe的方法,所以我可以在我的安装脚本中包含它。

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. 如果您正在使用VSTS进行发布管理。 You can use an inline powershell script of max 500bytes. 您可以使用最大500字节的内联PowerShell脚本。 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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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