PowerShell脚本使用RegEx替换字符串之间的行

时间:2013-03-15 07:36:19

标签: regex powershell

我想替换

之间的线路
<? and <Arguments>

以及

</Arguments> and </Task> 

通过powershell和regex

这是整个字符串

<?xml version="1.0" encoding="UTF-16"?>
<Task version="1.1" xmlns="http://schemas.microsoft.com/windows/2004/02/mit/task">
  <RegistrationInfo>
    <Author>administrator</Author>
  </RegistrationInfo>
  <Triggers>
    <CalendarTrigger>
      <Enabled>true</Enabled>
      <StartBoundary>2013-03-13T00:34:00</StartBoundary>
      <ScheduleByDay>
        <DaysInterval>1</DaysInterval>
      </ScheduleByDay>
    </CalendarTrigger>
  </Triggers>
  <Settings>
    <Enabled>true</Enabled>
    <ExecutionTimeLimit>PT259200S</ExecutionTimeLimit>
    <Hidden>false</Hidden>
    <WakeToRun>false</WakeToRun>
    <DisallowStartIfOnBatteries>false</DisallowStartIfOnBatteries>
    <StopIfGoingOnBatteries>false</StopIfGoingOnBatteries>
    <RunOnlyIfIdle>false</RunOnlyIfIdle>
    <Priority>5</Priority>
    <IdleSettings>
      <Duration>PT600S</Duration>
      <WaitTimeout>PT3600S</WaitTimeout>
      <StopOnIdleEnd>false</StopOnIdleEnd>
      <RestartOnIdle>false</RestartOnIdle>
    </IdleSettings>
  </Settings>
  <Principals>
    <Principal id="Author">
      <RunLevel>HighestAvailable</RunLevel>
      <UserId>SMETEST\Administrator</UserId>
      <LogonType>InteractiveTokenOrPassword</LogonType>
    </Principal>
  </Principals>
  <Actions Context="Author">
    <Exec>
      <Command>C:\Program Files\NetApp\SnapManager for Exchange\SMEJobLauncher.exe</Command>
      <Arguments>new-backup -Server 'SME' -ManagementGroup 'Standard' -RetainBackups 8 -RetainUtmBackups 20 -Sto
rageGroup 'testingDB1' -UseMountPoint  -MountPointDir 'C:\Program Files\MgrMountPoi
nt' -RemoteAdditionalCopyBackup $False</Arguments>
      <WorkingDirectory>C:\Program Files\NetApp\SnapManager for Exchange\</WorkingDirectory>
    </Exec>
  </Actions>
</Task>

我只想让输出看起来像

new-backup -Server 'SME' -ManagementGroup 'Standard' -RetainBackups 8 -RetainUtmBackups 20 -StorageGroup 'testingDB1' -UseMountPoint  -MountPointDir 'C:\Program Files\MgrMountPoint' -RemoteAdditionalCopyBackup $False

1 个答案:

答案 0 :(得分:5)

不要在使用XML的正则表达式上浪费时间 - 而是使用Select-Xml和XPath:

Select-Xml -Path .\Test.xml -Namespace @{ 
    t = "http://schemas.microsoft.com/windows/2004/02/mit/task" 
} -XPath //t:Arguments | foreach { $_.node.InnerText -replace '\n' }

我删除换行符,假设这就是你想要的。