Examples of IIS Powershell cmdlets

Summary:

This document explains how to specify the values of parameter values and how the configuration file is updated by IIS Powershell generic cmdlets such as Get-WebConfiguration and Get-WebConfigurationProperty so that users can use them effectively.

I'd like to inform that the configuration editor of IIS8 has an useful feature to generate IIS Powershell cmdlets. You can use that feature if you want to get the example usage quickly. 

  

  1. Examples how to use parameter values

-PSPATH

COMMAND Examples:  -PSPATH is used to specify the target web object such as server/site/web application/virtual directory and physical directory. The following examples show that there are two equivalent type of PSPATH

 

## Using the path form which is used by MWA and you can specify MACHINE or MACHINE/WEBROOT level as well in this form; The "MACHINE/WEBROOT/APPHOST" is equivalent to "IIS:\", which is another form of PSPATH

$filter = "appSettings"

$pspath = 'MACHINE/WEBROOT/APPHOST/Default Web Site/webapplication1'

Get-WebConfigurationProperty -pspath $pspath -filter $filter -name $propertyName

 

## IIS Powershell provider style PSPATh, which starts with "IIS:\"

$pspath = 'IIS:\Sites\Default Web Site\webapplication1'

Get-WebConfigurationProperty -pspath $pspath -filter $filter -name $propertyName

 

## Even -pspath parameter don't need to be specified if the current directory location is changed to that place

Cd  'IIS:\Sites\Default Web Site\webapplication1'

Get-WebConfigurationProperty -filter $filter -name $propertyName

 

TIP

  1. For server level configurations such as config section which starts with system.applicationhost, we can skip to specify the pspath parameter also

-Filter

COMMAND Examples:  -Filter can be xpath form to specify a specific or multiple  items of IIS config object such as element/collection/attribute; It will be very useful for you to know various xpath style value to enumerate IIS configurations. 

 

## Specify a site element section with xpath style value

$filter = "//sites/site"

$propertyName = "id"

Get-WebConfigurationProperty -filter $filter -name $propertyName

 

## Specify a site element section with full path value

$filter = "system.applicationHost/sites/site"

$propertyName = "id"

Get-WebConfigurationProperty -filter $filter -name $propertyName

 

## Enumerating site (NOTE: the following five xpath examples are equivalent  in IIS config file and shows that you can use wild card value. FYI, "." means a collection item, which can be optional for specifying collection items)

Get-WebConfiguration -filter /system.applicationHost/sites/site

Get-WebConfiguration -filter /system.applicationHost/sites/site/.

Get-WebConfiguration -filter /*/*/site

Get-WebConfiguration -filter //sites/site

Get-WebConfiguration -filter //*/sites/site

 

## Enumerating name attribute of site (NOTE: the following two xpath examples are equivalent)

Get-WebConfiguration -filter /system.applicationHost/sites/site/@name

Get-WebConfiguration -filter //sites/site/@name

Get-WebConfiguration -filter //sites/site/./@name

 

## Specify a certain site node with filtering with multiple attribute values

$filter = "/system.applicationHost/sites/site[@name='Default Web Site' and @id='1']"

Get-WebConfigurationProperty -filter $filter -name $propertyName

 

## Specify a certain site node with a single attribute value

$filter = "/system.applicationHost/sites/site[@name='Default Web Site']"

Get-WebConfigurationProperty -filter $filter -name $propertyName

 

## Specify a certain site node with contains() function to say that its attribute "name" should contain a specific string

$filter = "/system.applicationHost/sites/site[contains(@name,'Default')]" 

Get-WebConfigurationProperty -filter $filter -name $propertyName
 

## Specify a certain site node with last() function to say that its attribute "name"  should end with a specific string

$filter = "/system.applicationHost/sites/site[last(@name,'Site')]"

Get-WebConfigurationProperty -filter $filter -name $propertyName
 

## Specify a certain site node with starts-with() function to say that its attribute "name" should start with a specific string

$filter = "/system.applicationHost/sites/site[starts-with(@name,'Default')]"

