使用正确的行对齐方式替换多个文件中的文本使用-powershell.ps1

问题描述 投票:0回答:1

输出没有得到相同的行对齐替换..但它没有得到相同的对齐(它应该替换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 azure-devops azure-functions powerquery powershell-4.0
1个回答
0
投票

我尝试使用下面的

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

输出:-

enter image description here

替换内容:-

enter image description here

<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>
© www.soinside.com 2019 - 2024. All rights reserved.