Script to copy specified IIS logs from multiple Servers

Summary

Need to copy a set of IIS logs from multiple servers for data analysis? Are you doing it manually?  If so, please check out this script as it will help expedite the process.

The Script:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
#Modify Inputs
 
# Host and Drive to store the IISLogs
$Hostdir = "\\HOSTSERVER\c$" 
 
# Actual Location of the IIS Logs on the Server
$iislogsfolder = "c$\inetpub\logs\LogFiles\W3SVC1892304237"
 
#A wild card parameter to determine a file range
$thefiles = "*ex1906*"
 
 
# Create a target folder on host if does not exist
 $TARGETROOT = "$Hostdir\logs"
 if(!(Test-Path -Path  $TARGETROOT)){
 New-Item -ItemType directory -Path  $TARGETROOT
 }
 
 
# Create an export folder if it does not exist
$target = "$Hostdir\logs\export"
 if(!(Test-Path -Path  $target)){
 New-Item -ItemType directory -Path $target
 }
 
 
#Simple Server list
$servers = Get-Content C:\servers.txt
 
# For loop to do the work
foreach ($server in $servers)
{
 
#make a new folder by server name if it does not exist
 $TARGETDIR = "$target\$Server"
 if(!(Test-Path -Path  $TARGETDIR)){
 New-Item -ItemType directory -Path $TARGETDIR
 }
 
#Get the files
$iislogLogDir = "\\$server\$iislogsfolder"
$iislogName = Get-ChildItem "$iislogLogDir" -Recurse -Include "$thefiles" | Get-Item
 
#Start a loop to copy all the files to the host locatiion
foreach ($log in $iislogName) {
copy-Item -path $log $TARGETDIR
}
 
}

What the Script Does

You only need to modify the inputs and create a server list. Then the script will do the following:

  1.  Use a list of servers to collect the data from.
  2.  Create a “logs” folder on the specified drive.
  3.  Create an “export” sub folder under “logs”.
  4.  Create a sub folder under “export” using  server name of each server.
  5.  Use the wild card file name you specified to target specific files.
  6.  Copy each targeted file to the correct serer name folder.

Notes:

  •  If you want to collet 1 file from each server, just specify use the full date name in the search input (ie 190606).
  •  You can run this more than once if another file is needed and they will be added to the existing folders.
  •  The “export” folder will contain all the files needed. Just compress the folder and you’re ready to share.

Leave a Reply