Get-WebConfigurationProperty -filter $filter -name $propertyName
 

## Specify a certain site node with position() function to say it should be placed at first

$filter = "/system.applicationHost/sites/site[position()=1]"

Get-WebConfigurationProperty -filter $filter -name $propertyName
 

## Specify a certain site node with count() to say that its parent should have only one site node 

$filter = "//*[count(site)=1]/site"

Get-WebConfigurationProperty -filter $filter -name $propertyName
 

## Specify a certain site node with name() to say that the element name should be "site"

$filter = "//*[name()='site']"

Get-WebConfigurationProperty -filter $filter -name $propertyName

 

## Specify a certain site node with local-name() to say that the element name should be "site"

$filter = "//*[local-name()='site']"

Get-WebConfigurationProperty -filter $filter -name $propertyName

 

## Specify a certain site node with starts-with() function to say that the element name should start "site"  should end with a specific string

$filter = "//*[starts-with(name(),'site')]"

Get-WebConfigurationProperty -filter $filter -name $propertyName
 

## Specify a certain site node with concat() function to say that the combined string of name and id attributes should be a specific string, "Default Web Site1", which is "Default Web Site" + "1"

$filter = "/system.applicationHost/sites/site[concat(@name, @id)='Default Web Site1']"

Get-WebConfigurationProperty -filter $filter -name $propertyName
 

## Specify a certain node with substring-after() function to say that the node name should contains  a specific string such as "site"

$filter = "//*[substring-after(name(), 'site')]"

Get-WebConfigurationProperty -filter $filter -name $propertyName
 

## Specify a certain node with substring-before() function to say that the node name should contains  a specific string such as "ite"

$filter = "//*[substring-before(name(), 'ite')]"

Get-WebConfigurationProperty -filter $filter -name $propertyName

 

## Specify a certain node with substring function to say that the node name's substring start 0th with length 2 should be a specific string such as "si"

$filter = "//*[substring(name(),0,2)='si']"

Get-WebConfigurationProperty -filter $filter -name $propertyName

 

## Specify a certain node with string-length() function to say that the legnth of the node name should be 4

$filter = "//*[string-length(name())=4]"

Get-WebConfigurationProperty -filter $filter -name $propertyName

 

## Specify a certain node with string-length() function to say that the legnth of the node name should be 4

$filter = "//*[string-length(name())=4]"

Get-WebConfigurationProperty -filter $filter -name $propertyName

 

## Specify a certain site node with translate() function to say that its name attribute value should be "default web site" if "D" in the original string is replaced with "d", "W" with "w", and "S" with "s" as well

$filter ="/system.applicationHost/sites/site[translate(@name, 'DWS', 'dws')='default web site']"

Get-WebConfigurationProperty -filter $filter -name $propertyName

 

## Specify a certain site node with compare-string-ordinal() function to say that its name attribute should be "default web site" in comparing case-in-sensitively

$filter ="/system.applicationHost/sites/site[compare-string-ordinal(@name,'default web site',true())=0]"

Get-WebConfigurationProperty -filter $filter -name $propertyName

 

## Specify a certain site node with compare-string-ordinal() function to say that its name attribute should be "Default Web Site" in comparing case-in-sensitively

$filter ="/system.applicationHost/sites/site[compare-string-ordinal(@name,'Default Web Site',false())=0]"

Get-WebConfigurationProperty -filter $filter -name $propertyName

 

## Specify a certain site node with compare-string-ordinal() function to say that its name attribute should be "Default Web Site" in comparing case-in-sensitively

$filter ="/system.applicationHost/sites/site[compare-string(@name,'Default Web Site',true())=0]"

Get-WebConfigurationProperty -filter $filter -name $propertyName

 

## Using "and" and "or" for set multiple condition using contains() function

$filter='/system.webServer/handlers[(contains(@accessPolicy, "Script") and contains(@accessPolicy, "Write")) or (contains(@accessPolicy, "Execute") and contains(@accessPolicy, "Write"))]'

Get-WebConfiguration -filter $filter -recurse

 

## following xpath functions are also supported:

