Link to home
Start Free TrialLog in
Avatar of USGLOBAL
USGLOBALFlag for United States of America

asked on

script to copy previous day files

I have a BES server that generates log files daily. Each day' s log files are in their own folder i.e. log files for 1/6/2015 will be in a folder named "20150106". Within that folder are 3 particular CSV files I need by name. I have been using the attached script but have to run it before the end of the day (11:59pm) before it will work properly and since moving to Server 2008R2 the email reporting function does not work.

I am looking for a better way to accomplish this.

p.s. I have no VB experience, I would prefer a batch file or PowerShell script.
current-script.txt
Avatar of NVIT
NVIT
Flag of United States of America image

Need more info. What issues are you getting? Need specifics.
Avatar of USGLOBAL

ASKER

Thank you for the reply.
The process in short is copying BlackBerry BES lo files to our compliance package.

1) The log files possibly could be missing data since they are not a full day by one minute. It would be nice to have the log files complete.

2) The email report in my script no longer works and I have to check it daily to see if the process worked.

3) We have introduced another BES server and our compliance solution requires a specific file name for each log. So if I can push the logs from the previous day I can stagger the file copy to accommodate both sets of log files.
I'm guessing the disabled SLEEP 600 is there to wait until the date changes
No to my knowledge. This was a script that was created before my tenure. It is currently REM'd out so it is not in play.
When are the logs "complete", so to speak? At 12:00 AM next morning? Before that?
at 12am the logs roll over to the next day.
If the script is activated at 11:59 PM and SLEEPS until after 12:00 AM next day, or whenever the logs would be complete, that'd give you a complete log. IOW, would enabling SLEEP help?
Wouldn't the script then look at the date being the following day?
BTW what is "IOW"?
"since moving to Server 2008R2 the email reporting function does not work. "

Does the 2008R2 server have the Bmail utility? http://retired.beyondlogic.org/solutions/cmdlinemail/cmdlinemail.htm
The email is failing probably because you're missing 'bmail' on the new server.  Blat (or something similar) would need to be used if doing this from a batch file.

For a strait powershell example, try this.  Could probably be condensed a bit if I knew more about the environment.  For example, are these the only 3 CSV files to copy?
$emailServer = '' #smtp server

$date = get-date
$datepath = "$($date.Year)\$($date.month)\$($date.day)"
$datepath = "$(Get-Date -UFormat %Y\%m\%d)"
$pathVal = "V:\$($date.ToString('yyyyMMdd'))"
$archivePath = "U:\BBlogs\archive\$datepath"
$smsfile="smslog_$($date.tostring('yyyyMMdd')).csv"
$pinfile="pinlog_$($date.tostring('yyyyMMdd')).csv"
$phoneFile="PhoneCallLog_$($date.tostring('yyyyMMdd')).csv"

copy-item -Path .\ -Destination u:\BBlogs\queue -Include $smsfile,$pinfile,$phoneFile

if (!(test-path $archivePath)) {mkdir $archivePath}
copy-item -Path "$archivePath\$smsfile" -Destination "$archivePath\$smsfile.processed"
copy-item -Path "$pathVal\$pinfile" -Destination "$archivePath\$pinfile.processed"
copy-item -Path "$pathVal\$phoneFile" -Destination "$archivePath\$phoneFile.processed"

Send-MailMessage -SmtpServer $emailServer -To alerts@domain.com -From alerts@domain.com -Subject "BESLOG's Transferred to GG1" -Body  "BESLOG's Transferred Successfully" 

Open in new window

IOW = in other words.
The year month day variables are set before the SLEEP, just before next day at 12 AM
sirbounty,
To answer your question yes. 3 CSV files from each server. The original BES v5 server generates the proper log files. The new BES v10 server has to have the current log files renamed to the same naming convention as the BES v5. So once I can figure out how to stagger the file copies I would then need to have the script for the BES v10 server modified to rename the previous days logs to the proper format for ingestion into compliance.

