수많은 web.config 파일이 있습니다. 어떤 URL에 컴퓨터 이름이 포함되어 있습니다.powershell을 사용하여 [xml] 속성의 문자열을 업데이트하는 방법은 무엇입니까?
이 내 샘플 web.config 파일
사용자가 컴퓨터 이름이 모든 XML 파일에 업데이트해야하는 변경<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<system.Model>
<client>
<endpoint address="http://MyMachineName:80/MyProj/Core/NewService/SecurityObject.0/Secretservice.svc" binding="basicHttpBinding" bindingConfiguration="MyProj_Internal_Routing" behaviorConfiguration="UserSIDBehavior" contract="SecurityMgmt.SecurityMgmtContract" name="HttpEndpointSecMgt" />
<endpoint address="http://MyMachineName:80/MyProj/Core/DataObject/SystemCatalogs1.0/DataObjectservice/DataObjectservice.svc" binding="basicHttpBinding" bindingConfiguration="MyProj_Internal_Routing" behaviorConfiguration="UserSIDBehavior" contract="DataObjectservice.SystemCatalogsDataObjectservice" name="BasicHttpBinding_SystemCatalogsDataObjectservice" />
<endpoint address="http://MyMachineName:80/MyProj/Core/NewService/Movement.0/Movement.svc" binding="basicHttpBinding" bindingConfiguration="MyProj_Internal_Routing" behaviorConfiguration="UserSIDBehavior" contract="NavigationClientLib.NavigationNewService" name="BasicHttpBinding_NavigationNewService" />
</client>
<system.Model>
</configuration>
입니다.
나는 이와 같은 함수를 작성하기로 결정했습니다.
Function Update-MachineName ([string] $FilePath,[string] $xpath, [string] $NewMachineName="NewComputer") {
# Get the config file contents
$WebConfig = [XML] (Get-content $Filepath)
#Find the url using xpath which has old Machine name
$hostnameString= Select-XML -XML $WebConfig -XPath $Xpath
Foreach ($hostname in $hostnameString) {
if (($hostname.Node.value -match 'http') -eq $true) {
$hostname.Node.value
$MachineOldName = $($($hostname.Node.value.split("/"))[2].split(":"))[0]
$MachineOldName
$hostname.Node.value.replace($MachineOldName,$NewMachineName)
}
}
$WebConfig.Save($FilePath)
}
Update-MachineName "C:\web.config" "//@address"
나는 새 컴퓨터 이름으로 바뀌지 만 여전히 web.config 콘텐츠는 업데이트되지 않습니다. 새 컴퓨터 이름으로 업데이트하는 방법
: 나는 또한 기능을하지만 여전히 아무 소용 저장 추가 ($ WebConfig.Save ($ FilePath를