Normalize-space(), Boolean(), Not(), Number(), Sum(), Floor(), Ceiling(), Round() and Compare-string()

-Name

COMMAND Examples:  -Name parameter is used by Get-WebConfigurationProperty and it is used to specify not only property name but also collection item

 

## Return attribute object for a specific attribute of "name"

Get-WebConfigurationProperty -filter "/system.applicationHost/sites/site" -name name

 

## Return list of collection objects  using "." or Collection which are special attribute name to specify collection items

Get-WebConfigurationProperty -filter "/system.applicationHost/sites" -name .

Get-WebConfigurationProperty -filter "/system.applicationHost/sites" -name Collection


NOTE: Even though the actual objects returned by the above two commands are exactly the same but the output in display could look different each other.

 

## When collection is used, users can filter out with query in the form of [attributeName=value] 

Get-WebConfigurationProperty -filter "/system.applicationHost/sites" -name Collection[name="Default Web Site"]

 

TIP

  1. The above command can be replaced with using xpath filtering from -filter value as the following:

Get-WebConfigurationProperty -filter "/system.applicationHost/sites/site[@name='Default Web Site']" -name .

  1. Even users can use wild character in the filtering out form, which is not allowed in xpath

Get-WebConfigurationProperty -filter "/system.applicationHost/sites" -name Collection[name="Default*"]

  1. There are special purpose of property name such as Sections and SectionGroups to manage sections

 -Recurse

COMMAND Examples: When -Recurse parameter is used with Get-WebConfiguration or Get-WebConfigurationProperty, users can query recursively not only from the current directory

get-webconfiguration '/system.webServer/handlers[(contains(@accessPolicy, "Script") and contains(@accessPolicy, "Write")) or (contains(@accessPolicy, "Execute") and contains(@accessPolicy, "Write"))]' -recurse

TIP:

  1. You might be confused if you use the -recurse parameter from child level and you got no result. For the example, if you enable  basicAuthentication on Default Web Site and run the following command, you will get empty result. This is because the authentication setting is actually configured in the server level configuration file such as applicationhost.config using location tag of "<location path="Default Web Site">".
     

Get-WebConfigurationProperty '//basicAuthentication' -name enabled   -pspath "MACHINE/WEBROOT/APPHOST/Default

Web Site"  -Recurse

 

In the same situation, if you use the following command with specifying the server level for the pspath parameter, you will get the result.

Get-WebConfigurationProperty '//basicAuthentication' -name enabled   -pspath "MACHINE/WEBROOT/APPHOST"  -Recurse

 

If the authentication setting was configured in the web.config of Default Web Site's root directory after delegating the authentication configuration to site level, both of the above two commands  will work because the actual configuration is now set in the Default Web Site level.

-Value