Staggering the copy operation will allow the compliance package to ingest the files and archive them removing them from the queue folder. With the queue folder empty I can copy the files from the BES v10 server using the same naming convention.
When you say the logs roll over at 12:00AM do you mean they start a new log file or they just continue to append or that they clear the existing file and start logging again?
pony10us,
At 12am the BES starts a new set of logs in a new folder.
SLEEP was a part of the Windows 2003 Resource Kit. Not sure if it's built into server 2008
Maybe why it was REM'd out?
I believe sleep was included natively, though I could be wrong.
However there exists a native start-sleep cmdlet in powershell...which route are you wanting to go with this thing?
I am more familiar with batch files. I am not opposed to PowerShell but will probably have more questions for this thread.
Ok, well as advised above, you would need to use something like blat or get that bmail utility loaded on the new server...
But in my opinion, powershell will allow you a lot more freedom with a lot less coding... ;^)
sirbounty,
I would like to attempt the PowerShell method if the thread will allow me some seemingly stupid questions. Starting a flame war is not on my list of things to do today.
I agree with both NewVillageIT in using Sleep however in 2008R2 that command has been changed to Timeout:

Y:\>timeout /?

TIMEOUT [/T] timeout [/NOBREAK]

Description:
    This utility accepts a timeout parameter to wait for the specified
    time period (in seconds) or until any key is pressed. It also
    accepts a parameter to ignore the key press.

Parameter List:
    /T        timeout       Specifies the number of seconds to wait.
                            Valid range is -1 to 99999 seconds.

    /NOBREAK                Ignore key presses and wait specified time.

    /?                      Displays this help message.

NOTE: A timeout value of -1 means to wait indefinitely for a key press.

Examples:
    TIMEOUT /?
    TIMEOUT /T 10
    TIMEOUT /T 300 /NOBREAK
    TIMEOUT /T -1

Y:\>

However I also agree with sirbounty that this project would be easier to address if you used an alternate to Batch such as PS, VBA or ?

There are limitations to manipulating dates in batch that makes the code much more involved.

IMHO  :)
Lets try the PowerShell script...
Ok, fire away with any questions you might have...
Why not try to fix what you have, i..e. replace SLEEP with TIMEOUT? Less work, IMO. If it doesn't work, then try the PS solution. Also make sure it runs at 11:59 pm.
Can it run at 11:59:59 and still process correctly?
The benefits of using PS is that it has the ability to perform math on dates with much less code than Batch does AND it has a built-in ability to send email where Batch requires a call to an external program BMail in this case.

