[英]Trying to combine replace and new-item in 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\*******s\MonitoringOMTR\MonitorLog_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 '***FS2072\UserData$\aroyet01\Desktop\HostPS.txt') |
if ([bool]((Get-Content -Path "***FS2072\UserData$\aroyet01\Desktop\HostPS.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 '***FS2072\UserData$\aroyet01\Desktop\HostPS.txt'
Get-Content -Path '***FS2072\UserData$\aroyet01\Desktop\HostPS.txt'
查看您的代码片段,我只能通过向您提供从基本 Powershell 语法文档开始的建议来提供帮助。
help about_If
help about_Pipelines
给你一个想法......你的if
被放置在管道中。 您的ForEach-Object
未放置在管道中。 这两个都不像你发布的那样工作。 你可以从这样的事情开始:
$Content = '***FS2072\UserData$\aroyet01\Desktop\HostPS.txt'
if ($Content -condition 'Yourcondition') {
$Content | ForEach-Object {
$_ -replace 'Regex', 'Replacement'
}
} else {
'Your else here'
}
要编辑 xml 文件,您可以查看今天 vonPryz 在这个问题中暗示的这篇文章。
我的任务是对目录中的一些配置文件进行更改,需要更改的文件有 7 个,均以“Monitoring_Tran_xx”开头。
停止将您的 XML 文件视为原始文本 - 它们可以以更高的精度进行解析和处理:)
首先,我们需要将文档解析为 XML,最简单的方法可能是:
$xmlDocument = [xml](Get-Content C:\Path\To\)
由于您有多个具有共同命名模式的文档,我们可能会使用Get-ChildItem
来发现磁盘上的文件并循环它们以获取路径:
Get-ChildItem -Path C:\path\to\config\files\Monitoring_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:\path\to\config\files\Monitoring_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)
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.