Solved

script to copy previous day files

Posted on 2015-01-07
85
155 Views
Last Modified: 2015-01-16
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
0
Comment
Question by:USGLOBAL
  • 44
  • 25
  • 8
  • +1
85 Comments
 
LVL 23

Expert Comment

by:NVIT
ID: 40535740
Need more info. What issues are you getting? Need specifics.
0
 

Author Comment

by:USGLOBAL
ID: 40535753
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.
0
 
LVL 23

Expert Comment

by:NVIT
ID: 40535845
I'm guessing the disabled SLEEP 600 is there to wait until the date changes
0
 

Author Comment

by:USGLOBAL
ID: 40535848
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.
0
 
LVL 23

Expert Comment

by:NVIT
ID: 40535859
When are the logs "complete", so to speak? At 12:00 AM next morning? Before that?
0
 

Author Comment

by:USGLOBAL
ID: 40535863
at 12am the logs roll over to the next day.
0
 
LVL 23

Expert Comment

by:NVIT
ID: 40535871
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?
0
 

Author Comment

by:USGLOBAL
ID: 40535876
Wouldn't the script then look at the date being the following day?
0
 

Author Comment

by:USGLOBAL
ID: 40535880
BTW what is "IOW"?
0
 
LVL 26

Expert Comment

by:pony10us
ID: 40535881
"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
0
 
LVL 67

Expert Comment

by:sirbounty
ID: 40535882
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

0
 
LVL 23

Expert Comment

by:NVIT
ID: 40535892
IOW = in other words.
The year month day variables are set before the SLEEP, just before next day at 12 AM
0
 

Author Comment

by:USGLOBAL
ID: 40535904
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.
0
 
LVL 26

Expert Comment

by:pony10us
ID: 40535906
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?
0
 

Author Comment

by:USGLOBAL
ID: 40535913
pony10us,
At 12am the BES starts a new set of logs in a new folder.
0
 
LVL 23

Expert Comment

by:NVIT
ID: 40535918
SLEEP was a part of the Windows 2003 Resource Kit. Not sure if it's built into server 2008
0
 

Author Comment

by:USGLOBAL
ID: 40535921
Maybe why it was REM'd out?
0
 
LVL 67

Expert Comment

by:sirbounty
ID: 40535930
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?
0
 

Author Comment

by:USGLOBAL
ID: 40535932
I am more familiar with batch files. I am not opposed to PowerShell but will probably have more questions for this thread.
0
 
LVL 67

Expert Comment

by:sirbounty
ID: 40535939
Ok, well as advised above, you would need to use something like blat or get that bmail utility loaded on the new server...
0
 
LVL 67

Expert Comment

by:sirbounty
ID: 40535941
But in my opinion, powershell will allow you a lot more freedom with a lot less coding... ;^)
0
 

Author Comment

by:USGLOBAL
ID: 40535947
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.
0
 
LVL 26

Expert Comment

by:pony10us
ID: 40535965
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  :)
0
 

Author Comment

by:USGLOBAL
ID: 40535985
Lets try the PowerShell script...
0
 
LVL 67

Expert Comment

by:sirbounty
ID: 40535997
Ok, fire away with any questions you might have...
0
 
LVL 23

Expert Comment

by:NVIT
ID: 40536056
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.
0
 

Author Comment

by:USGLOBAL
ID: 40536074
Can it run at 11:59:59 and still process correctly?
0
 
LVL 26

Expert Comment

by:pony10us
ID: 40536083
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)
0
 
LVL 23

Expert Comment

by:NVIT
ID: 40536100
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.
0
 

Author Comment

by:USGLOBAL
ID: 40552144
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?
0
 
LVL 67

Expert Comment

by:sirbounty
ID: 40552238
enclose the actual smtp server between quotes, as an example: (# = a comment)
$emailServer = 'somemailserver@domain.com'
0
 

Author Comment

by:USGLOBAL
ID: 40552245
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!
0
 

Author Comment

by:USGLOBAL
ID: 40552287
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

0
 

Author Comment

by:USGLOBAL
ID: 40552295
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.
0
 

Author Comment

by:USGLOBAL
ID: 40552310
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

0
 
LVL 67

Expert Comment

by:sirbounty
ID: 40552402
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

0
 

Author Comment

by:USGLOBAL
ID: 40552406
Yes but it needs to be for the previous day,
0
 
LVL 67

Expert Comment

by:sirbounty
ID: 40552409
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

0
 
LVL 67

Expert Comment

by:sirbounty
ID: 40552410
Which one needs to be previous?  All of the date related values?
0
 

Author Comment

by:USGLOBAL
ID: 40552411
nothing special about my environment just using mapped drives on a network.
0
 

Author Comment

by:USGLOBAL
ID: 40552418
Yes all the dates should be for the previous day.
0
 
LVL 67

Expert Comment

by:sirbounty
ID: 40552419
Just subtract 1 day from $date...
Change this line:
$date = (get-date).AddDays(-1)
0
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 

Author Comment

by:USGLOBAL
ID: 40552423
I did that and it worked for getting the files but the archive folder it created was for today.
0
 
LVL 67

Expert Comment

by:sirbounty
ID: 40552444
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

0
 

Author Comment

by:USGLOBAL
ID: 40552462
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

0
 

Author Comment

by:USGLOBAL
ID: 40552466
Can this be turned off? will impair the scripts ability to run as a scheduled task?
0
 

Author Comment

by:USGLOBAL
ID: 40552469
what about the ability for the script to email a success or failure email?
0
 
LVL 67

Expert Comment

by:sirbounty
ID: 40552475
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.
0
 

Author Comment

by:USGLOBAL
ID: 40552501
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 !!
0
 
LVL 67

Expert Comment

by:sirbounty
ID: 40552676
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

0
 

Author Comment

by:USGLOBAL
ID: 40553475
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

0
 
LVL 67

Expert Comment

by:sirbounty
ID: 40553556
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

0
 

Author Comment

by:USGLOBAL
ID: 40553574
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

0
 
LVL 67

Expert Comment

by:sirbounty
ID: 40553580
doh!  Sorry, rough day for me :^)