COMMAND Examples:  When -Value  parameter is used by IIS powershell cmdlet, it usually set with a single string value (NOTE: if the value contains space, it should be quoted with " or ')
 

## The value can be hash table object if it is used to set a collection item

add-webconfiguration '/system.applicationHost/sites/site[@name="Default Web Site"]/bindings'-value @{protocol="http";bindingInformation="*:80:"} -pspath iis:\ 

 

  1. Adding  "file" attribute

FROM:

<?xml version="1.0" encoding="UTF-8"?>

<configuration>

    <appSettings>    

      <add key="foo" value="bar" />

    </appSettings>

</configuration>

 

TO:

<?xml version="1.0" encoding="UTF-8"?>

<configuration>

    <appSettings file="test">    

      <add key="foo" value="bar" />

    </appSettings>

</configuration>

 

COMMAND Examples:

 

$pspath = 'MACHINE/WEBROOT/APPHOST/Default Web Site/webapplication1'

$filter = "appSettings"

$propertyName = "file"

$propertyValue =  "test"

 

Set-WebConfigurationProperty -pspath $pspath -filter $filter -name $propertyName -value $propertyValue

 

  1. Removing "file" attribute

FROM:

<?xml version="1.0" encoding="UTF-8"?>

<configuration>

    <appSettings file="test">    

      <add key="foo" value="bar" />

    </appSettings>

</configuration>

 

TO:

<?xml version="1.0" encoding="UTF-8"?>

<configuration>

    <appSettings>    

      <add key="foo" value="bar" />

    </appSettings>

</configuration>

 

COMMAND Examples:

 

$pspath = 'MACHINE/WEBROOT/APPHOST/Default Web Site/webapplication1'

$filter = "appSettings/@file"

 

clear-WebConfiguration -pspath $pspath -filter appSettings/@file

 

  1. Locking "file" attribute only

FROM:

<?xml version="1.0" encoding="UTF-8"?>

<configuration>

    <appSettings>    

      <add key="foo" value="bar" />

    </appSettings>

</configuration>

 

TO:

<?xml version="1.0" encoding="UTF-8"?>

<configuration>

    <appSettings lockAttributes="file">    

      <add key="foo" value="bar" />

    </appSettings>

</configuration>

 

COMMAND Examples:

 

$pspath = 'MACHINE/WEBROOT/APPHOST/Default Web Site/webapplication1'

$filter = "appSettings/@file"

$lockType = "inclusive"

 

Add-WebConfigurationLock -pspath $pspath -filter $filter -type $lockType

 

  1. Unlock "file" attribute with removing the lockAttributes attribute, which is a specialized attribute

FROM:

<?xml version="1.0" encoding="UTF-8"?>

<configuration>

    <appSettings lockAttributes="file">    

      <add key="foo" value="bar" />

    </appSettings>

</configuration>

 

TO:

<?xml version="1.0" encoding="UTF-8"?>

<configuration>

    <appSettings>    

      <add key="foo" value="bar" />

    </appSettings>

</configuration>

 

COMMAND Examples:

 

$pspath = 'MACHINE/WEBROOT/APPHOST/Default Web Site/webapplication1'

$filter = "appSettings/@file"

 

Remove-WebConfigurationLock -pspath $pspath -filter $filter

 

  1. Locking all attributes except "file" attribute with using lockAllAttributesExcept attribute which is a specialized attribute

FROM:

<?xml version="1.0" encoding="UTF-8"?>

<configuration>

    <appSettings>    

      <add key="foo" value="bar" />

    </appSettings>

</configuration>

 

TO:

<?xml version="1.0" encoding="UTF-8"?>

<configuration>

    <appSettings lockAllAttributesExcept="file">    

      <add key="foo" value="bar" />

    </appSettings>

</configuration>

 

COMMAND Examples:

 

$pspath = 'MACHINE/WEBROOT/APPHOST/Default Web Site/webapplication1'

$filter = "appSettings/@file"

$lockType = "exclusive"

 

Add-WebConfigurationLock -pspath $pspath -filter $filter -type $lockType

 

  1. Removing  the exclusive lock which is set by lockAllAttributesExcept attribute which is a specialized attribute

FROM:

<?xml version="1.0" encoding="UTF-8"?>

<configuration>

    <appSettings lockAllAttributesExcept="file">    

      <add key="foo" value="bar" />

    </appSettings>

</configuration>

 

TO:

<?xml version="1.0" encoding="UTF-8"?>

<configuration>

    <appSettings>    

      <add key="foo" value="bar" />

    </appSettings>

</configuration>

COMMAND Examples: (NOTE: Remove-WebConfigurationLock does not support removing the exclusive lock; As a work-around, we should remove the whole section and copy the previous items)

 

$pspath = 'MACHINE/WEBROOT/APPHOST/Default Web Site/webapplication1'

$filter = "appSettings"

$propertyName = "."

 

$items = (get-webconfigurationproperty -pspath $pspath -filter $filter -name $propertyName).collection

 

Clear-WebConfiguration  -pspath $pspath -filter $filter

 

$items | foreach { Add-WebConfigurationProperty -pspath $pspath -filter $filter -name $propertyName -value @{key=$_.key;value=$_.value}  }

 

  1. Adding collection item

FROM:

<?xml version="1.0" encoding="UTF-8"?>

<configuration>

    <appSettings>    

    </appSettings>

</configuration>

 

TO:

<?xml version="1.0" encoding="UTF-8"?>

<configuration>

    <appSettings file="test">    

      <add key="foo" value="bar" />

    </appSettings>

</configuration>

 

COMMAND Examples:

 

$pspath = 'MACHINE/WEBROOT/APPHOST/Default Web Site/webapplication1'

$filter = "appSettings"

$propertyName = "."

$propertyValue =  @{key='ee';value='ee'}

 

Add-WebConfigurationProperty -pspath $pspath -filter $filter -name $propertyName -value $propertyValue

 

  1. Modifying property value of collection item

FROM:

<?xml version="1.0" encoding="UTF-8"?>

<configuration>

    <appSettings>    

      <add key="foo" value="bar" />

    </appSettings>

</configuration>

 

TO:

<?xml version="1.0" encoding="UTF-8"?>

<configuration>

    <appSettings file="test">    

      <add key="foo2" value="bar" />

    </appSettings>

</configuration>

 

COMMAND Examples:

 

$pspath = 'MACHINE/WEBROOT/APPHOST/Default Web Site/webapplication1'

$filter = "appSettings/add[@key='foo']"

$propertyName = "key"

$propertyValue =  "foo2"

 

Set-WebConfigurationProperty -pspath $pspath -filter $filter -name $propertyName -value $propertyValue

 

  1. Reverting to parent with removing whole config section

FROM:

<?xml version="1.0" encoding="UTF-8"?>

<configuration>

    <appSettings>

      <clear/>       

        <add key="foo" value="bar"/>

    </appSettings>

</configuration>

 

TO:

<?xml version="1.0" encoding="UTF-8"?>

<configuration>

</configuration>

COMMAND Examples:

 

$pspath = 'MACHINE/WEBROOT/APPHOST/Default Web Site/webapplication1'

$filter = "appSettings"

 

Clear-WebConfiguration  -pspath $pspath -filter $filter

 

  1. Removing <clear />

FROM:

<?xml version="1.0" encoding="UTF-8"?>

<configuration>

    <appSettings>

      <clear/>       

        <add key="foo" value="bar"/>

    </appSettings>

</configuration>

 

TO:

<?xml version="1.0" encoding="UTF-8"?>

<configuration>

    <appSettings>

        <add key="foo" value="bar"/>

    </appSettings>

</configuration>

COMMAND Examples:

 

$pspath = 'MACHINE/WEBROOT/APPHOST/Default Web Site/webapplication1'

$filter = "appSettings"

$propertyName = "."

 

$items = (get-webconfigurationproperty -pspath $pspath -filter $filter -name $propertyName).collection

 

Clear-WebConfiguration  -pspath $pspath -filter $filter

 

$items | foreach { Add-WebConfigurationProperty -pspath $pspath -filter $filter -name $propertyName -value @{key=$_.key;value=$_.value}  }

 

  1. Removing all collection items of the current level only

FROM:

<?xml version="1.0" encoding="UTF-8"?>

<configuration>

    <appSettings>    

      <remove key="foo_parent" />

      <add key="foo" value="bar"/>

      <add key="foo2" value="bar2"/>

    </appSettings>

</configuration>

 

TO:

<?xml version="1.0" encoding="UTF-8"?>

<configuration>

    <appSettings>

      <remove key="foo_parent" />

    </appSettings>

</configuration>

COMMAND Examples:

 

$pspath = 'MACHINE/WEBROOT/APPHOST/Default Web Site/webapplication1'

$filter = "appSettings"

$propertyName = "."

 

$items = (get-webconfigurationproperty -pspath $pspath -filter $filter -name $propertyName).collection  | where {$_.getmetadata("collectionItemFileConfigPath") -eq $pspath }

 

$items | foreach { Remove-WebConfigurationProperty -pspath $pspath -filter $filter -name $propertyName -AtElement @{key=$_.key}  }

 

  1. Remove a specific <remove> item of key value "foo_parent"

FROM:

<?xml version="1.0" encoding="UTF-8"?>

<configuration>

    <appSettings>

      <remove key="foo_parent" />

      <remove key="foo_parent2" />

      <add key="foo" value="bar" />

    </appSettings>

</configuration>

 

TO:

<?xml version="1.0" encoding="UTF-8"?>

<configuration>

    <appSettings>

      <remove key="foo_parent2" />

      <add key="foo" value="bar" />

    </appSettings>

</configuration>

 

COMMAND Examples:

 

$pspath = 'MACHINE/WEBROOT/APPHOST/Default Web Site/webapplication1'

$pspath_parent = 'MACHINE/WEBROOT/APPHOST/Default Web Site'

$filter = "appSettings"

$propertyName = "."

$KeyValue= "foo_parent"

 

$parentItems = (get-webconfigurationproperty -pspath $pspath_parent -filter $filter -name $propertyName).collection

$inheritedItems = (get-webconfigurationproperty -pspath $pspath -filter $filter -name $propertyName).collection  | where {$_.getmetadata("collectionItemFileConfigPath") -ne $pspath }

$localitems = (get-webconfigurationproperty -pspath $pspath -filter $filter -name $propertyName).collection  | where {$_.getmetadata("collectionItemFileConfigPath") -eq $pspath }

 

$removedItems = @{}

$parentItems | foreach {

     $item = $_

     $itemFound = $inheritedItems | where { ($_.key -eq $item.key) -and  ($_.value -eq $item.value) }

     if (($itemFound -eq $null) -and ( $item.key -ne $KeyValue))

     {

       $removedItems.add($item.key, $item.value)

     }

}

 

Clear-WebConfiguration  -pspath $pspath -filter $filter

 

$localitems | foreach { Add-WebConfigurationProperty -pspath $pspath -filter $filter -name $propertyName -value @{key=$_.key;value=$_.value}  }

 

$removedItems.Keys | foreach { Remove-WebConfigurationProperty -pspath $pspath -filter $filter -name $propertyName -AtElement @{key=$_}  }

 

  1. Remove all collection items including items inherited from parents

FROM:

<?xml version="1.0" encoding="UTF-8"?>

<configuration>

    <appSettings>

     <add key="foo" value="bar"/>

    </appSettings>

</configuration>

 

TO:

<?xml version="1.0" encoding="UTF-8"?>

<configuration>

    <appSettings>

    <clear/>        

    </appSettings>

</configuration>

COMMAND Examples:

 

$pspath = 'MACHINE/WEBROOT/APPHOST/Default Web Site/webapplication1'

$filter = "appSettings"

$propertyName = "."


Rem
ove-WebConfigurationProperty -pspath $pspath -filter $filter -name $propertyName

 

  1. Remove "foo_parent" collection item which is inherited from parent level)

