从powershell替换* dtsConfig文件中的标记

时间:2013-06-19 13:58:05

标签: powershell powershell-v2.0 powershell-v3.0

$xml = [xml](Get-Content C:\temp\STA\*.dtsConfig)
$xml.SelectNodes('//ConfiguredValue')|%{$_.'#text'=($_.'#text' -replace '_\d+',[datetime]::Today.ToString('_MMddyyy'))}
$xml.SelectNodes('//ConfiguredValue')|%{$_.'#text'}
xml.Save('C:\temp\STA\*.dtsConfig')

从前3个步骤我想在dtsConfig中的ConfiguredValue中检查标签,命名为PackageDependencies_somedate(20110528),我想更改PackageDependencies_currentdate(20130619), 将某个日期更改为当前日期并保存文件,

http://i.stack.imgur.com/EHH6h.jpg

1 个答案:

答案 0 :(得分:0)

您的代码应如下所示:

ls C:\temp\STA\*.dtsConfig | foreach {
  $file = $_.FullName;
  $xml = [xml](Get-Content $file)
  $todayStr = [datetime]::Today.ToString('_MMddyyy')
  @($xml.SelectNodes('//ConfiguredValue')) | %{
      $_.'#text'=($_.'#text' -replace '_\d+',$todayStr)
  }
  $xml.Save($file)
}