使用PowerShell比较和哈希文本文件中的不需要的条目。

huangapple go评论62阅读模式
英文:

Compare and hash out unwanted entries in text file using powershell

问题

以下是您要的翻译部分:

$File_1 = gc "\$svr\c$\Services_20062023.txt"
$GetAutomaticServices = Get-Service | where {$_.StartType -eq "Automatic"} | select DisplayName | Format-Table -HideTableHeaders | out-file "\$svr\c$\Automatic_service_list.txt" 
$File_3 = gc "\$svr\c$\Automatic_service_list.txt" 

foreach($entries in $File_1)
{
    $Final_arr += ($entries -split ',')[-3]
}

$file_2 = $Final_arr | ForEach-Object {
    if ($File_3 -match "$_")
    { "$_" }
    else { "#$_" }
}

请注意,这是您提供的PowerShell脚本的翻译部分,仅返回代码。

英文:

I am having below 2 text files with service details in them

File_1

Application Host Helper Service,OS,WARNING
Astro BLSA (Business Logic Server Authentication),OS,CRITICAL
#Base Filtering Engine,OS,WARNING
#Background Tasks Infrastructure Service,OS,WARNING
Cb Defense,OS,WARNING
#Cb Defense WSC,OS,WARNING
Astrogyprocazwatch,OS,WARNING
Astroweberazwatch,OS,WARNING

File_2 (Service List which are having startup type as automatic)

Application Host Helper Service                                  
Astro BLSA (Business Logic Server Authentication)                                                     
Base Filtering Engine                                            
Background Tasks Infrastructure Service
Cb Defense,OS,WARNING   

I want to compare File_2 with File_1. If the entry in File_2 present in File_1 the ignore else put a # in front of the line to comment that.
and if it is already commented then it should not get commented again.

The changes should reflect in File_1 itself

Expected Output

Application Host Helper Service,OS,WARNING
Astro BLSA (Business Logic Server Authentication),OS,CRITICAL
#Base Filtering Engine,OS,WARNING
#Background Tasks Infrastructure Service,OS,WARNING
Cb Defense,OS,WARNING
#Cb Defense WSC,OS,WARNING
#Astrogyprocazwatch,OS,WARNING
#Astroweberazwatch,OS,WARNING

Only last 2 entries in File_1 is not present in File_2, so they got commented and rest which are already present/commented will remain as it is.

I was trying the below code, but it is putting # for all the entries and double # for already hashed entries

$File_1 = gc "\$svr\c$\\Services_20062023.txt"
$GetAutomaticServices = Get-Service | where {$_.StartType -eq "Automatic"} | select DisplayName | Format-Table -HideTableHeaders | out-file "\$svr\c$\\Automatic_service_list.txt" 
$File_3 = gc "\$svr\c$\Automatic_service_list.txt" 

foreach($entries in $File_1)
{
    $Final_arr += ($entries -split ',')[-3]
}

$file_2 = $Final_arr | ForEach-Object {
    if ($File_3 -match "$_")
    { "$_" }
    else { "#$_" }
}

Above updated code is working fine but only problem is which are already commented, this code is adding another # in front of it.

Please let me know how do this

答案1

得分: 2

在您的问题中存在一个问题,因为$File2中包含许多尾随空格。无论如何(使用PowerShell 当输入是一个集合时,操作符返回与表达式右侧值匹配的集合元素。如果集合中没有匹配项,比较运算符返回一个空数组功能):

$File1 = @'
Application Host Helper Service,OS,WARNING
Astro BLSA (Business Logic Server Authentication),OS,CRITICAL
#Base Filtering Engine,OS,WARNING
#Background Tasks Infrastructure Service,OS,WARNING
Cb Defense,OS,WARNING
#Cb Defense WSC,OS,WARNING
Astrogyprocazwatch,OS,WARNING
Astroweberazwatch,OS,WARNING
'@ -Split '\r?\n'

$File2 = @'
Application Host Helper Service
Astro BLSA (Business Logic Server Authentication)
Base Filtering Engine
Background Tasks Infrastructure Service
Cb Defense,OS,WARNING
'@ -Split '\r?\n'

