在PowerShell中创建Web场

时间:2017-11-09 14:10:04

标签: powershell iis load-balancing arr

我正在尝试在PowerShell中自动创建服务器场。通过手动创建,我得到了以下XML:

<webFarms>
    <webFarm name="alwaysup" enabled="true">
        <server address="alwaysup-blue" enabled="true">
            <applicationRequestRouting httpPort="8001" />
        </server>
        <server address="alwaysup-green" enabled="true">
            <applicationRequestRouting httpPort="8002" />
        </server>
        <applicationRequestRouting>
            <healthCheck url="http://alwaysup/up.html" interval="00:00:05" responseMatch="up" />
        </applicationRequestRouting>
    </webFarm>
    <applicationRequestRouting>
        <hostAffinityProviderList>
            <add name="Microsoft.Web.Arr.HostNameRoundRobin" />
        </hostAffinityProviderList>
    </applicationRequestRouting>
</webFarms>

尝试通过PS执行此操作证明很麻烦:据我所知,没有专门的API来执行此操作(WebFarmSnapin适用于旧版本。)

我已将注意力转移到IIS Administration Cmdlets但只能使其工作一半。

我的代码:

#####
# Overwriting the server farm
#####

Write-Host "Overwriting the server farm $($webFarmName)"

$webFarm = @{};
$webFarm["name"] = 'siteFarm'

Set-WebConfiguration "/webFarms" -Value $webFarm

#####
# Adding the servers
#####

Write-Host "Adding the servers"

$blueServer = @{}
$blueServer["address"] = 'site-blue'
$blueServer["applicationRequestRouting"] = @{}

$greenServer = @{}
$greenServer["address"] = 'site-green'
$greenServer["applicationRequestRouting"] = @{}

$servers = @($blueServer, $greenServer)

Add-WebConfiguration -Filter "/webFarms/webFarm[@name='siteFarm']" -Value $servers

#####
# Adding routing
#####

Write-Host "Adding the routing configurations"

$blueServerRouting = @{}
$blueServerRouting["httpPort"] = "8001"
Add-WebConfiguration -Filter "/webFarms/webFarm[@name='siteFarm']/server[@address='site-blue']" -Value $blueServerRouting

这会生成

<webFarms>
    <webFarm name="siteFarm">
        <server address="site-blue" />
        <server address="site-green" />
    </webFarm>
    <applicationRequestRouting>
        <hostAffinityProviderList>
            <add name="Microsoft.Web.Arr.HostNameRoundRobin" />
        </hostAffinityProviderList>
    </applicationRequestRouting>
</webFarms>

正如您所看到的那样,它缺少与路由相关的端口。而且我还没有开始尝试在此时添加健康检查。

我做错了什么?是否有一些我没有找到的Cmdlet让这更容易?

Related但没有太多有用的答案(带有生成代码的PowerShell标签保持为空)。

3 个答案:

答案 0 :(得分:4)

您似乎通过修改XML配置文件本身来了解如何执行此操作。尽管感觉这似乎不是“正确的方式”,但直接更改配置文件是一个非常有效的解决方案。实际上,您创建了一个模板,配置模板提供了一种快速且可读的方法来生成可维护和可重复的配置。

我们可以通过将脚本中的模板文本提取到单独的文本文件中来改进此方法。然后,脚本可以读取(或获取)模板,并根据需要交换任何占位符值。这类似于我们通过将HTML模板与代码分离来分离Web应用程序中的问题。

为了更直接地回答这个问题,让我们看一下如何使用PowerShell和IIS管理API来做到这一点(你是对的 - 如果是IIS和Windows,最近的版本不再支持Web Farm Framework)。问题中的原始代码是一个良好的开端。我们只需区分操作配置集合(使用*-WebConfiguration cmdlet)和配置(使用*-WebConfigurationProperty cmdlet)。这是一个脚本,它将根据问题中的示例设置配置值:

$farmName = 'siteFarm'

Add-WebConfigurationProperty -PSPath 'MACHINE/WEBROOT/APPHOST' `
    -Filter 'webFarms' `
    -Name '.'  `
    -Value @{ name = $farmName; enabled = $true }

Add-WebConfiguration -PSPath 'MACHINE/WEBROOT/APPHOST' `
    -Filter "webFarms/webFarm[@name='$farmName']" `
    -Value @(
        @{ address = 'site-blue'; enabled = $true },
        @{ address = 'site-green'; enabled = $true }
    )

Set-WebConfigurationProperty -PSPath 'MACHINE/WEBROOT/APPHOST' `
    -Filter "webFarms/webFarm[@name='$farmName']/server[@address='site-blue']" `
    -Name 'applicationRequestRouting' `
    -Value @{ httpPort = 8001 }

