输出没有得到相同的行对齐替换..但它没有得到相同的对齐(它应该替换3行。但它会进行之字形替换..看起来很难看,有什么帮助吗
`$configFiles = Get-ChildItem . *.csconfigini -rec
foreach ($file in $configFiles)
{
try {
Write-Host "Processing $($file.FullName)"
$content = Get-Content $file.PSPath -Raw -ErrorAction Stop
if ($content -match "(<BatchReleaseType>datacenter</BatchReleaseType>)") {
Write-Host "Match found in $($file.FullName)"
$leadingWhitespace = $matches[1] -replace '<String>Invoice</battery>.*', ''
$replacement = @"
${leadingWhitespace}<Hello world=""`'$($Apple)'=='Hero'"">Invoice</battery>
${leadingWhitespace} <Sunday>World</Sunday>
${leadingWhitespace} <battery>Hello</battery>
"@
$content = $content -replace "<String>Invoice</battery>", $replacement
$content | Set-Content $file.PSPath -ErrorAction Stop
Write-Host "Replacement done for $($file.FullName)"
} else {
Write-Host "No match found in $($file.FullName)"
}
} catch [System.UnauthorizedAccessException] {
Write-Host "Skipping file $($file.PSPath) due to permission issue."
} catch {
Write-Host "Error occurred while processing $($file.PSPath): $_"
}
}
`
我尝试使用下面的
Azure Functions Powershell http trigger
代码和替换已成功完成,请参阅下面:-
using namespace System.Net
# Input bindings are passed in via param block.
param($Request, $TriggerMetadata)
# Write to the Azure Functions log stream.
Write-Host "PowerShell HTTP trigger function processed a request."
# Define the PowerShell script
$script = @'
$configFiles = Get-ChildItem . *.csconfigini -rec
foreach ($file in $configFiles) {
try {
Write-Host "Processing $($file.FullName)"
$content = Get-Content $file.PSPath -Raw -ErrorAction Stop
if ($content -match "(<BatchReleaseType>datacenter</BatchReleaseType>)") {
Write-Host "Match found in $($file.FullName)"
$leadingWhitespace = $matches[1] -replace '<String>Invoice</battery>.*', ''
$replacement = @"
${leadingWhitespace}<Hello world=""`'$($Apple)'=='Hero'"">Invoice</battery>
${leadingWhitespace} <Sunday>World</Sunday>
${leadingWhitespace} <battery>Hello</battery>
"@
$pattern = "<String>Invoice</battery>"
$content = $content -replace $pattern, $replacement
$content | Set-Content $file.PSPath -ErrorAction Stop
Write-Host "Replacement done for $($file.FullName)"
} else {
Write-Host "No match found in $($file.FullName)"
}
} catch [System.UnauthorizedAccessException] {
Write-Host "Skipping file $($file.PSPath) due to permission issue."
} catch {
Write-Host "Error occurred while processing $($file.PSPath): $_"
}
}
'@
# Execute the PowerShell script
Invoke-Expression $script
# Interact with query parameters or the body of the request.
$name = $Request.Query.Name
if (-not $name) {
$name = $Request.Body.Name
}
$body = "This HTTP triggered function executed successfully. Pass a name in the query string or in the request body for a personalized response."
if ($name) {
$body = "Hello, $name. This HTTP triggered function executed successfully."
}
# Associate values to output bindings by calling 'Push-OutputBinding'.
Push-OutputBinding -Name Response -Value ([HttpResponseContext]@{
StatusCode = [HttpStatusCode]::OK
Body = $body
})
输出:-
替换内容:-
<BatchReleaseType>datacenter</BatchReleaseType>
<BatchReleaseType>datacenter</BatchReleaseType><Hello world=""''=='Hero'"">Invoice</battery>
<BatchReleaseType>datacenter</BatchReleaseType> <Sunday>World</Sunday>
<BatchReleaseType>datacenter</BatchReleaseType> <battery>Hello</battery>
<AnotherTag>SomeValue</AnotherTag>