(The above is if you follow sirbounty's method)

The benefits of using Batch in this case is that you already have the program written except for changing:

REM SLEEP 600

to

TIMEOUT /T 600

and installing the BMail program.

No further changes, including running it at 11:59, should be needed.  

(the above is if you follow NewVillageIT's method)
Can it run at 11:59:59 and still process correctly?
I'd run it at 11:59:00 or a bit earlier, e.g. 11:57:00. Using the TIMEOUT /T 600 suggestion by pony gives you 600 seconds or 10 minutes headroom, which should suffice.
sirbounty,
In the PS script example I cannot determine where it will look for the previous days files. Also where do I put the SMTP server address? do I replace "#smtp server" with my server IP?
enclose the actual smtp server between quotes, as an example: (# = a comment)
$emailServer = 'somemailserver@domain.com'
When I edit your code and save it as a PS1 file the run it. I get a brief flash of some red text and an email that the process ran correctly. I checked the destination and the CSV files did not copy to the queue folder but did get archived. Also the files copied were from today and not the previous day.

Thank you in advance!
OK I got this to work but it pulls todays files not yesterdays and it will alert me with a success even though it may have failed.


$emailServer = 'email_server_address' #smtp server

$date = get-date
$datepath = "$($date.Year)\$($date.month)\$($date.day)"
$datepath = "$(Get-Date -UFormat %Y\%m\%d)"
$pathVal = "Z:\$($date.ToString('yyyyMMdd'))"
$archivePath = "D:\Scripttest\BBlogs\archive\$datepath"
$smsfile="smslog_$($date.tostring('yyyyMMdd')).csv"
$pinfile="pinlog_$($date.tostring('yyyyMMdd')).csv"
$phoneFile="PhoneCallLog_$($date.tostring('yyyyMMdd')).csv"

copy-item -Path "$pathVal\$smsfile" -Destination D:\Scripttest\BBlogs\queue
copy-item -Path "$pathVal\$pinfile" -Destination D:\Scripttest\BBlogs\queue
copy-item -Path "$pathVal\$phoneFile" -Destination D:\Scripttest\BBlogs\queue

if (!(test-path $archivePath)) {mkdir $archivePath}
copy-item -Path "$pathVal\$smsfile" -Destination "$archivePath\$smsfile.processed"
copy-item -Path "$pathVal\$pinfile" -Destination "$archivePath\$pinfile.processed"
copy-item -Path "$pathVal\$phoneFile" -Destination "$archivePath\$phoneFile.processed"

Send-MailMessage -SmtpServer $emailServer -To alerts@domain.com -From alerts@domain.com -Subject "BESLOG's Transferred to GG1" -Body  "BESLOG's Transferred Successfully" 

Open in new window

if possible we would need to see if the script will alert us if the 3 files did not make it into the queue folder.
Ok I changed line 3 to the following and got the script to pick up the previous days logs but it did not create and archive directory for that day.

$date = (get-date).AddDays(-1)

Open in new window

Some of that may be difficult for me to ascertain since I'm not within your environment, however running the above, this is the output I get for each.  What is not as you expect it to be?

PS C:\>> $datepath
2015\1\15

PS C:\>> $datepath
2015\01\15

PS C:\>> $pathVal
Z:\20150115

PS C:\>> $archivePath
D:\Scripttest\BBlogs\archive\2015\01\15

PS C:\>> $smsfile
smslog_20150115.csv

PS C:\>> $pinfile
pinlog_20150115.csv

PS C:\>> $phoneFile
PhoneCallLog_20150115.csv

Open in new window

Yes but it needs to be for the previous day,
This would be the result of the other steps - not the valid command, but should give you an idea if something is off...

PS C:\>> "$pathVal\$smsfile -destination D:\Scripttest\BBlogs\queue"
Z:\20150115\smslog_20150115.csv -destination D:\Scripttest\BBlogs\queue

PS C:\>> "$pathVal\$pinfile -Destination D:\Scripttest\BBlogs\queue"
Z:\20150115\pinlog_20150115.csv -Destination D:\Scripttest\BBlogs\queue

PS C:\>> "$pathVal\$phoneFile -Destination D:\Scripttest\BBlogs\queue"
Z:\20150115\PhoneCallLog_20150115.csv -Destination D:\Scripttest\BBlogs\queue


PS C:\>> "$pathVal\$smsfile -Destination $archivePath\$smsfile.processed"
Z:\20150115\smslog_20150115.csv -Destination D:\Scripttest\BBlogs\archive\2015\01\15\smslog_20150115.csv.processed

PS C:\>> "$pathVal\$pinfile -Destination $archivePath\$pinfile.processed"
Z:\20150115\pinlog_20150115.csv -Destination D:\Scripttest\BBlogs\archive\2015\01\15\pinlog_20150115.csv.processed

PS C:\>> "$pathVal\$phoneFile -Destination $archivePath\$phoneFile.processed"
Z:\20150115\PhoneCallLog_20150115.csv -Destination D:\Scripttest\BBlogs\archive\2015\01\15\PhoneCallLog_20150115.csv.processed

Open in new window

Which one needs to be previous?  All of the date related values?
nothing special about my environment just using mapped drives on a network.
Yes all the dates should be for the previous day.
Just subtract 1 day from $date...
Change this line:
$date = (get-date).AddDays(-1)
I did that and it worked for getting the files but the archive folder it created was for today.
Ok, I see - my apologies...
$date is yesterday's date, and it used to construct your date path ($datepath variable), but then it doesn't get passed in again when we format it...

How does this look?
PS C:\>> $datepath
2015\01\14

PS C:\>> $pathVal
Z:\20150114

PS C:\>> $archivePath
D:\Scripttest\BBlogs\archive\2015\01\14


If that's all correct, then this should do it for you:
$emailServer = 'email_server_address' #smtp server

$date = (get-date).AddDays(-1)
$datepath = "$(Get-Date -UFormat %Y\%m\%d $date)"
$pathVal = "Z:\$($date.ToString('yyyyMMdd'))"
$archivePath = "D:\Scripttest\BBlogs\archive\$datepath"
$smsfile="smslog_$($date.tostring('yyyyMMdd')).csv"
$pinfile="pinlog_$($date.tostring('yyyyMMdd')).csv"
$phoneFile="PhoneCallLog_$($date.tostring('yyyyMMdd')).csv"

copy-item -Path "$pathVal\$smsfile" -Destination D:\Scripttest\BBlogs\queue
copy-item -Path "$pathVal\$pinfile" -Destination D:\Scripttest\BBlogs\queue
copy-item -Path "$pathVal\$phoneFile" -Destination D:\Scripttest\BBlogs\queue

if (!(test-path $archivePath)) {mkdir $archivePath}
copy-item -Path "$pathVal\$smsfile" -Destination "$archivePath\$smsfile.processed"
copy-item -Path "$pathVal\$pinfile" -Destination "$archivePath\$pinfile.processed"
copy-item -Path "$pathVal\$phoneFile" -Destination "$archivePath\$phoneFile.processed"

Send-MailMessage -SmtpServer $emailServer -To alerts@domain.com -From alerts@domain.com -Subject "BESLOG's Transferred to GG1" -Body  "BESLOG's Transferred Successfully" 

Open in new window

Ok that worked but I get an output in the PS screen.

 Directory: D:\Scripttest\BBlogs\archive\2015\01


Mode                LastWriteTime     Length Name                                                                                                                                                                      
----                -------------     ------ ----                                                                                                                                                                      
d----         1/15/2015   5:00 PM            14                                                                                                                                                                        

Open in new window

Can this be turned off? will impair the scripts ability to run as a scheduled task?
what about the ability for the script to email a success or failure email?
Typically appending
| out-null
will prevent that from appearing, but I'm not certain which line(s) might be generating that output.
If it's going to be setup as a task, then it's probably irrelevant.
Emailing success should be simple...
What do you determine pass/fail off of?

Stepping out for a bit - back on in about a half hour.
The pass fail would be determined by the CSV files making it to the queue folder or not.

Success email would be all 3 files made it
Failure would be some or all of the files not making it.

For compliance all three files have to be ingested daily, any less is a fail.

once finished I will open another thread about this same process but renaming logs from the other BES server.

My shift is over. I will check back in the morning.

Thank you !!
I tweaked it a bit, adding a loop to process each file, flagging it as a failure if not found.  If the failure bit is true, the mail message will indicate that and send as high priority, otherwise it's sent as normal.  Let me know how this works out for you.

$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',"$archivePath\$file_yesterdayString.processed"
    foreach ($target in $targets) {
        copy-item -Path "$pathval\$file_$yesterdayString.csv"
        if (-not (Test-Path "$target\$file_$yesterdayString.csv")) {$failure += 1}
    }
}

$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 GG1 : $msg " -Body  "BESLOG's Transfer $msg"  -Priority $priority

Open in new window

When I run it I get the following output. But I did get the failure notice! Looks like its not looking for the correct file but it did look in the correct directory..

Copy-Item : Cannot find path 'Z:\20150115\20150115.csv' because it does not exist.
At D:\user\Desktop\BBLogs2ZIP.ps1:17 char:18
+         copy-item <<<<  -Path "$pathval\$file_$yesterdayString.csv"
    + CategoryInfo          : ObjectNotFound: (Z:\20150115\20150115.csv:String) [Copy-Item], ItemNotFoundException
    + FullyQualifiedErrorId : PathNotFound,Microsoft.PowerShell.Commands.CopyItemCommand
 
Copy-Item : Cannot find path 'Z:\20150115\20150115.csv' because it does not exist.
At D:\user\Desktop\BBLogs2ZIP.ps1:17 char:18
+         copy-item <<<<  -Path "$pathval\$file_$yesterdayString.csv"
    + CategoryInfo          : ObjectNotFound: (Z:\20150115\20150115.csv:String) [Copy-Item], ItemNotFoundException
    + FullyQualifiedErrorId : PathNotFound,Microsoft.PowerShell.Commands.CopyItemCommand
 
Copy-Item : Cannot find path 'Z:\20150115\20150115.csv' because it does not exist.
At D:\user\Desktop\BBLogs2ZIP.ps1:17 char:18
+         copy-item <<<<  -Path "$pathval\$file_$yesterdayString.csv"
    + CategoryInfo          : ObjectNotFound: (Z:\20150115\20150115.csv:String) [Copy-Item], ItemNotFoundException
    + FullyQualifiedErrorId : PathNotFound,Microsoft.PowerShell.Commands.CopyItemCommand
 
Copy-Item : Cannot find path 'Z:\20150115\20150115.csv' because it does not exist.
At D:\user\Desktop\BBLogs2ZIP.ps1:17 char:18
+         copy-item <<<<  -Path "$pathval\$file_$yesterdayString.csv"
    + CategoryInfo          : ObjectNotFound: (Z:\20150115\20150115.csv:String) [Copy-Item], ItemNotFoundException
    + FullyQualifiedErrorId : PathNotFound,Microsoft.PowerShell.Commands.CopyItemCommand
 
Copy-Item : Cannot find path 'Z:\20150115\20150115.csv' because it does not exist.
At D:\user\Desktop\BBLogs2ZIP.ps1:17 char:18
+         copy-item <<<<  -Path "$pathval\$file_$yesterdayString.csv"
    + CategoryInfo          : ObjectNotFound: (Z:\20150115\20150115.csv:String) [Copy-Item], ItemNotFoundException
    + FullyQualifiedErrorId : PathNotFound,Microsoft.PowerShell.Commands.CopyItemCommand
 
Copy-Item : Cannot find path 'Z:\20150115\20150115.csv' because it does not exist.
At D:\user\Desktop\BBLogs2ZIP.ps1:17 char:18
+         copy-item <<<<  -Path "$pathval\$file_$yesterdayString.csv"
    + CategoryInfo          : ObjectNotFound: (Z:\20150115\20150115.csv:String) [Copy-Item], ItemNotFoundException
    + FullyQualifiedErrorId : PathNotFound,Microsoft.PowerShell.Commands.CopyItemCommand

Open in new window

Sorry about that - wasn't expanding the variable $file properly...try this:
$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',"$archivePath\$($file)_$yesterdayString.processed"
    foreach ($target in $targets) {
        copy-item -Path "$pathval\$($file)_$yesterdayString.csv" $target
        if (-not (Test-Path "$target\$(file)_$yesterdayString.csv")) {$failure += 1}
    }
}

$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 GG1 : $msg " -Body  "BESLOG's Transfer $msg"  -Priority $priority

Open in new window

Ok that copied and archived the file correctly. but I received a failure notice via email and the script threw the following errors.

The term 'file' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again.
At line:18 char:5
+ file <<<< 
    + CategoryInfo          : ObjectNotFound: (file:String) [], CommandNotFoundException
    + FullyQualifiedErrorId : CommandNotFoundException
 
The term 'file' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again.
At line:18 char:5
+ file <<<< 
    + CategoryInfo          : ObjectNotFound: (file:String) [], CommandNotFoundException
    + FullyQualifiedErrorId : CommandNotFoundException
 
The term 'file' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again.
At line:18 char:5
+ file <<<< 
    + CategoryInfo          : ObjectNotFound: (file:String) [], CommandNotFoundException
    + FullyQualifiedErrorId : CommandNotFoundException
 
The term 'file' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again.
At line:18 char:5
+ file <<<< 
    + CategoryInfo          : ObjectNotFound: (file:String) [], CommandNotFoundException
    + FullyQualifiedErrorId : CommandNotFoundException
 
The term 'file' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again.
At line:18 char:5
+ file <<<< 
    + CategoryInfo          : ObjectNotFound: (file:String) [], CommandNotFoundException
    + FullyQualifiedErrorId : CommandNotFoundException
 
The term 'file' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again.
At line:18 char:5
+ file <<<< 
    + CategoryInfo          : ObjectNotFound: (file:String) [], CommandNotFoundException
    + FullyQualifiedErrorId : CommandNotFoundException

Open in new window

doh!  Sorry, rough day for me :^)

