Quantcast
Channel: Jeong's Blog
Viewing all articles
Browse latest Browse all 23

How to use IISAdministration powershell cmdlets to configure IIS configuration settings

$
0
0

FYI, see the below blog first for the detailed information.

https://blogs.iis.net/bariscaglar/iisadministration-powershell-cmdlets-new-feature-in-windows-10-server-2016

 

If you use Configuration Editor, you can make IISPowershell cmdlets more easily. Configuration Editor is a good start point to make IISAdministration powershell script easily.

For example, if you want to add a new appSetting value to Default Web Site, you can generate a code for C# programming with Configuration Editor and you can convert the generated code to IISAdministration powshell cmdles because they are based on the same API.

Here is how to do that:

  1. Open Inetmgr.exe
  2. Select Default Web Site
  3. Go to Configuration Editor page
  4. Expand “Section:” dropdown and select “appSettings”
  5. Click the second column of “(Collection)” listview item
  6. Click “…” button and you will see “Collection Editor” dialog opened
  7. Click “Add” task on the right pane of the “Collection Editor” dialog
  8. Type “test” for the key property and “test2” for the value property and then close the “Collection Editor” dialog
  9. Click “Generate Script” task, and you will get the below code. Click “Cancel” task of Configuration Editor to ignore the change.

using System;

using System.Text;

using Microsoft.Web.Administration;

internal static class Sample {

   private static void Main() {

       using(ServerManager serverManager = new ServerManager()) {

           Configuration config = serverManager.GetWebConfiguration("Default Web Site");

           ConfigurationSection appSettingsSection = config.GetSection("appSettings");

           ConfigurationElementCollection appSettingsCollection = appSettingsSection.GetCollection();          

           ConfigurationElement addElement = appSettingsCollection.CreateElement("add");

             addElement["key"] = @"test";

             addElement["value"] = @"test2";

             appSettingsCollection.Add(addElement);

             serverManager.CommitChanges();

       }

   }

}

Now let’s convert the generated code into IISAdministration powshell cmdlet. Here is the example of the outcome.

Import-Module IISAdministration

Reset-IISServerManager -Confirm:$false

Start-IISCommitDelay

$webConfig = Get-IISConfigSection -SectionPath "appSettings" -CommitPath "Default Web Site"

$collection = Get-IISConfigCollection -ConfigElement $webConfig

New-IISConfigCollectionElement -ConfigCollection $collection -ConfigAttribute @{key='test';value='test2'}

Stop-IISCommitDelay

Remove-Module IISAdministration

 

More examples for your information.

Example 1. Configuring identityType and username together with a new attribute value for computer level.

Import-Module IISAdministration

Reset-IISServerManager -Confirm:$false

Start-IISCommitDelay

$appPoolConfigSection   = Get-IISConfigSection -SectionPath "system.applicationHost/applicationPools"

$appPoolDeefaultsElement = Get-IISConfigElement -ConfigElement $appPoolConfigSection -ChildElementName "applicationPoolDefaults"

$processModelElement     = Get-IISConfigElement -ConfigElement $appPoolDeefaultsElement -ChildElementName "processModel"

Set-IISConfigAttributeValue -ConfigElement $processModelElement -AttributeName "identityType" -AttributeValue "LocalSystem"

$anonymousAuthenticationConfigSection = Get-IISConfigSection -SectionPath "system.webServer/security/authentication/anonymousAuthentication"

Set-IISConfigAttributeValue -ConfigElement $anonymousAuthenticationConfigSection -AttributeName "userName" -AttributeValue ""

Stop-IISCommitDelay

Remove-Module IISAdministration

 

Example 2. Configuring IIS central certificate using IISAdministration

$sharePath = "$env:systemdrive\temp_share"

md $sharePath

 

$certStorePath = "Cert:\LocalMachine\My"

$thumbprint = New-SelfSignedCertificate -DnsName "explicit.one.ccs" -CertStoreLocation $certStorePath

$mypwd = ConvertTo-SecureString -String "xxx" -Force -AsPlainText

Export-PfxCertificate -FilePath "$sharePath\explicit.one.ccs.pfx" -Cert ($certStorePath + "\" + $thumbprint.Thumbprint) -Password $mypwd

 

$PrivateKeyPassword = "xxx"

$user = "administrator"

$passwordSecure = convertto-securestring iis6!dfu -asplaintext -force

$PrivateKeyPasswordSecure = convertto-securestring $PrivateKeyPassword -asplaintext -force

 

# Enable-IISCentralCertProvider

Enable-IISCentralCertProvider -CertStoreLocation $sharePath -UserName $user -Password $passwordSecure -PrivateKeyPassword $PrivateKeyPasswordSecure

 

Example 3. Configuring IIS Shared configuration

$sharedPath = "$env:systemdrive\temp_share2"

md $sharePath

$username = "$env:computername\administrator"

$password = convertto-securestring "password1&" -asplaintext -force

$keyEncryptionPassword = convertto-securestring "password2&" -asplaintext -force

Export-IISConfiguration -UserName $username -Password $password -PhysicalPath $sharedPath -KeyEncryptionPassword $keyEncryptionPassword -force

Enable-IISSharedConfig -UserName $username -Password $password -PhysicalPath $sharedPath -DontCopyRemoteKeys

 

Example 4. Set SSL Binding

$thumbprint = (dir Cert:\LocalMachine\WebHosting)[0].Thumbprint

$certificate = get-item ("Cert:\LocalMachine\WebHosting\" + $thumbprint)

$hash = $certificate.GetCertHash()

$sm = Get-IISServerManager

$sm.Sites["Default Web Site"].Bindings.Add("*:443:", $hash, "WebHosting", "None")

$sm.CommitChanges()


Viewing all articles
Browse latest Browse all 23

Latest Images

Trending Articles



Latest Images

<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>