?
Solved

PowerShell script to copy and rename log files

Posted on 2015-01-16
10
Medium Priority
?
410 Views
Last Modified: 2015-01-26
I have a script written for copying our BES-5 log files to a compliance software package. I need help modifying this script to work with the log files BES-10 produces. Currently our logs look like this:

smslog_yyyymmdd.csv
pinlog_yyyymmdd.csv
PhoneCallLog_yyyymmdd.csv

This is the naming convention our compliance software wants it in.... BUT our new log files look like this:

New BES-10 logs:
BES-10_sms_1.0_20150102_0001.csv
BES-10_Phone_1.0_20150102_0001.csv
BES-10_bbmmessage_1.0_20150102_0001.csv

So in short I need this:
BES-10_sms_1.0_20150102_0001.csv ---> smslog_20150104.csv then copied to a queue folder and an archive folder renaming the file to:
BES-10_smslog_20150104.processed

Here is the code that works with our BES-5 environment:

$emailServer = 'email_server_address' #smtp server
$yesterday = (get-date).AddDays(-1)
$yesterdayString = $yesterday.tostring('yyyyMMdd')
$datepath = $yesterday.ToString('yyyy\\MM\\dd')
$pathVal = "Z:\$yesterdayString"
$archivePath = "D:\Scripttest\BBlogs\archive\$datepath"
$smsfile="smslog_$yesterdayString.csv"
$pinfile="pinlog_$yesterdayString.csv"
$phoneFile="PhoneCallLog_$yesterdayString.csv"

if (-not (test-path $archivePath)) {mkdir $archivePath}

foreach ($file in ('smslog','pinlog','PhoneCallLog')) {
    $source = "$pathval\$($file)_$yesterdayString.csv"
    $targets = "D:\Scripttest\BBlogs\queue\$($file)_$yesterdayString.csv","$archivePath\$($file)_$yesterdayString.processed"
    foreach ($target in $targets) {
#write-host "Copying $source to $target"
        copy-item -Path $source -Destination $target -ErrorAction SilentlyContinue
        if (-not (Test-Path $target)) {$failure += 1} else {write-host "Copy successful"}
    }
}

$msg = "SUCCEEDED" ; $priority = 'normal'
if ($failure -gt 1) { $msg = "FAILED" ; $priority = 'high'} 

Send-MailMessage -SmtpServer $emailServer -To alerts@domain.com -From alerts@domain.com  -Subject "BESLOG's Transfer to ZL : $msg " -Body  "BESLOG's Transfer $msg"  -Priority $priority

Open in new window

0
Comment
Question by:USGLOBAL
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 6
  • 4
10 Comments
 
LVL 67

Expert Comment

by:sirbounty
ID: 40554548
BES-10_sms_1.0_20150102_0001.csv will always be
BES-10_sms_1.0_<DATE>_0001.csv??
0
 
LVL 67

Expert Comment

by:sirbounty
ID: 40554555
Think I'm still a bit confused...
Original source file is BES-10_sms_1.0_20150102_0001.csv
You need that copied to the queue folder as smslog_20150104.csv
and then copied to the archive folder as BES-10_smslog_20150104.processed?

How did we get from _sms_ to _smslog_ ?
0
 

Author Comment

by:USGLOBAL
ID: 40554560
one is BES-5 (the original script) and the new file is from BES-10 the new one I need.
0
Ransomware Attacks Keeping You Up at Night?

Will your organization be ransomware's next victim?  The good news is that these attacks are predicable and therefore preventable. Learn more about how you can  stop a ransomware attacks before encryption takes place with our Ransomware Prevention Kit!

 
LVL 67

Expert Comment

by:sirbounty
ID: 40554697
Can you clarify tho?  Are there 3 versions of the same file?
BES-10_sms_1.0_20150102_0001.csv  (original)
smslog_20150104.csv (queued)
BES-10_smslog_20150104.processed (archive)
0
 

Author Comment

by:USGLOBAL
ID: 40559639
Yes that would be correct. the file would change names twice. once to be ingested by the compliance software. (smslog_20150104.csv ) and again in the archive folder (BES-10_smslog_20150104.processed)
0
 

Author Comment

by:USGLOBAL
ID: 40559712
The issue is the compliance software will only ingest a certain format:

for instance the SMS logs need to be smslog_yyyymmdd.csv. They cannot or will not accommodate another naming convention. Blackberry with version 10 changed the format of the log files, they also state that this is not configurable. So since both sets of logs have to have the same name I am force with running 2 scripts staggering the time they run to allow ingestion of the previous server's log files before the next set are put into the queue folder.
0
 
