希望使用 PowerShell 更新 Web 服務器 IP 地址。
每個主機上的多個 IP。
匯入的組態檔的新配置 IP 地址不正確,因此我想通過在外部“src”檔案中創建站點名稱和關聯系結串列來更改 IP。
然后讓 PowerShell 決議原始組態檔并使用正確的詳細資訊更新相關站點的 IP 地址。
腳本中用于生成新配置的組態檔示例。
<appcmd>
<SITE SITE.NAME="SiteA" SITE.ID="3" bindings="https/4.4.4.4:443:" state="Started">
<site name="SiteA" id="3" serverAutoStart="true">
<bindings>
<binding protocol="https" bindingInformation="4.4.4.4:443:" sslFlags="0" />
</bindings>
</site>
</SITE>
<SITE SITE.NAME="SiteB" SITE.ID="4" bindings="https/32.32.32.32:443:" state="Started">
<site name="SiteB" id="4" serverAutoStart="true">
<bindings>
<binding protocol="http" bindingInformation="32.32.32.32:80:" />
<binding protocol="https" bindingInformation="32.32.32.32:443:" sslFlags="0" />
</bindings>
</site>
</SITE>
</appcmd>
來自外部檔案的 IP 地址,用于填充哈希表。
Name,IPaddr
Site1,10.192.32.1
Site2,10.33.192.1
SiteA,10.2.2.2
Siteb,10.232.323.4
名為 IP 地址檔案路徑和組態檔路徑提示的腳本檔案。
Function checkexists {
Param(
# Parameter help description
[Parameter(Mandatory = $false)]
[string]$PathToFile
)
if (!(Test-Path $PathToFile)) {
Write-Host "File $PathToFile does not exist. Exiting...." -ForegroundColor cyan
start-sleep 3
exit 1
}
}
$csvfile = Read-Host -Prompt "Enter full Path IP Addressing File for WebServers.."
CheckExists -PathToFile $csvfile
$XMLfile = Read-Host -Prompt "Enter full Path to Configuration XML File for WebServers.."
CheckExists -PathToFile $XMLfile
$today = (Get-Date -Format mmhhddMMyy).ToString()
$expFile = (Split-Path -Parent $csvfile) "\WebSites" $today ".xml"
If (!(Test-Path -Path $XMLFile)) {
Write-Host "Exiting as the file does not exist." -ForegroundColor Red
Exit 1
}
$sites = @{}
import-CSV $csvFile | ForEach-Object { $sites[$_.Name] = $_.IPaddr }
[XML]$xml = Get-Content -Path $XMLfile
foreach ($element in $xml.AppCmd.Site) {
$siteIP = $sites.($element.Site.Name)
Write-Host "Planned IP for $($element.Site.Name) is : $siteIp "
$element.bindings = $element.bindings -replace '\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}', $siteIP
$element.site.Bindings.binding.bindingInformation = $element.site.Bindings.binding.bindingInformation -replace '\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}', $siteIP
}
$xml.Save($expFile)
如果 BindingInformation 節點/元素中存在單個節點值,則在運行時它會填充新的組態檔。
如果 Web 服務器支持多個端點,我不知道如何更新這兩個元素。
Planned IP for SiteB is : 10.232.323.4
InvalidOperation: C:\somescript.ps1:58:8
Line |
58 | $element.site.Bindings.binding.bindingInformation = $element.s …
| ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
| The property 'bindingInformation' cannot be found on this object. Verify that the property exists and can be set.
我正在嘗試通過多種嘗試來學習我的方式,并且在彈出多個 IP 之前似乎取得了一些成功。我很欣賞這一切可能是不正確的,但可以用一個指標來做(在外行級別)
任何幫助表示贊賞。
uj5u.com熱心網友回復:
根本原因是成員訪問列舉僅支持獲取屬性值,而不支持設定它們(分配給它們)——請參閱此答案以獲得深入解釋。
因此,因為$element.site.Bindings.binding可以根據情況回傳多個元素,您不能直接分配給(更新)它們的.bindingInformating屬性,而必須單獨處理元素,例如使用內部.ForEach()方法:
foreach ($element in $xml.AppCmd.Site) {
# Note: 'SITE.NAME' is a *single* property name must therefore be quoted.
$siteIP = $sites.($element.'SITE.NAME')
Write-Host "Planned IP for $($element.Site.Name) is : $siteIp "
$element.bindings = $element.bindings -replace '\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}', $siteIP
# !! Because $element.site.Bindings.binding can return an ARRAY of
# !! elements and you want to modify a property of EACH, you must
# !! process them individually, such as with .ForEach()
$element.site.Bindings.binding.ForEach({
$_.bindingInformation = $_.bindingInformation -replace '\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}', $siteIP
})
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/535501.html
