简体   繁体   English

通过 PowerShell 在 IIS 中设置应用程序设置

[英]Set application settings in IIS through PowerShell

How can I set application settings in IIS through PowerShell?如何通过 PowerShell 在 IIS 中设置应用程序设置?

I tried using Set-WebConfigurationProperty as我尝试使用Set-WebConfigurationProperty作为

Set-WebConfigurationProperty "/appSettings/add[@key='someKey']" -PSPath "IIS:\Sites\Default Web Site\someSite" -name "someKey" -Value "someValue"

But I am getting但我越来越

WARNING: Target configuration object '/appSettings/add[@key='someKey'] is not found at path 'MACHINE/WEBROOT/APPHOST/Default Web Site/someSite'.

The easiest way to do this I find is to build the PowerShell from IIS configuration editor.我发现最简单的方法是从 IIS 配置编辑器构建 PowerShell。

To do this;去做这个;

1) Open Inetmgr (IIS) 1) 打开 Inetmgr (IIS)
2) Click on the site you want to target. 2) 单击您要定位的站点。
3) Feature View, Configuration Editor down at the bottom left. 3) 功能视图,左下角的配置编辑器。
4) From here, browse to the section of the configuration you want to edit, and 4) 从这里,浏览到要编辑的配置部分,然后
make the change做出改变
5) Then click "Generate Script" on the top right. 5) 然后点击右上角的“生成脚本”。

This will generate multiple different scripts for configuring this, choose PowerShell and there you go.这将生成多个不同的脚本来配置它,选择 PowerShell 就可以了。

For example, changing Windows authentication to Forms例如,将 Windows 身份验证更改为 Forms

Set-WebConfigurationProperty -pspath 'MACHINE/WEBROOT/APPHOST/Somewebsite'  -filter "system.web/authentication" -name "mode" -value "Forms"

You can learn how to do just about anything from here.你可以从这里学习如何做任何事情。

There is also the get-webconfigurationproperty command that will get you the config before you edit it, this is just run from PowerShell.还有 get-webconfigurationproperty 命令可以在您编辑之前获取配置,这只是从 PowerShell 运行。

A key to remember is is SET-WebConfigurationProperty will override everything and often not do what you want.要记住的一个关键是SET-WebConfigurationProperty将覆盖所有内容并且通常不会执行您想要的操作。

Where Add-WebConfigurationProperty will add, not override and add additional config.其中Add-WebConfigurationProperty将添加,而不是覆盖和添加其他配置。

Hope that helps!希望有帮助!

Rich富有的

And how exactly to use Add-WebConfigurationProperty ?以及如何使用Add-WebConfigurationProperty Because it must be used in case the app setting is yet missing ( Set-WebConfigurationProperty will fail).因为它必须在应用设置尚未丢失的情况下使用( Set-WebConfigurationProperty将失败)。

So, given the following configuration, a site "SiteOne" with a virtual directory "VirtualDirOne":因此,鉴于以下配置,站点“SiteOne”具有虚拟目录“VirtualDirOne”:

<?xml version="1.0" encoding="utf-8"?>
<configuration xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform">
  <appSettings>
    <add key="first" value="a" />
  </appSettings>
</configuration>

When I want to change the value to "b":当我想将值更改为“b”时:

Set-WebConfigurationProperty -pspath "iis:\Sites\SiteOne\VirtualDirOne" -filter "/appSettings/add[@key='first']" -name value -value "b"

When I want to add another setting:当我想添加另一个设置时:

Add-WebConfigurationProperty -pspath "iis:\Sites\SiteOne\VirtualDirOne" -filter "/appSettings" -name "." -value @{key='second'; value='x'}

When I want to get the value:当我想获得价值时:

Get-WebConfigurationProperty -pspath "iis:\Sites\SiteOne\VirtualDirOne" -filter "/appSettings/add[@key='second']" -name "value.Value"

And finally, to remove the setting:最后,删除设置:

Clear-WebConfiguration -pspath "iis:\Sites\SiteOne\VirtualDirOne" -filter "/appSettings/add[@key='second']"

There many examples here .有很多例子在这里

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

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