LVL 67

Accepted Solution

by:
sirbounty earned 2000 total points
ID: 40560329
Having a bit more trouble getting my head wrapped around this one, but give this a shot and let me know...

$emailServer = 'email_server_address' #smtp server
$yesterday = (get-date).AddDays(-1)
$yesterdayString = $yesterday.tostring('yyyyMMdd')
$datepath = $yesterday.ToString('yyyy\\MM\\dd')
$pathVal = "z:\$yesterdayString"
$archivePath = "d:\Scripttest\BBlogs\archive\$datepath"
if (-not (test-path $archivePath)) {mkdir $archivePath}

$files = foreach ($file in 'sms','Phone','bbmmessage') {"BES-10_$($file)_1.0_$($yesterdayString)_0001.csv"}

foreach ($file in $files) {
    $source = "$pathVal\$file"
    $targets = "D:\Scripttest\BBlogs\queue\$($file)".Replace("BES-10_","").Replace("_1.0_","_").Replace("_0001",""),"$archivePath\$($file).processed".Replace("_sms_","_smslog_").Replace("_1.0_","_").Replace("_0001.csv","")
    foreach ($target in $targets) {
        copy-item -Path $source -Destination $target -ErrorAction SilentlyContinue
        if (-not (Test-Path $target)) {$failure += 1} else {write-host "Copy successful"}
    }
}

$msg = "SUCCEEDED" ; $priority = 'normal'
if ($failure -gt 1) { $msg = "FAILED" ; $priority = 'high'} 

Send-MailMessa

Open in new window

0
 

Author Comment

by:USGLOBAL
ID: 40560602
Wow that almost worked on the first try! Here is what happened.. the sms file copied to the queue folder was "sms_20150119.csv" it needed to be "smslog_20150119.csv" the archive file created was correct "BES-10_smslog_20150119.processed".
0
 
LVL 67

Expert Comment

by:sirbounty
ID: 40560703
So, z:\20150119\BES-10_sms_1.0_20150119_0001.csv
should be copied as:
d:\Scripttest\BBlogs\queue\smslog_20150119.csv
d:\Scripttest\BBlogs\archive\2015\01\19\BES-10_smslog_20150119.processed

?
0
 
LVL 67

Assisted Solution

by:sirbounty
sirbounty earned 2000 total points
ID: 40560704
$emailServer = 'email_server_address' #smtp server
$yesterday = (get-date).AddDays(-1)
$yesterdayString = $yesterday.tostring('yyyyMMdd')
$datepath = $yesterday.ToString('yyyy\\MM\\dd')
$pathVal = "c:\testfolder\z\$yesterdayString"
$archivePath = "c:\testfolder\d\Scripttest\BBlogs\archive\$datepath"
if (-not (test-path $archivePath)) {mkdir $archivePath}

$files = foreach ($file in 'sms','Phone','bbmmessage') {"BES-10_$($file)_1.0_$($yesterdayString)_0001.csv"}

foreach ($file in $files) {
    $source = "$pathVal\$file"
    $targets = "c:\testfolder\d\Scripttest\BBlogs\queue\$($file)".replace("_sms_","_smslog_").Replace("BES-10_","").Replace("_1.0_","_").Replace("_0001",""),"$archivePath\$($file).processed".Replace("_sms_","_smslog_").Replace("_1.0_","_").Replace("_0001.csv","")
    foreach ($target in $targets) {
        copy-item -Path $source -Destination $target -ErrorAction SilentlyContinue
        if (-not (Test-Path $target)) {$failure += 1} else {write-host "Copy successful"}
    }
}

$msg = "SUCCEEDED" ; $priority = 'normal'
if ($failure -gt 1) { $msg = "FAILED" ; $priority = 'high'} 

Open in new window

0

Featured Post

Problems using Powershell and Active Directory?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Background Information Recently I have fixed file server permission issues for one of my client. The client has 1800 users and one Windows Server 2008 R2 domain joined file server with 12 TB of data, 250+ shared folders and the folder structure i…
In previous parts of this Nano Server deployment series, we learned how to create, deploy and configure Nano Server as a Hyper-V host. In this part, we will look for a clustering option. We will create a Hyper-V cluster of 3 Nano Server host nodes w…
This tutorial will walk an individual through locating and launching the BEUtility application to properly change the service account username and\or password in situation where it may be necessary or where the password has been inadvertently change…
This tutorial will walk an individual through setting the global and backup job media overwrite and protection periods in Backup Exec 2012. Log onto the Backup Exec Central Administration Server. Examine the services. If all or most of them are stop…
Suggested Courses

777 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question