$Split2 = $File2.Foreach{ $_.Trim().Split(',', 2)[0] }
$File1 | Foreach-Object {
    if( $_[0] -ne '#' -and -not ($Split2 -eq $_.Trim().Split(',', 2)[0])) {
        "#$_"
    } else {
        $_
    }
}

请注意,我已经将代码中的中文字符替换为英文字符。

英文:

There is a pitfall in your question as it contains a lot of trailing spaces in $File2.
Anyways (using the PowerShell when the input is a collection, the operator returns the elements of the collection that match the right-hand value of the expression. If there are no matches in the collection, comparison operators return an empty array feature):

$File1 = @'
Application Host Helper Service,OS,WARNING
Astro BLSA (Business Logic Server Authentication),OS,CRITICAL
#Base Filtering Engine,OS,WARNING
#Background Tasks Infrastructure Service,OS,WARNING
Cb Defense,OS,WARNING
#Cb Defense WSC,OS,WARNING
Astrogyprocazwatch,OS,WARNING
Astroweberazwatch,OS,WARNING
'@ -Split '\r?\n'

$File2 = @'
Application Host Helper Service
Astro BLSA (Business Logic Server Authentication)
Base Filtering Engine
Background Tasks Infrastructure Service
Cb Defense,OS,WARNING
'@ -Split '\r?\n'

$Split2 = $File2.Foreach{ $_.Trim().Split(',', 2)[0] }
$File1 | Foreach-Object {
    if( $_[0] -ne '#' -and -not ($Split2 -eq $_.Trim().Split(',', 2)[0])) {
        "#$_"
    } else {
        $_
    }
}

答案2

得分: 0

我建议在ForEach循环内部拆分服务行,这将使打印行变得更容易。

现在,要可靠地匹配多行的DisplayLine,我认为您需要使用正则表达式,并使用锚点(^$)与$File_3中的sls(Select-String)来匹配行。

$File_1 = gc services.txt;
$File_3 = gc automatic-services.txt;

$File_1 | % {
  $pattern = [RegEx]::Escape(($_ -split '',')[0]);
  if ( ($_ -match '^#') -or ($File_3 | sls "(?m)^$pattern$") ) { $_ }
  else { "#$_" }
}

请注意,正则表达式中的(?m)使锚点匹配换行符,有关更多详细信息,请参阅正则表达式快速参考

输出:

Application Host Helper Service,OS,WARNING
Astro BLSA (Business Logic Server Authentication),OS,CRITICAL
#Base Filtering Engine,OS,WARNING
#Background Tasks Infrastructure Service,OS,WARNING
Cb Defense,OS,WARNING
#Cb Defense WSC,OS,WARNING
#Astrogyprocazwatch,OS,WARNING
#Astroweberazwatch,OS,WARNING
英文:

I would suggest splitting the service lines inside the ForEach loop, this will make printing the lines much easier.

Now for matching DisplayLine reliably in a multi-line fashion, I think you need to use regular expressions and match the line with anchors (^ and $) to $File_3 with sls (Select-String).

$File_1 = gc services.txt;
$File_3 = gc automatic-services.txt;

$File_1 | % {
  $pattern = [RegEx]::Escape(($_ -split ',')[0]);
  if ( ($_ -match '^#') -or ($File_3 | sls "(?m)^$pattern$") ) { $_ }
  else { "#$_" }
}

Note the (?m) in the regular expression makes the anchors match on line-breaks, see the regular expression quick referense for more details.

Output:

Application Host Helper Service,OS,WARNING
Astro BLSA (Business Logic Server Authentication),OS,CRITICAL
#Base Filtering Engine,OS,WARNING
#Background Tasks Infrastructure Service,OS,WARNING
Cb Defense,OS,WARNING
#Cb Defense WSC,OS,WARNING
#Astrogyprocazwatch,OS,WARNING
#Astroweberazwatch,OS,WARNING

huangapple
  • 本文由 发表于 2023年7月13日 16:00:57
  • 转载请务必保留本文链接:https://go.coder-hub.com/76677170.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定