$(file)  should be $($file) (on line 18).  Though I can repost the full script if needed..
0
 

Author Comment

by:USGLOBAL
ID: 40553597
That worked to clear up the errors and the files were all transferred and archived but I still received a failure email.
0
 
LVL 67

Expert Comment

by:sirbounty
ID: 40553722
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
0
 

Author Comment

by:USGLOBAL
ID: 40553740
I am testing it through PowerShell ISE. does that matter?
0
 
LVL 67

Expert Comment

by:sirbounty
ID: 40553746
yeah, it has a habit of keeping the variable value when re-running it.
Just use the clear-variable failure command (no preceeding $)
0
 

Author Comment

by:USGLOBAL
ID: 40553769
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.
0
 

Author Comment

by:USGLOBAL
ID: 40553772
sirbounty,
I am not sure what to do with the code you posted "In the copy block"?
0
 

Author Comment

by:USGLOBAL
ID: 40553776
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

0
 

Author Comment

by:USGLOBAL
ID: 40553781
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

0
 
LVL 67

Expert Comment

by:sirbounty
ID: 40553794
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

0
 

Author Comment

by:USGLOBAL
ID: 40553819
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

0
 

Author Comment

by:USGLOBAL
ID: 40553825
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"
0
 
LVL 67

Expert Comment

by:sirbounty
ID: 40553842
So, it doesn't like the .processed files, it appears?
And they are present?
0
 

Author Comment

by:USGLOBAL
ID: 40553848
Yes, all three files are present in their appropriate locations.
0
 
LVL 26

Expert Comment

by:pony10us
ID: 40553928
sirbounty:  Just curious as to why you are not using the error checking method to verify as used in developedtester's solution shown here: http://www.experts-exchange.com/Programming/Misc/Q_27236709.html?
0
 
LVL 67

Expert Comment

by:sirbounty
ID: 40554076
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...
0
 
LVL 67

Expert Comment

by:sirbounty
ID: 40554095
I see the issue - one moment and I'll have it sorted...
0
 
LVL 67

Accepted Solution

by:
sirbounty earned 500 total points
ID: 40554110
I believe this should work correctly 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\$($file)_$yesterdayString.csv","$archivePath\$($file)_$yesterdayString.processed"
    foreach ($target in $targets) {
        copy-item -Path $source -Destination $target -ErrorAction SilentlyContinue
        if (-not (Test-Path $target)) {$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

0
 

Author Comment

by:USGLOBAL
ID: 40554180
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.
0
 
LVL 67

Assisted Solution

by:sirbounty
sirbounty earned 500 total points
ID: 40554352
Ok, let's put some chatter back in the for loop:

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"}
    }
}

Open in new window

Post the results please.
0
 

Author Comment

by:USGLOBAL
ID: 40554425
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

0
 

Author Comment

by:USGLOBAL
ID: 40554438
Ok my bad.. I didn't clear the variable or close and reopen ISE. Once I did that it worked! Copies and email!
0
 
LVL 26

Expert Comment

by:pony10us
ID: 40554447
Why not initialize the variables at the beginning so you don't have to remember to clear them?
0
 

Author Comment

by:USGLOBAL
ID: 40554452
pony10us,
I'm a novice when it comes to PowerShell... did not know I can do that nor do I know how..
0
 
LVL 67

Expert Comment

by:sirbounty
ID: 40554481
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?
0
 
LVL 26

Expert Comment

by:pony10us
ID: 40554482
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.
0
 
LVL 26

Expert Comment

by:pony10us
ID: 40554485
sirbounty: I mean that in the nicest way also, because I have been following along learning as well.  :)
0
 

Author Comment

by:USGLOBAL
ID: 40554489
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.
0
 

Author Comment

by:USGLOBAL
ID: 40554528
Opened a new topic for the other server log files:

PowerShell script to copy and rename log files
0
 

Author Closing Comment

by:USGLOBAL
ID: 40554531
Thank you sirbounty for helping me through this!
0
 

Author Comment

by:USGLOBAL
ID: 40554536
0
 
LVL 67

Expert Comment

by:sirbounty
ID: 40554543
pony10us, when we stop learning, we might as well give up. ;^)
No offense taken.

Glad I could help USGLOBAL.
0

Featured Post

Why spend so long doing email signature updates?

Do you spend loads of your time carrying out email signature updates? Not very interesting are they? Don’t let signature updates get you down. Let Exclaimer Cloud - Signatures for Office 365 make managing email signatures a breeze.

Join & Write a Comment

Set OWA language and time zone in Exchange for individuals, all users or per database.
This article will help you understand what HashTables are and how to use them in PowerShell.
This tutorial will walk an individual through the process of transferring the five major, necessary Active Directory Roles, commonly referred to as the FSMO roles to another domain controller. Log onto the new domain controller with a user account t…
This tutorial will show how to configure a single USB drive with a separate folder for each day of the week. This will allow each of the backups to be kept separate preventing the previous day’s backup from being overwritten. The USB drive must be s…

706 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now