FROM:

<?xml version="1.0" encoding="UTF-8"?>

<configuration>

    <appSettings>

     <add key="foo" value="bar"/>

    </appSettings>

</configuration>

 

TO:

<?xml version="1.0" encoding="UTF-8"?>

<configuration>

    <appSettings>

       <remove key="foo_parent" />

        <add key="foo" value="bar"/>

    </appSettings>

</configuration>

COMMAND Examples:

 

$pspath = 'MACHINE/WEBROOT/APPHOST/Default Web Site/webapplication1'

$filter = "appSettings"

$propertyName = "."

$propertyValue =  "foo_parent"

 

Remove-WebConfigurationProperty  -pspath $pspath -filter $filter -name $propertyName -AtElement @{key=$propertyValue}

 

  1. Remove "foo" collection item for a specific file such as default.aspx which is placed at the current directory

FROM:

<?xml version="1.0" encoding="UTF-8"?>

<configuration>

    <appSettings>    

      <add key="foo" value="bar"/>

    </appSettings>

</configuration>

 

TO:

<?xml version="1.0" encoding="UTF-8"?>

<configuration>

    <appSettings>    

      <add key="foo" value="bar" />

    </appSettings>

    <location path="default.aspx">

        <appSettings>

            <remove key="foo" />

        </appSettings>

    </location>

