Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/powershell/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
难以在powershell脚本(Azure Devops Yaml)中转义引号字符_Powershell_Azure Yaml Pipelines - Fatal编程技术网

难以在powershell脚本(Azure Devops Yaml)中转义引号字符

难以在powershell脚本(Azure Devops Yaml)中转义引号字符,powershell,azure-yaml-pipelines,Powershell,Azure Yaml Pipelines,My azure piplines yaml脚本使用powershell将.CS文件中的占位符字符串替换为当前日期字符串。这是具有要替换的值的行(20200101000000) 这是实现此功能的powershell步骤 pwsh: (get-content -path $(versionFile)) | foreach-object {$_ -replace "20200101000000", (get-date -f 'yyyyMMddhhmmss')} | set-con

My azure piplines yaml脚本使用powershell将.CS文件中的占位符字符串替换为当前日期字符串。这是具有要替换的值的行(
20200101000000

这是实现此功能的powershell步骤

pwsh: (get-content -path $(versionFile)) | foreach-object {$_ -replace "20200101000000", (get-date -f 'yyyyMMddhhmmss')} | set-content -path $(versionFile)
  displayName: 'Update time stamp file'
我想改变这一步,在搜索字符串周围包含引号字符
,并将它们与新日期一起写入新的输出值。但我似乎无法做到这一点

我错误地将转义引号字符
\“
放在搜索中并替换字符串。但我猜你不能在一个带引号的字符串中逃逸,所以它不起作用

pwsh: (get-content -path $(versionFile)) | foreach-object {$_ -replace "\"20200101000000\"", (get-date -f '\"yyyyMMddhhmmss\"')} | set-content -path $(versionFile)
这就是错误:

##[command]"C:\Program Files\PowerShell\7\pwsh.exe" -NoLogo -NoProfile -NonInteractive -ExecutionPolicy Unrestricted -Command ". 'D:\a\_temp\80625e52-1302-4e35-a799-223ab893bcf1.ps1'"
ParserError: D:\a\_temp\80625e52-1302-4e35-a799-223ab893bcf1.ps1:3
Line |
   3 |  … lyInfo.cs) | foreach-object {$_ -replace "\"20200101000000\"", (get-d …
     |                                                ~~~~~~~~~~~~~~~~~
     | Unexpected token '20200101000000\""' in expression or statement.
##[error]PowerShell exited with code '1'.
我还尝试在脚本的
get date
部分使用双引号,这样就可以转义引号字符,但这似乎也不起作用。我猜这是以这种方式编写脚本的一个限制

是否有其他方法来实现我的目标?

您可以使用添加任意字符,类似于.NET中的
DateTime.ToString()
函数

'[Attrib("20200101000000")]' -replace '"20200101000000"', (get-date -UFormat '"%Y%m%d%H%M%S"')

Powershell中的转义字符是反斜杠(`),而不是反斜杠(\)。试试看,例如,
“`20200101000000`

'[Attrib("20200101000000")]' -replace '"20200101000000"', (get-date -UFormat '"%Y%m%d%H%M%S"')