gpt4 book ai didi

string - 文字替换-字符串和空格

转载 作者:行者123 更新时间:2023-12-03 01:02:18 24 4
gpt4 key购买 nike

我公司已部署Splunk来收集日志并报告系统更改。
Splunk使用两个文件-input.conf和server.conf来跟踪现有主机名并将其报告给Splunk控件。

如果主机名更改(在我们的环境中很常见),系统将报告“主机健全性检查损坏”,要求我们登录到每个系统并用新主机名替换旧主机名。

对于现场的1000个系统来说,这是困难的。

我想自动化该过程,并且我编写的脚本有问题(请参见下文)。

输入和服务器都对.conf文件使用此格式(.conf只是扩展名为.conf的txt文件)。

Host = systemname 

The script below will currently read the text file, and then instead of replacing 'systemname' it will append the changes to the end of the existing value. ie. instead of hostname1hostname2 it does Hostname2Hostname1.

$InputsOLD = "host = *"
$InputsNEW = "host = $Env:COMPUTERNAME"
Get-Content "C:\Program Files\SplunkUniversalForwarder\etc\system\local\inputs.conf" |
Foreach-Object {$_ -replace "$InputsOLD","$InputsNEW"} |
Set-Content "C:\Program Files\SplunkUniversalForwarder\etc\system\local\inputs_1.conf"

$ServerOLD = "serverName = *"
$ServerNew = "serverName = $Env:COMPUTERNAME"
Get-Content "C:\Program Files\SplunkUniversalForwarder\etc\system\local\server.conf" |
Foreach-Object {$_ -replace "$ServerOLD","$ServerNew"} |
Set-Content "C:\Program Files\SplunkUniversalForwarder\etc\system\local\server_1.conf"

最佳答案

-replace运算符基于正则表达式进行匹配。因此,您的匹配表达式:

"host = *"

在匹配第一个“host =”(不包括主机名)之后,将结束其匹配,这就是被替换的内容,此后所有内容保持不变。要将主机名包含在match表达式中,请使用以下正则表达式:
"host = .*"

关于string - 文字替换-字符串和空格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54259368/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com