我正在尝试编写代码以检查配置文件(如果它包含某些UserParameters),以及是否不将其添加到文件中。我觉得我的代码在很多层面上都是绝对错误的,而且我没有想法。
$s1 = 'a'
$s2 = 'b'
$s3 = 'c'
$s4 = 'd'
$s5 = 'e'
$s6 = 'f'
$s7 = 'g'
$s8 = 'h'
$scripts = $s1, $s2, $s3, $s4, $s5, $s6, $s7, $s8
foreach ($element in $scripts) {
if ("(UserParameter=$element)" -notmatch $config) {
Get-Content $File $element -replace '^(UserParameter=)',"`$1$($element)"
} else {
Add-Content $File "UserParameter=$element"
}
我不完全知道如何确保会有这样的事情:
UserParameter=a UserParameter=b ... UserParameter=h
编辑:
我当前拥有的配置 https://pastebin.com/BdwH53QZ
我想获得的配置状态: https://pastebin.com/6f0xe2k3
在文件末尾添加了新行。
UserParameter=windowsdisk.discovery, powershell -NoProfile -File discover_PerfMon_PhysicalDisk.ps1
UserParameter=vfs.fs.partition.label[*], powershell -NoProfile -File get_volume_label.ps1 $1
UserParameter=psh.rds.empty.session.detect, powershell -NoProfile -File rds_empty_session_detect.ps1
UserParameter=psh.pki.rootca.thumb.count[*], powershell -NoProfile -Command "(Get-ChildItem Cert:\LocalMachine\Root\$1 -ErrorAction SilentlyContinue).Count"
答案 0 :(得分:0)
由于您的UserParameters非常复杂,并且将它们与文件进行比较会失败,即使在空格上有细微的差异,我也怀疑您的方法有效。
如果给定的UserParameter已经存在,我建议找到一些正则表达式进行过滤。
如果选择PowerShell,则不可避免地需要学习此脚本语言或将其委派给其他人。
您可以尝试以下方法:
## Q:\Test\2019\04\24\SO_55827904.ps1
$UserParameters = @"
UserParameter=windowsdisk.discovery, powershell -NoProfile -File discover_PerfMon_PhysicalDisk.ps1
UserParameter=vfs.fs.partition.label[*], powershell -NoProfile -File get_volume_label.ps1 $1
UserParameter=psh.rds.empty.session.detect, powershell -NoProfile -File rds_empty_session_detect.ps1
UserParameter=psh.pki.rootca.thumb.count[*], powershell -NoProfile -Command "(Get-ChildItem Cert:\LocalMachine\Root\$1 -ErrorAction SilentlyContinue).Count"
"@ -split '\r?\n'
ForEach ($File in (Get-ChildItem -Path X:\path -Filter *zabbix_agent.conf)){
$FileContent = Get-Content $File -raw
ForEach ($UserParam in $UserParameters){
if($FileContent -notmatch [RegEx]::Escape($UserParam)){
Add-Content -Path $File.FullName -Value $UserParam
}
}
}
您必须使-Path
和-Filter
适应您的环境。
答案 1 :(得分:0)
另一种方法可能是:
$file = 'D:\zabbix_agent.conf'
# create an array of userparameters to add if not already present
$userParams = 'UserParameter=windowsdisk.discovery, powershell -NoProfile -File discover_PerfMon_PhysicalDisk.ps1',
'UserParameter=vfs.fs.partition.label[*], powershell -NoProfile -File get_volume_label.ps1 $1',
'UserParameter=vfs.fs.partition.label[*], powershell -NoProfile -File get_volume_label.ps1 $2',
'UserParameter=psh.rds.empty.session.detect, powershell -NoProfile -File rds_empty_session_detect.ps1',
'UserParameter=psh.pki.rootca.thumb.count[*], powershell -NoProfile -Command "(Get-ChildItem Cert:\LocalMachine\Root\$1 -ErrorAction SilentlyContinue).Count"'
# read the file as a string array
$content = Get-Content -Path $file
# remember the original lines count so we know if parameters were added or not
$originalCount = $content.Count
# get a sub-array of all lines from $content that start with 'UserParameter='
$currentParams = @($content | Where-Object { $_ -match '^UserParameter=' })
if ($currentParams.Count -eq 0) {
# if the config file did not have any 'UserParameter=' lines, we add the whole $userParams array to the content
$content += $userParams
}
else {
# there were 'UserParameter=' lines found, so filter out all required parameters that need to be added
$addThese = @($userParams | Where-Object { $currentParams -notcontains $_ })
if ($addThese.Count) { $content += $addThese }
}
if ($originalCount -ne $content.Count) {
Write-Host "Writing new Zabbix configuration file." -ForegroundColor Yellow
Set-Content -Path $file -Value $content
}
else {
Write-Host "File '$file' already contained the required user parameters." -ForegroundColor Green
}
答案 2 :(得分:0)
这是我的版本。 [咧嘴]我从您的样本中制作了两个文件。一条没有UserParameter行,一条带有两条这样的行。下面的代码检查是否有任何此类行...如果没有,则将所有行都添加。如果是,则会添加缺少的所需值列表中的值。
$FileList = Get-ChildItem -Path 'C:\Temp\JDHellequin_-_sample - *.txt'
$Prefix = 'UserParameter='
$NeededParamList = @(
'windowsdisk.discovery, powershell -NoProfile -File discover_PerfMon_PhysicalDisk.ps1'
'vfs.fs.partition.label[*], powershell -NoProfile -File get_volume_label.ps1 $1'
'psh.rds.empty.session.detect, powershell -NoProfile -File rds_empty_session_detect.ps1'
'psh.pki.rootca.thumb.count[*], powershell -NoProfile -Command "(Get-ChildItem Cert:\LocalMachine\Root\$1 -ErrorAction SilentlyContinue).Count"'
)
foreach ($FL_Item in $FileList)
{
$TargetLines = @(Get-Content -LiteralPath $FL_Item.FullName).
Where({
$_.StartsWith($Prefix)
}).
ForEach({
$_.Split('=')[1]
})
foreach ($NPL_Item in $NeededParamList)
{
$NewLine = '{0}{1}' -f $Prefix, $NPL_Item
if ([string]::IsNullOrEmpty($TargetLines))
{
Add-Content -LiteralPath $FL_Item.FullName -Value $NewLine
}
else
{
if ($NPL_Item -notin $TargetLines)
{
Add-Content -LiteralPath $FL_Item.FullName -Value $NewLine
}
}
}
}
在每种情况下,添加到的文件都有四行与$NeededParamList
集合中的必需项目匹配。