我正在尝试在 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 可以执行此操作(网络农场管理单元适用于旧版本)。
我把注意力转移到IIS 管理 Cmdlet但只实现了一半的功能。
我拥有的代码:
#####
# 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 可以使此操作更容易?
有关的但没有太多有用的答案(生成代码的 PowerShell 选项卡保持为空)。
转贴自所以在意识到 SF 存在并且可能更合适之后。
答案1
我见过的实现此目的的最佳方法是将 powershell 与 appcmd.exe 命令混合使用。Microsoft 发布了一些在 docker 容器中设置服务器场的示例代码这里。
以下是一个例子:
ForEach($farm in $farms)
{
# Create the farm
.\appcmd.exe set config -section:webFarms /+"[name='$($farm.name)']" /commit:apphost
ForEach($server in $farm.Servers)
{
# Add server to farm
.\appcmd.exe set config -section:webFarms /+"[name='$($farm.name)'].[address='$($server.address)']" /commit:apphost
}
# URL Rewrite
.\appcmd.exe set config -section:system.webServer/rewrite/globalRules /+"[name='ARR_$($farm.name)_lb', patternSyntax='Wildcard',stopProcessing='True']" /commit:apphost
.\appcmd.exe set config -section:system.webServer/rewrite/globalRules /"[name='ARR_$($farm.name)_lb',patternSyntax='Wildcard',stopProcessing='True']".match.url:"*" /commit:apphost
.\appcmd.exe set config -section:system.webServer/rewrite/globalRules /"[name='ARR_$($farm.name)_lb',patternSyntax='Wildcard',stopProcessing='True']".action.type:"Rewrite" /"[name='ARR_$($farm.name)_lb',patternSyntax='Wildcard',stopProcessing='True']".action.url:"http://$($farm.name)/{R:0}" /commit:apphost
}