【发布时间】:2020-08-11 16:48:29
【问题描述】:
我正在编写自定义 Powershell Azure CD 管道任务(用于 VM),其中我的 web.config 应替换为管道变量。我在 Azure CD 管道变量中定义了与 WebService 和 AuditService 一样的示例配置文件。
<client>
<endpoint address="__WebService__" binding="basicHttpBinding" bindingConfiguration="ServiceSoap" contract="WebServiceClient.ServiceSoap" name="NLSService" />
<endpoint address="__AuditService__" binding="basicHttpBinding" bindingConfiguration="BasicHttpBinding_Audit" contract="AuditApiService.Audit" name="AuditService" />
</client>
我有 powershell 脚本作为
$zipfileName = "$(System.DefaultWorkingDirectory)\_WebService-CI\drop\Service.zip"
$fileToEdit = "web.config"
$reg = [regex] '__(.*?)__'
[Reflection.Assembly]::LoadWithPartialName("System.IO.Compression.FileSystem");
# Open zip and find the particular file (assumes only one inside the Zip file)
$zip = [System.IO.Compression.ZipFile]::Open($zipfileName,"Update")
$configFile = $zip.Entries.Where({$_.name -like $fileToEdit})
# Read the contents of the file
$desiredFile = [System.IO.StreamReader]($configFile).Open()
$text = $desiredFile.ReadToEnd()
$desiredFile.Close()
$desiredFile.Dispose()
$text = $text -replace $reg, $(${1}) -join "`r`n"
#update file with new content
$desiredFile = [System.IO.StreamWriter]($configFile).Open()
$desiredFile.BaseStream.SetLength(0)
# Insert the $text to the file and close
$desiredFile.Write($text)
$desiredFile.Flush()
$desiredFile.Close()
# Write the changes and close the zip file
$zip.Dispose()
那么我该如何动态替换“__”中的正则表达式内容并将该内容视为一个变量,该变量应该查看管道变量并在行中替换它:
$text = $text -replace $reg, $(${1}) -join "rn"
【问题讨论】:
-
留意其他答案。这很可能是一种更好的方法。如果您选择继续使用这个,请查看stackoverflow.com/questions/30666101/…,您可以将其与可以在这里正常工作的替换哈希表合并。
标签: azure powershell azure-devops continuous-deployment