$(file)  should be $($file) (on line 18).  Though I can repost the full script if needed..
That worked to clear up the errors and the files were all transferred and archived but I still received a failure email.
Tougher one to troubleshoot... :(

In the copy block,
write-host "Copying $pathval\$($file)_$yesterdayString.csv to $target"
#does the above output the expected source and targets?
 copy-item -Path "$pathval\$($file)_$yesterdayString.csv" $target
write-host "File exists: " -nonewline
write-host (Test-Path "$target\$(file)_$yesterdayString.csv")
#if the above ever says "False", then that is the 'problem'...and why $failure is incremented...
        if (-not (Test-Path "$target\$(file)_$yesterdayString.csv")) {$failure += 1}
    }

Open in new window

Outside of that, are you running this from a 'new' instance of powershell?  That $failure variable could still have the 'old' value from when it failed before...
Start a new session to be sure or use
Get-variable | remove-variable
or simply
clear-variable failure
I am testing it through PowerShell ISE. does that matter?
yeah, it has a habit of keeping the variable value when re-running it.
Just use the clear-variable failure command (no preceeding $)
Here is what I did. Closed and reopened ISE. Ran this code:

PS L:\> clear-variable failure
Clear-Variable : Cannot find a variable with name 'failure'.
At line:1 char:15
+ clear-variable <<<<  failure
    + CategoryInfo          : ObjectNotFound: (failure:String) [Clear-Variable], ItemNotFoundException
    + FullyQualifiedErrorId : VariableNotFound,Microsoft.PowerShell.Commands.ClearVariableCommand

