Merge contents of file from different server in single file

Table of Contents

  1. Introduction
  2. Scenario
  3. Benefits
  4. Pre-requisites
  5. Script
  6. Script Input
  7. Script Output
  8. Script sample email output
  9. End of Document

Merge contents of file from different server in single file

1.     Introduction

Purpose of this document is to explain how you can capture contents of any file from different servers in single file.

This PowerShell script is created on request of one of my subscriber. Hope you will enjoy. If you have any query or concern please contact me at nirajsrivastava1@gmail.com

2.     Scenario

Below are the scenarios.

3.     Benefits

You don’t need to login to each of the server and check log file. Also save your time. If you have exchange server you may also configure to have an email.

4.     Pre-requisites

User require (usually a service account) with password never expires and have sufficient privileges to read the contents from domain joined systems.

SMTP server should be reachable if you want automated email via Task Scheduler.

5.     Script

Script is written in PowerShell. Script should be executed from system via Task Scheduler using service account.

## Owner : Niraj Srivastava
## Date : 04-Apr-2020
## Version : 1.0


## This Script is used to capture contents of text file from different server and put in single CSV file 
## To work script properly
## Create Proper Folder Structure "C:\NSITAutomation\Scripts","C:\NSITAutomation\Scripts\INPUT", "C:\NSITAutomation\OUTPUT\CombinedFile"
## Copy this script in Folder "C:\NSITAutomation\Scripts"
## Change Source Path and file in line no 30 as per your requirment
## Execute this script from Domain joined system with have sufficient permission on all the servers
## You may schedule the Script to execute every day via Task Scheduler.
## Create input file "C:\NSITAutomation\Scripts\INPUT\Servers.txt"
## Add servers name in input file, one server_name each line.
## You can check Log at "C:\NSITAutomation\OUTPUT".
## SMTP Server should be accessible from This system to send email

cls
$dt= (Get-Date)
$tm= (Get-Date).ToString("hh:mm")
$fldt= (Get-Date).ToString("ddMMMyyyy")

Write "Report Start :$fldt Time $tm "

$inputpath ="C:\NSITAutomation\Scripts\INPUT\" 
$inputfile ="$inputpath\Servers.txt"
$outpath ="C:\NSITAutomation\OUTPUT\CombinedFile\$fldt"
$outputlog="$outpath\FileContents_$fldt.log"
$outputcsv = "$outpath\FileContents_$fldt.csv"
$sourcefile ="C$\TEST\TESTFile.txt"
$str = "*" * 130
write $str

Write "Input Path :$inputpath"
Write "Input File :$inputpath"
write $str

mkdir "$outpath" -ErrorAction SilentlyContinue
write "Reporte Date :$fldt,Time :$tm" | Out-File $outputlog 

$servers= Get-Content $inputfile

Foreach ($server in $servers)

{

$filepath = "\\$server\$sourcefile"

#Write "Working for server :$server"
#write "Checking file      :$filepath"
#write ""

if (Test-Path $filepath ) 
    {
    write "Status :OK, file exist on server $server"
    $filelines= Get-Content $filepath
        Foreach ($fileline in $filelines)
        {
        #write "$server,$fileline"
        write "$server,$fileline" | Out-File $outputlog -Append
        }

    } 
else 
    {
    write "Status :NOK, unable to connect $server or file does not exist"
    write "$server,Unable to connect server or file not found $filepath" | Out-File $outputlog -Append
    #write ""
    }

}
write $str
Write "Output Path :$outpath"
write "Output File :$outputcsv"
write $str

#Get-Content $outputlog | Export-Csv $outputcsv
import-csv $outputlog -delimiter "`," | export-csv $outputcsv

## Email Section

$emailfrom = 'niraj.srivasatava@nsitautomation.in'
$emailto = 'niraj.srivasatava@nsitautomation.in'
$emailSMTPServer ="EXCHANGE_SERVER_NAME"
$emailsubject = "Status"
$emailbody = "<b><font color=orange>Dear team,</b></font> <br>" 
$emailbody += '<P>Please see attachment.</P>'
$emailbody += "<P>Thank you,<br><b><font color=black>NSIT Automation</P></br></b>"

Send-MailMessage -From $emailfrom -To $emailto -SmtpServer $emailSMTPServer -Subject $emailsubject -Body $emailbody -BodyAsHtml -Attachments $outputlog,$outputcsv

6.     Script Input

Input file should be test file and contain one server per line.

7.     Script Output

Script will provide you two output files (FileContents_Date.log and FileContents_Date.csv).

FileContents_Date.log file output

FileContents_Date.csv file output

8.     Script sample email output

Below is the sample email.

9.     End of Document

Facebook Comments

Leave a Reply

Your email address will not be published. Required fields are marked *