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

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

Compare and hash out unwanted entries in text file using powershell

问题

以下是您要的翻译部分:

  1. $File_1 = gc "\$svr\c$\Services_20062023.txt"
  2. $GetAutomaticServices = Get-Service | where {$_.StartType -eq "Automatic"} | select DisplayName | Format-Table -HideTableHeaders | out-file "\$svr\c$\Automatic_service_list.txt"
  3. $File_3 = gc "\$svr\c$\Automatic_service_list.txt"
  4. foreach($entries in $File_1)
  5. {
  6. $Final_arr += ($entries -split ',')[-3]
  7. }
  8. $file_2 = $Final_arr | ForEach-Object {
  9. if ($File_3 -match "$_")
  10. { "$_" }
  11. else { "#$_" }
  12. }

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

英文:

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

File_1

  1. Application Host Helper Service,OS,WARNING
  2. Astro BLSA (Business Logic Server Authentication),OS,CRITICAL
  3. #Base Filtering Engine,OS,WARNING
  4. #Background Tasks Infrastructure Service,OS,WARNING
  5. Cb Defense,OS,WARNING
  6. #Cb Defense WSC,OS,WARNING
  7. Astrogyprocazwatch,OS,WARNING
  8. Astroweberazwatch,OS,WARNING

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

  1. Application Host Helper Service
  2. Astro BLSA (Business Logic Server Authentication)
  3. Base Filtering Engine
  4. Background Tasks Infrastructure Service
  5. 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

  1. Application Host Helper Service,OS,WARNING
  2. Astro BLSA (Business Logic Server Authentication),OS,CRITICAL
  3. #Base Filtering Engine,OS,WARNING
  4. #Background Tasks Infrastructure Service,OS,WARNING
  5. Cb Defense,OS,WARNING
  6. #Cb Defense WSC,OS,WARNING
  7. #Astrogyprocazwatch,OS,WARNING
  8. #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

  1. $File_1 = gc "\$svr\c$\\Services_20062023.txt"
  2. $GetAutomaticServices = Get-Service | where {$_.StartType -eq "Automatic"} | select DisplayName | Format-Table -HideTableHeaders | out-file "\$svr\c$\\Automatic_service_list.txt"
  3. $File_3 = gc "\$svr\c$\Automatic_service_list.txt"
  4. foreach($entries in $File_1)
  5. {
  6. $Final_arr += ($entries -split ',')[-3]
  7. }
  8. $file_2 = $Final_arr | ForEach-Object {
  9. if ($File_3 -match "$_")
  10. { "$_" }
  11. else { "#$_" }
  12. }

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 当输入是一个集合时,操作符返回与表达式右侧值匹配的集合元素。如果集合中没有匹配项,比较运算符返回一个空数组功能):

  1. $File1 = @'
  2. Application Host Helper Service,OS,WARNING
  3. Astro BLSA (Business Logic Server Authentication),OS,CRITICAL
  4. #Base Filtering Engine,OS,WARNING
  5. #Background Tasks Infrastructure Service,OS,WARNING
  6. Cb Defense,OS,WARNING
  7. #Cb Defense WSC,OS,WARNING
  8. Astrogyprocazwatch,OS,WARNING
  9. Astroweberazwatch,OS,WARNING
  10. '@ -Split '\r?\n'
  11. $File2 = @'
  12. Application Host Helper Service
  13. Astro BLSA (Business Logic Server Authentication)
  14. Base Filtering Engine
  15. Background Tasks Infrastructure Service
  16. Cb Defense,OS,WARNING
  17. '@ -Split '\r?\n'
  18. $Split2 = $File2.Foreach{ $_.Trim().Split(',', 2)[0] }
  19. $File1 | Foreach-Object {
  20. if( $_[0] -ne '#' -and -not ($Split2 -eq $_.Trim().Split(',', 2)[0])) {
  21. "#$_"
  22. } else {
  23. $_
  24. }
  25. }

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

英文:

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):

  1. $File1 = @'
  2. Application Host Helper Service,OS,WARNING
  3. Astro BLSA (Business Logic Server Authentication),OS,CRITICAL
  4. #Base Filtering Engine,OS,WARNING
  5. #Background Tasks Infrastructure Service,OS,WARNING
  6. Cb Defense,OS,WARNING
  7. #Cb Defense WSC,OS,WARNING
  8. Astrogyprocazwatch,OS,WARNING
  9. Astroweberazwatch,OS,WARNING
  10. '@ -Split '\r?\n'
  11. $File2 = @'
  12. Application Host Helper Service
  13. Astro BLSA (Business Logic Server Authentication)
  14. Base Filtering Engine
  15. Background Tasks Infrastructure Service
  16. Cb Defense,OS,WARNING
  17. '@ -Split '\r?\n'
  18. $Split2 = $File2.Foreach{ $_.Trim().Split(',', 2)[0] }
  19. $File1 | Foreach-Object {
  20. if( $_[0] -ne '#' -and -not ($Split2 -eq $_.Trim().Split(',', 2)[0])) {
  21. "#$_"
  22. } else {
  23. $_
  24. }
  25. }

答案2

得分: 0

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

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

  1. $File_1 = gc services.txt;
  2. $File_3 = gc automatic-services.txt;
  3. $File_1 | % {
  4. $pattern = [RegEx]::Escape(($_ -split '',')[0]);
  5. if ( ($_ -match '^#') -or ($File_3 | sls "(?m)^$pattern$") ) { $_ }
  6. else { "#$_" }
  7. }

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

输出:

  1. Application Host Helper Service,OS,WARNING
  2. Astro BLSA (Business Logic Server Authentication),OS,CRITICAL
  3. #Base Filtering Engine,OS,WARNING
  4. #Background Tasks Infrastructure Service,OS,WARNING
  5. Cb Defense,OS,WARNING
  6. #Cb Defense WSC,OS,WARNING
  7. #Astrogyprocazwatch,OS,WARNING
  8. #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).

  1. $File_1 = gc services.txt;
  2. $File_3 = gc automatic-services.txt;
  3. $File_1 | % {
  4. $pattern = [RegEx]::Escape(($_ -split ',')[0]);
  5. if ( ($_ -match '^#') -or ($File_3 | sls "(?m)^$pattern$") ) { $_ }
  6. else { "#$_" }
  7. }

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

Output:

  1. Application Host Helper Service,OS,WARNING
  2. Astro BLSA (Business Logic Server Authentication),OS,CRITICAL
  3. #Base Filtering Engine,OS,WARNING
  4. #Background Tasks Infrastructure Service,OS,WARNING
  5. Cb Defense,OS,WARNING
  6. #Cb Defense WSC,OS,WARNING
  7. #Astrogyprocazwatch,OS,WARNING
  8. #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:

确定