Is it possible to manage the active/passive servers within an IIS web farm load-balancer

Greg M 1 Reputation point
2020-09-14T19:01:12.77+00:00

I have been tasked with trying to automate the switching of the active/passive nodes within a Web Farm load-balancer in IIS 10. To this point, I have not been able to find a means that could be used to do this from a scripted perspective.

From exporting the current configuration of the server, there is a "webFarms" node that contains the information I need, and it is stored in the C:\Windows\System32\inetsrv\Config\ApplicationHost.config file.

    <webFarms>
        <webFarm name="rpm-lb" enabled="true">
            <server address="server1" enabled="true">
                <applicationRequestRouting hostName="server1" httpPort="8080" httpsPort="8443" />
            </server>
            <server address="server2" enabled="false">
                <applicationRequestRouting hostName="server2" httpPort="8080" httpsPort="8443" />
            </server>
            <applicationRequestRouting>
                <protocol httpVersion="PassThrough" timeout="00:02:00" reverseRewriteHostInResponseHeaders="true">
                    <cache enabled="false" />
                </protocol>
            </applicationRequestRouting>
        </webFarm>
        <applicationRequestRouting>
            <hostAffinityProviderList>
                <add name="Microsoft.Web.Arr.HostNameRoundRobin" />
            </hostAffinityProviderList>
        </applicationRequestRouting>
    </webFarms>

If I pull information using PowerShell's Get-IISConfigSection commandlet, which results in this output:
PS C:\Windows\system32> Get-IISConfigSection -SectionPath "webFarms"

IsLocked                        : False
OverrideMode               : Inherit
OverrideModeEffective : Deny
SectionPath                   : webFarms
Attributes                      : {onDemandHealthCheck}
ChildElements               : {applicationRequestRouting}
ElementTagName          : webFarms
IsLocallyStored              : True
Methods                        :
RawAttributes                : {[onDemandHealthCheck, False]}
Schema                          : Microsoft.Web.Administration.ConfigurationElementSchema

What I have been unable to find is how to get the server nodes from the configuration via PowerShell. If I can get that information, I think it would be fair to assume that I can update the active/passive nodes (enabled="true"/enabled="false").

If anyone can point me in the correct direction, or better yet, help me find the correct commands to execute, I'd be extremely appreciative.

Thanks!
Greg

Windows Server PowerShell
Windows Server PowerShell
Windows Server: A family of Microsoft server operating systems that support enterprise-level management, data storage, applications, and communications.PowerShell: A family of Microsoft task automation and configuration management frameworks consisting of a command-line shell and associated scripting language.
5,364 questions
{count} votes

3 answers

Sort by: Most helpful
  1. Rich Matheisen 44,776 Reputation points
    2020-09-14T21:30:04.333+00:00

    See if this works (assuming there's only one web farm!):

    (Get-IISConfigSection -SectionPath "webFarms").webfarms.webfarm|foreach{$_.server|select -expand address}  
    
    1 person found this answer helpful.

  2. Ian Xue (Shanghai Wicresoft Co., Ltd.) 29,651 Reputation points Microsoft Vendor
    2020-09-16T03:53:28.41+00:00

    Hi,

    Does this work? Sorry I've got no environment to test it.

    Get-IISConfigSection -SectionPath "webFarms" | Get-IISConfigCollection -CollectionName "webFarm" | Get-IISConfigElement -ChildElementName "server"  
    

    Best Regards,
    Ian

    ============================================

    If the Answer is helpful, please click "Accept Answer" and upvote it.
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    1 person found this answer helpful.

  3. Rich Matheisen 44,776 Reputation points
    2020-09-17T14:58:56.73+00:00

    Since I have no servers, and no IIS, I'm assuming that the configuration file contains properly constructed XML. But, using your data sample (adding only a single tag at the beginning, I think that using XPATH is what you need if the IIS folks (who would be the subject matter experts for your question) can't help with the usage of that products cmdlets.

    This just gets the 'server' nodes from within he webFarm node named 'rpm-lb' and swaps the active/passive state.

    $t=@"
    <?xml version="1.0" encoding="UTF-8"?>
    <webFarms>
             <webFarm name="rpm-lb" enabled="true">
                 <server address="server1" enabled="true">
                     <applicationRequestRouting hostName="server1" httpPort="8080" httpsPort="8443" />
                 </server>
                 <server address="server2" enabled="false">
                     <applicationRequestRouting hostName="server2" httpPort="8080" httpsPort="8443" />
                 </server>
                 <applicationRequestRouting>
                     <protocol httpVersion="PassThrough" timeout="00:02:00" reverseRewriteHostInResponseHeaders="true">
                         <cache enabled="false" />
                     </protocol>
                 </applicationRequestRouting>
             </webFarm>
             <applicationRequestRouting>
                 <hostAffinityProviderList>
                     <add name="Microsoft.Web.Arr.HostNameRoundRobin" />
                 </hostAffinityProviderList>
             </applicationRequestRouting>
         </webFarms>
    "@
    $xml=[xml]$t
    
    $n = $xml.SelectNodes("//webFarm[@name = 'rpm-lb']/server")
    $n | ForEach-Object{
        "{0} is enabled? {1}" -f $_.address, $_.enabled
    }
    $n | ForEach-Object{
        if     ($_.enabled -eq "true")  {$_.enabled = "false"}
        elseif ($_.enabled -eq "false") {$_.enabled = "true"}
    }
    $xml.Save("c:\junk\NewFile.xml")
    
    1 person found this answer helpful.