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