Set-WebConfigurationProperty -PSPath 'MACHINE/WEBROOT/APPHOST' `
    -Filter "webFarms/webFarm[@name='$farmName']/server[@address='site-green']" `
    -Name 'applicationRequestRouting' `
    -Value @{ httpPort = 8002 }

Set-WebConfigurationProperty -PSPath 'MACHINE/WEBROOT/APPHOST' `
    -Filter "webFarms/webFarm[@name='$farmName']/applicationRequestRouting" `
    -Name 'healthCheck' `
    -Value @{
        url = 'http://mySite/up.html'
        interval = '00:00:05'
        responseMatch = 'up'
    }

Set-WebConfigurationProperty -PSPath 'MACHINE/WEBROOT/APPHOST' `
    -Filter "webFarms/webFarm[@name='$farmName']/applicationRequestRouting" `
    -Name 'protocol' `
    -Value @{ reverseRewriteHostInResponseHeaders = $true }

Set-WebConfigurationProperty -PSPath 'MACHINE/WEBROOT/APPHOST' `
    -Filter "webFarms/webFarm[@name='$farmName']/applicationRequestRouting/protocol" `
    -Name 'cache' `
    -Value @{ enabled = $false; queryStringHandling = 'NoCaching' }

这可能比必要的更冗长,但我想清楚地说明每一步的意图。此实现使用-Filter参数的XPath查询来选择适当的XML节点。或许,我们可以重构脚本以减少重复性任务,例如通过定义一个带有服务器名称和端口的Add-FarmServer函数,然后添加适当的指令。如果我们遇到锁定的配置问题,我们可能还需要Remove-WebConfigurationLock

我们选择使用模板还是程序化方法取决于项目和团队偏好。当我们要配置许多类似的项目时,API变得更有吸引力,例如,如果我们有数百台服务器要添加到Web场。另一方面,模板易于理解,不要求其他团队成员学习新的(可能有些令人困惑的)API。

答案 1 :(得分:2)

我一直在努力,我发现你可以在Powershell中使用Microsoft.Web.Administration来实现这一目标。

Add-Type -AssemblyName "Microsoft.Web.Administration, Version=7.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL"

$iisManager = New-Object Microsoft.Web.Administration.ServerManager
$applicationHostConfig = $IISManager.GetApplicationHostConfiguration()

# Get the WebFarms section. Section names are case-sensitive.
$webFarmSection = $ApplicationHostConfig.GetSection("webFarms")
$webFarms = $WebFarmSection.GetCollection()

# Create a new webfarm and assign a name.
$webFarm = $WebFarms.CreateElement("webFarm")
$webFarm.SetAttributeValue("name", "MyNewWebFarmName")
# Add it to the parent element
$webFarms.Add($webFarm)
# get Webfarm server collection
$servers = $webFarm.GetCollection()
# add server
$serverBlue = $servers.CreateElement("server")
$routingBlue = $serverBlue.GetChildElement("applicationRequestRouting")
$routingBlue.SetAttributeValue("httpPort", "8001")
$serverBlue.SetAttributeValue("address", "MyNewWebFarmName-blue")
$servers.Add($serverBlue)
# Save changes
$iisManager.CommitChanges()

有两件事值得一提:

  1. 调用$iisManager.CommitChanges()后,对象进入只读模式。在进行任何新的更改之前,您需要重新实例化所有对象。

  2. 如果您决定在Web场中创建新服务器,则在为服务器分配名称然后尝试访问其端口设置时,您将遇到问题。您需要做的是先分配端口,然后分配名称以及是否启用/禁用。否则,它会抛出System.AccessViolationException: Attempted to read or write protected memory.错误。

  3. 让我知道它是怎么回事!

答案 2 :(得分:1)

看上去没有人提出解决方案:我已经采取了直接编辑XML而不是通过API的非常简单的解决方法。

实际的webfarm功能我还没有开始工作,但似乎所有部分都至少存在。

操作XML的一个例子是:

/**
 * @param mixed $object
 * @param array $arguments
 */
public function save($object, array $arguments = ['flush' => true])
{
    $this->em->persist($object);
    if (true === $arguments['flush']) {
        $this->em->flush();
    }
}