Open in new window


Ran the script again and same as before the files were copied. received Failure email.
sirbounty,
I am not sure what to do with the code you posted "In the copy block"?
This would be correct.. assuming I did it properly.

PS L:\> write-host "Copying $pathval\$($file)_$yesterdayString.csv to $target"
Copying Z:\20150115\PhoneCallLog_20150115.csv to D:\Scripttest\BBlogs\archive\2015\01\15\PhoneCallLog_20150115.processed

Open in new window

PS L:\> copy-item -Path "$pathval\$($file)_$yesterdayString.csv" $target
write-host "File exists: " -nonewline
write-host (Test-Path "$target\$(file)_$yesterdayString.csv")
File exists: The term 'file' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try a
gain.
At line:3 char:5
+ file <<<< 
    + CategoryInfo          : ObjectNotFound: (file:String) [], CommandNotFoundException
    + FullyQualifiedErrorId : CommandNotFoundException
 
False

Open in new window

Same silly mistake I made earlier - try this version:

$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',"$archivePath\$($file)_$yesterdayString.processed"
    foreach ($target in $targets) {
        write-host "Copying $pathval\$($file)_$yesterdayString.csv to $target"
        #does the above output the expected source and targets?
        copy-item -Path "$pathval\$($file)_$yesterdayString.csv" $target
        write-host "File exists: " -nonewline
        write-host (Test-Path "$target\$($file)_$yesterdayString.csv")
        #if the above ever says "False", then that is the 'problem'...and why $failure is incremented...
        if (-not (Test-Path "$target\$($file)_$yesterdayString.csv")) {$failure += 1}
    }
}

