正在尝试在powershell中组合替换项和新项



我有一项任务要更改目录中的一些配置文件,需要更改的文件有7个,都以"Monitoring_Tran_xx";。

在这些文件中,有某些值(TransactionID="01" AgreedResponseTime="500" SearchProfileID="216")需要更改,但不存在于所有7个文件中,并且在替换或创建它们之前需要检查它们是否存在。此外,我正在尝试插入一个新参数(使用新项(,如果某个参数等于某个值,例如if TemplateType = "4152",则在其旁边创建一个新的参数"DirectoryPoolID = '3' "

我会感谢你在这方面的帮助。

感谢

下面的配置文件示例:

<?xml *version="1.0"* ?>
<Monitor>
<Configuration OperatingMode="Transaction" LogFileName="C:Program Files*******sMonitoringOMTRMonitorLog_01.log" WriteFileInterval="120" ConnectionKey="**********" />
<TransactionMonitoringConfig TransactionID="01" AgreedResponseTime="500" SearchProfileID="216" />
<ShowMessages>
<Message Name="MOISearchStart" />
<Message Name="MOOSearchFound" />
<Message Name="MOOSearchEnd" />
<Message Name="MOOAlert" />
</ShowMessages>
<PerformanceCounters TransactionCount="191" TransactionBreaches="0" />
</Monitor>

我尝试过的Powershell脚本,但结果不太好:

(Get-Content -Path '***FS2072UserData$aroyet01DesktopHostPS.txt') |
if ([bool]((Get-Content -Path "***FS2072UserData$aroyet01DesktopHostPS.txt") -like '*DirectoryPool*', '*SearchProfile*')) { 
write-host "Found it"
}
else {
write-host "Did not find it"
}
ForEach-Object {$_ -replace 'TransactionCount="191"', 'TransactionCount="196"'} |
Set-Content -Path '***FS2072UserData$aroyet01DesktopHostPS.txt'
Get-Content -Path '***FS2072UserData$aroyet01DesktopHostPS.txt'

我有一项任务要更改目录中的一些配置文件,需要更改的文件有7个,都以"Monitoring_Tran_xx";。

停止将XML文件视为原始文本-它们可以以更好的精度进行解析和处理:(

首先,我们需要将文档解析为XML,最简单的方法可能是:

$xmlDocument = [xml](Get-Content C:PathTo)

由于您有多个具有通用命名模式的文档,我们可能会使用Get-ChildItem来发现磁盘上的文件,并在它们上循环以获得路径:

Get-ChildItem -Path C:pathtoconfigfilesMonitoring_Tran_*.ps1 |ForEach-Object {
# read each document from disk with `Get-Content`, convert to [xml]
$xmlDocument = [xml]($_ |Get-Content)
}

接下来,我们需要能够导航我们的XML文档。PowerShell对此具有本机语法绑定:

$tmConfig = $xmlDocument.Monitor.TransactionMonitoringConfig

或者,您也可以使用XPath表达式来导航文档:

$tmConfig = $xmlDocument.SelectSingleNode("/Monitor/TransactionMonitoringConfig")

在这些文件中,有某些值(TransactionID="01" AgreedResponseTime="500" SearchProfileID="216"(需要更改,但不存在于所有7个文件中,并且需要在替换或创建之前检查它们是否存在

要检查节点上是否存在命名属性,我们可以使用HasAttribute()方法:

if($tmConfig.HasAttribute('TransactionID')){
# attribute exists, let's update it!
$tmConfig.SetAttribute('TransactionID', 123) # replace 123 with whatever ID you need
}

此外,如果某个参数等于某个值,我正在尝试插入一个新参数(使用新项(,例如,如果TemplateType = "4152",则在其旁边创建一个新的参数"DirectoryPoolID = '3' "

如果只想在另一个属性具有特定值的情况下向节点添加新属性,则可以使用GetAttribute()SetAttribute():

if($xmlNode.GetAttribute('TemplateType') -eq "4152"){
$xmlNode.SetAttribute("DirectoryPoolID", 3)
}

完成后,将文档保存回文件:

Get-ChildItem -Path C:pathtoconfigfilesMonitoring_Tran_*.ps1 |ForEach-Object {
# read each document from disk with `Get-Content`, convert to [xml]
$xmlDocument = [xml]($_ |Get-Content)
<# write all the code to inspect and update the attributes here #>
# write document back to disk
$xmlDocument.Save($_.FullName)
}

查看您的代码片段,我只能为您提供从基本Powershell语法文档开始的建议。

help about_If
help about_Pipelines

给你一个想法。。。您的if已放置在管道中。您的ForEach-Object未放置在管道中。这两个都不是你发布的方式。你可以从这样的东西开始:

$Content = '***FS2072UserData$aroyet01DesktopHostPS.txt'
if ($Content -condition 'Yourcondition') {
$Content | ForEach-Object {
$_ -replace 'Regex', 'Replacement'
}
} else {
'Your else here'
}

对于编辑xml文件,您可以看看这篇文章,正如vonPryz在今天的问题中所暗示的那样。

最新更新