我有以下PowerShell命令替换文件:
(Get-Content c:\myfile.txt) | ForEach-Object {
$_ -replace "date is x", "date is 2015.02.29"
} | Set-Content c:\myfile.txt
我希望硬编码日期(2015.02.29)来自另一个cmdlet:
(Get-Date -format yyyy.MM.dd)
我该如何注射?
答案 0 :(得分:2)
只需用您的命令替换您的硬编码值:
(Get-Content c:\myfile.txt) | ForEach-Object {
$_ -replace "date is x", "date is $(Get-Date -format yyyy.MM.dd)"
} | Set-Content c:\myfile.txt