</configuration>

 

COMMAND Examples:

 

$pspath = 'MACHINE/WEBROOT/APPHOST/Default Web Site/webapplication1'

$filter = "appSettings"

$propertyName = "."

$propertyValue =  "foo"

$locationPath =  "default.aspx"

 

Remove-WebConfigurationProperty  -pspath $pspath -filter $filter -name $propertyName -AtElement @{key=$propertyValue} -location $locationPath

 

  1. Reverting to parent for the default.aspx location

FROM:

<?xml version="1.0" encoding="UTF-8"?>

<configuration>

    <appSettings>    

      <add key="foo" value="bar" />

    </appSettings>

    <location path="default.aspx">

        <appSettings>

            <remove key="foo" />

        </appSettings>

    </location>

</configuration>

 

TO:

<?xml version="1.0" encoding="UTF-8"?>

<configuration>

    <appSettings>    

      <add key="foo" value="bar" />

    </appSettings>

    <location path="default.aspx">

    </location>

</configuration>

 

COMMAND Examples:

 

$pspath = 'MACHINE/WEBROOT/APPHOST/Default Web Site/webapplication1'

$filter = "appSettings"

$propertyName = "."

$propertyValue =  "foo"

$locationPath =  "default.aspx"

 

clear-webconfiguration  -pspath $pspath -filter $filter -name $propertyName -AtElement @{key=$propertyValue} -location $locationPath

 

  1. Locking collection item

