Hello All,
I got a script which helps me in appending the host files on multiple servers but my requirement is bit different and the new entries should append under Networker configuration below, whereas now the entries are getting appended at the bottom of the Host file.
Below is the sample output of host file (for now the new entries are getting appended below #Control-M whereas i want them to be appended under #Networker Configuration.
NetWorker Configuration
#########################
192.168.244.50 pfrxbnw31
192.168.244.121 pfrxbdd41
Control-M
#########################
10.61.199.146 gerctmpas73
10.61.199.143 gerctmpem71
Below is my script how can i modify it so the entries should append right exactly at the bottom of #Networker Configuration
$ErrorActionPreference=“stop”
$Error_log_File=“C:\Temp\InError.txt”
$serverNames=“C:\Temp\serverlist.txt”
$InputFile=“C:\Temp\entries.txt”
$file=“c$\Windows\System32\drivers\etc\hosts”
$backupFileName=“c$\Windows\System32\drivers\etc\hosts_script_backup”
$hostEntries=Get-Content “C:\Temp\entries.txt”
If(Test-Path $Error_log_File)
{
Remove-Item $Error_log_File
}
New-Item $Error_log_File -ItemType file | Out-Null
If(Test-Path $InputFile) {
$servers=Get-content $serverNames
Foreach ($computer in $servers) {
try {
$hostfilePath=Join-Path "\$computer" -ChildPath $file
$backupPath=Join-Path "\$computer" -ChildPath $backupFileName
Copy-Item $hostfilePath -Destination $backupPath
Add-Content $hostFilePath -Value $hostEntries
Write-host “Processed $computer” -ForegroundColor Cyan
}
catch {
write-host “Error processing $computer” -ForegroundColor Red
Write-Output “$computer” | Out-File $Error_log_File -Append
}
}
}
Else {
write-host “InputFile: $InputFile Not Found” -BackgroundColor Red
}