USGLOBAL
asked on
PowerShell script to copy and rename log files
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_00 01.csv
BES-10_Phone_1.0_20150102_ 0001.csv
BES-10_bbmmessage_1.0_2015 0102_0001. csv
So in short I need this:
BES-10_sms_1.0_20150102_00 01.csv ---> smslog_20150104.csv then copied to a queue folder and an archive folder renaming the file to:
BES-10_smslog_20150104.pro cessed
Here is the code that works with our BES-5 environment:
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_00
BES-10_Phone_1.0_20150102_
BES-10_bbmmessage_1.0_2015
So in short I need this:
BES-10_sms_1.0_20150102_00
BES-10_smslog_20150104.pro
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
Think I'm still a bit confused...
Original source file is BES-10_sms_1.0_20150102_00 01.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.pro cessed?
How did we get from _sms_ to _smslog_ ?
Original source file is BES-10_sms_1.0_20150102_00
You need that copied to the queue folder as smslog_20150104.csv
and then copied to the archive folder as BES-10_smslog_20150104.pro
How did we get from _sms_ to _smslog_ ?
ASKER
one is BES-5 (the original script) and the new file is from BES-10 the new one I need.
Can you clarify tho? Are there 3 versions of the same file?
BES-10_sms_1.0_20150102_00 01.csv (original)
smslog_20150104.csv (queued)
BES-10_smslog_20150104.pro cessed (archive)
BES-10_sms_1.0_20150102_00
smslog_20150104.csv (queued)
BES-10_smslog_20150104.pro
ASKER
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.pr ocessed)
ASKER
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.
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.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
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.pr ocessed".
So, z:\20150119\BES-10_sms_1.0 _20150119_ 0001.csv
should be copied as:
d:\Scripttest\BBlogs\queue \smslog_20 150119.csv
d:\Scripttest\BBlogs\archi ve\2015\01 \19\BES-10 _smslog_20 150119.pro cessed
?
should be copied as:
d:\Scripttest\BBlogs\queue
d:\Scripttest\BBlogs\archi
?
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
BES-10_sms_1.0_<DATE>_0001