FROM:

<?xml version="1.0" encoding="UTF-8"?>

<configuration>

    <appSettings>    

      <add key="foo" value="bar" />

    </appSettings>

</configuration>

 

TO:

<?xml version="1.0" encoding="UTF-8"?>

<configuration>

    <appSettings file="test">    

      <add key="foo" value="bar" lockItem="true"/>

    </appSettings>

</configuration>

 

COMMAND Examples:

 

$pspath = 'MACHINE/WEBROOT/APPHOST/Default Web Site/webapplication1'

$filter = "appSettings/add[@key='foo']"

$lockType = "general"

 

Add-WebConfigurationLock -pspath $pspath -filter "appSettings/add[@key='foo']" -type $lockType

 

  1. Unlocking collection item

FROM:

<?xml version="1.0" encoding="UTF-8"?>

<configuration>

    <appSettings>    

      <add key="foo" value="bar" lockItem="true"/>

    </appSettings>

</configuration>

 

TO:

<?xml version="1.0" encoding="UTF-8"?>

<configuration>

    <appSettings file="test">    

      <add key="foo" value="bar"/>

    </appSettings>

</configuration>

 

COMMAND Examples:

 

$pspath = 'MACHINE/WEBROOT/APPHOST/Default Web Site/webapplication1'

$filter = "appSettings/add[@key='foo']"

 

Remove-WebConfigurationLock -pspath $pspath  -filter $filter

 

  1. Set machine level with specifying framework version of v2.0, which is different to the default value -> This is only applicable for Windows 2012

FROM: for the root web.config file of .Net v2.0

<?xml version="1.0" encoding="UTF-8"?>

<configuration>

    <appSettings>    

    </appSettings>

</configuration>

 

TO:

<?xml version="1.0" encoding="UTF-8"?>

<configuration>

    <appSettings>    

      <add key="foo" value="bar" />

    </appSettings>

</configuration>

 

COMMAND Examples:

 

$pspath = 'MACHINE/WEBROOT'

$filter = "appSettings"

$propertyName = "file"

$propertyValue =  "test"

$clrVersion = "v2.0"

 

Set-WebConfigurationProperty -pspath $pspath -filter $filter -name $propertyName -value $propertyValue -clr $clrVersion

 

  1. Locking item from section level

FROM: for the applicationhost.config file

...

</configuration>

 

TO:

...

    <appSettings lockItem="true" />

</configuration>

 

COMMAND Examples:

 

$pspath = 'MACHINE/WEBROOT/APPHOST'

$filter = "appSettings"

$type="general"

 

Add-WebConfigurationLock -pspath $pspath -filter $filter

 

  1. Remove section level lock

FROM: for the applicationhost.config file

...

    <appSettings lockItem="true" />

