gpt4 book ai didi

powershell - 在 PowerShell 中创建网络场

转载 作者:行者123 更新时间:2023-12-03 20:53:57 25 4
gpt4 key购买 nike

我正在尝试在 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 选项卡保持为空)。

最佳答案

我一直在研究这个,我发现你可以使用 Microsoft.Web.Administration在 Powershell 中完成此操作。

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()

有两件事很重要:
  • 一旦您调用 $iisManager.CommitChanges()然后对象进入只读模式。在进行任何新更改之前,您需要重新实例化所有对象。
  • 如果您决定在您的网络场中创建一个新服务器,如果您为服务器分配一个名称然后尝试访问其端口设置,您将遇到问题。您需要做的是先分配端口,然后分配名称以及是否启用/禁用。否则,它会抛出 System.AccessViolationException: Attempted to read or write protected memory.错误。

  • 让我知道事情的后续!

    关于powershell - 在 PowerShell 中创建网络场,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47203954/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com