$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 GG1 : $msg " -Body  "BESLOG's Transfer $msg"  -Priority $priority

Open in new window

Same issue, copy process worked incorrect email status. It does however produce the following output. Not in red.

PS L:\> clear-variable failure

PS L:\> D:\user\Desktop\Untitled3.ps1
Copying Z:\20150115\smslog_20150115.csv to D:\Scripttest\BBlogs\queue
File exists: True
Copying Z:\20150115\smslog_20150115.csv to D:\Scripttest\BBlogs\archive\2015\01\15\smslog_20150115.processed
File exists: False
Copying Z:\20150115\pinlog_20150115.csv to D:\Scripttest\BBlogs\queue
File exists: True
Copying Z:\20150115\pinlog_20150115.csv to D:\Scripttest\BBlogs\archive\2015\01\15\pinlog_20150115.processed
File exists: False
Copying Z:\20150115\PhoneCallLog_20150115.csv to D:\Scripttest\BBlogs\queue
File exists: True
Copying Z:\20150115\PhoneCallLog_20150115.csv to D:\Scripttest\BBlogs\archive\2015\01\15\PhoneCallLog_20150115.processed
File exists: False

Open in new window

Copying Z:\20150115\PhoneCallLog_20150115.csv to D:\Scripttest\BBlogs\archive\2015\01\15\PhoneCallLog_20150115.processed
File exists: False