</configuration>

 

TO:

    <appSettings/>

</configuration>

...

 

COMMAND Examples:

 

$pspath = 'MACHINE/WEBROOT/APPHOST'

$filter = "appSettings"

 

Remove-WebConfigurationLock -pspath $pspath -filter $filter

 

  1. Add a new section group

FROM: for the applicationhost.config file

...

    </configSections>

...

 

TO:

        <sectionGroup name="MySectionGroup" />

    </configSections>

...

 

COMMAND Examples:

 

$pspath = 'MACHINE/WEBROOT/APPHOST'

$filter = "/"

$propertyName = "sectionGroups"

$propertyValue =  "MySectionGroup"

 

Add-WebConfigurationProperty -pspath $pspath -filter $filter -name $propertyName -value $propertyValue

 

  1. Delete a section group

FROM: for the applicationhost.config file

...

        <section name="MySection" />

    </configSections>

...

 

TO:

    </configSections>

...

 

COMMAND Examples:

 

## FYI, This is to show how to get the section object at root level

get-WebConfigurationProperty -pspath iis:\ -filter / -name sectionGroups["MySectionGroup"]

 

## Delete the section group

Remove-WebConfigurationProperty -pspath iis:\ -filter "/" -name sectionGroups["MySectionGroup"]

 

  1. Add a new section

FROM: for the applicationhost.config file

...

    </configSections>

...

 

TO:

        <section name="MySection" />

    </configSections>

...

 

COMMAND Examples:

 

$pspath = 'MACHINE/WEBROOT/APPHOST'

$filter = "/"

$propertyName = "sections"

$propertyValue =  "MySection"

 

Add-WebConfigurationProperty -pspath $pspath -filter $filter -name $propertyName -value $propertyValue

 

  1. Delete a section

FROM: for the applicationhost.config file

...

        <section name="MySection" />

    </configSections>

...

 

TO:

    </configSections>

...

 

COMMAND Examples:

 

## FYI, This is to show how to get the section object at root level

get-WebConfigurationProperty -pspath iis:\ -filter / -name sections["MySection"]

 

## Delete the section

Remove-WebConfigurationProperty -pspath iis:\ -filter "/" -name sections["MySection"]

 

  1. Add a new section under a specific section group

FROM: for the applicationhost.config file

...

        <sectionGroup name="MySectionGroup" />

    </configSections>

...

 

TO:

        <sectionGroup name="MySectionGroup">

             <section name="MyChildSection" />

        </sectionGroup>           

    </configSections>

...

 

COMMAND Examples:

 

$pspath = 'MACHINE/WEBROOT/APPHOST'

$filter = "/MySectionGroup"

$propertyName = "sections"

$propertyValue =  "MyChildSection"

 

Add-WebConfigurationProperty -pspath $pspath -filter $filter -name $propertyName -value $propertyValue

 

  1. Delete a section under a specific section group

FROM: for the applicationhost.config file

...

        <sectionGroup name="MySectionGroup">

             <section name="MyChildSection" />

        </sectionGroup>           

    </configSections>

...

 

TO:

        <sectionGroup name="MySectionGroup" />

    </configSections>

...

 

COMMAND Examples:

 

## FYI, This is to show how to get the section object under the specific section group

get-WebConfigurationProperty -pspath iis:\ -filter /MySectionGroup -name sections["MyChildSection"]

 

## Delete the section

Remove-WebConfigurationProperty -pspath iis:\ -filter "/MySectionGroup" -name sections["MyChildSection"]

 

  1. Configure "Deny" for OverrideModeDefault attribute of a specific section

FROM: for the applicationhost.config file

...

        <sectionGroup name="system.webServer">

            <section name="asp" overrideModeDefault="Deny" />

...

 

TO:

        <sectionGroup name="system.webServer">

            <section name="asp" overrideModeDefault="Allow" />

...

 

COMMAND Examples:

 

Set-WebConfigurationProperty -filter /system.webServer -name 'sections["asp"].OverrideModeDefault' -value Allow -pspath iis:\

 

 

No Comments