我正在编写自定义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“r
n”
有什么原因不能在市场上使用替换令牌任务?这是我替换配置文件中值的方法。
https://marketplace.visualstudio.com/items?itemName=qetza.replacetokens