Open in new window


The correct directory structure is there along with the file yet it reported "File exists: False"
So, it doesn't like the .processed files, it appears?
And they are present?
Yes, all three files are present in their appropriate locations.
sirbounty:  Just curious as to why you are not using the error checking method to verify as used in developedtester's solution shown here: https://www.experts-exchange.com/questions/27236709/powershell-copy-item-with-success-or-fail-notification-logs.html?
I don't quite fully understand where it's breaking down at this point.
If the file is actually being copied, then the error would probably not provide anything.
The problem is somehow in the validation that the file is there using test-path.
I'm going to try to setup a similar environment and perform some better testing...
I see the issue - one moment and I'll have it sorted...
ASKER CERTIFIED SOLUTION
Avatar of sirbounty
sirbounty
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
No joy.. the files were not copied this time and I received the failed email, in this case was true.. There was no "Red Text" output.
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
No errors, no file copies, failure email


PS C:\Windows\system32> D:\user\Desktop\BBLogs2ZL.ps1
Copying Z:\20150115\smslog_20150115.csv to D:\Scripttest\BBlogs\queue\smslog_20150115.csv
Copying Z:\20150115\smslog_20150115.csv to D:\Scripttest\BBlogs\archive\2015\01\15\smslog_20150115.processed
Copying Z:\20150115\pinlog_20150115.csv to D:\Scripttest\BBlogs\queue\pinlog_20150115.csv
Copying Z:\20150115\pinlog_20150115.csv to D:\Scripttest\BBlogs\archive\2015\01\15\pinlog_20150115.processed
Copying Z:\20150115\PhoneCallLog_20150115.csv to D:\Scripttest\BBlogs\queue\PhoneCallLog_20150115.csv
Copying Z:\20150115\PhoneCallLog_20150115.csv to D:\Scripttest\BBlogs\archive\2015\01\15\PhoneCallLog_20150115.processed

Open in new window

Ok my bad.. I didn't clear the variable or close and reopen ISE. Once I did that it worked! Copies and email!
Why not initialize the variables at the beginning so you don't have to remember to clear them?
pony10us,
I'm a novice when it comes to PowerShell... did not know I can do that nor do I know how..
Well, if it's ultimately a scheduled task, it's really not necessary.
I realize it's SOP for many coders, but most of my scripts are automated are just run once - those that aren't I'll ensure the variables are cleared after I've used them...

So everything is working now, or need some finishing touches?
I am really just prodding sirbounty along with some suggestions.  

Basically though:

      Set-StrictMode -Version 1

      $failure = 0

Add any other variables at that need to be initialized.
sirbounty: I mean that in the nicest way also, because I have been following along learning as well.  :)
This cures my BES-5 log file issue. I have another issue with my BES-10 logs that will require some renaming of the current logs to a format our compliance software can ingest. But I believe that is for another thread correct? The original question on this topic was answered.
Opened a new topic for the other server log files:

PowerShell script to copy and rename log files
Thank you sirbounty for helping me through this!
pony10us, when we stop learning, we might as well give up. ;^)
No offense taken.

Glad I could help USGLOBAL.