Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 194
  • Last Modified:

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
0
USGLOBAL
Asked:
USGLOBAL
  • 44
  • 25
  • 8
  • +1
2 Solutions
 
NVITCommented:
Need more info. What issues are you getting? Need specifics.
0
 
USGLOBALAuthor Commented:
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
 
NVITCommented:
I'm guessing the disabled SLEEP 600 is there to wait until the date changes
0
Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

 
USGLOBALAuthor Commented:
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
 
NVITCommented:
When are the logs "complete", so to speak? At 12:00 AM next morning? Before that?
0
 
USGLOBALAuthor Commented:
at 12am the logs roll over to the next day.
0
 
NVITCommented:
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
 
USGLOBALAuthor Commented:
Wouldn't the script then look at the date being the following day?
0
 
USGLOBALAuthor Commented:
BTW what is "IOW"?
0
 
pony10usCommented:
"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
 
sirbountyCommented:
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
 
NVITCommented:
IOW = in other words.
The year month day variables are set before the SLEEP, just before next day at 12 AM
0
 
USGLOBALAuthor Commented:
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
 
pony10usCommented:
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
 
USGLOBALAuthor Commented:
pony10us,
At 12am the BES starts a new set of logs in a new folder.
0
 
NVITCommented:
SLEEP was a part of the Windows 2003 Resource Kit. Not sure if it's built into server 2008
0
 
USGLOBALAuthor Commented:
Maybe why it was REM'd out?
0
 
sirbountyCommented:
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
 
USGLOBALAuthor Commented:
I am more familiar with batch files. I am not opposed to PowerShell but will probably have more questions for this thread.
0
 
sirbountyCommented:
Ok, well as advised above, you would need to use something like blat or get that bmail utility loaded on the new server...
0
 
sirbountyCommented:
But in my opinion, powershell will allow you a lot more freedom with a lot less coding... ;^)
0
 
USGLOBALAuthor Commented:
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
 
pony10usCommented:
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
 
USGLOBALAuthor Commented:
Lets try the PowerShell script...
0
 
sirbountyCommented:
Ok, fire away with any questions you might have...
0
 
NVITCommented:
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
 
USGLOBALAuthor Commented:
Can it run at 11:59:59 and still process correctly?
0
 
pony10usCommented:
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
 
NVITCommented:
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
 
USGLOBALAuthor Commented:
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
 
sirbountyCommented:
enclose the actual smtp server between quotes, as an example: (# = a comment)
$emailServer = 'somemailserver@domain.com'
0
 
USGLOBALAuthor Commented:
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
 
USGLOBALAuthor Commented:
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
 
USGLOBALAuthor Commented:
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
 
USGLOBALAuthor Commented:
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
 
sirbountyCommented:
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
 
USGLOBALAuthor Commented:
Yes but it needs to be for the previous day,
0
 
sirbountyCommented:
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
 
sirbountyCommented:
Which one needs to be previous?  All of the date related values?
0
 
USGLOBALAuthor Commented:
nothing special about my environment just using mapped drives on a network.
0
 
USGLOBALAuthor Commented:
Yes all the dates should be for the previous day.
0
 
sirbountyCommented:
Just subtract 1 day from $date...
Change this line:
$date = (get-date).AddDays(-1)
0
 
USGLOBALAuthor Commented:
I did that and it worked for getting the files but the archive folder it created was for today.
0
 
sirbountyCommented:
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
 
USGLOBALAuthor Commented:
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
 
USGLOBALAuthor Commented:
Can this be turned off? will impair the scripts ability to run as a scheduled task?
0
 
USGLOBALAuthor Commented:
what about the ability for the script to email a success or failure email?
0
 
sirbountyCommented:
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
 
USGLOBALAuthor Commented:
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
 
sirbountyCommented:
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
 
USGLOBALAuthor Commented:
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
 
sirbountyCommented:
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
 
USGLOBALAuthor Commented:
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
 
sirbountyCommented:
doh!  Sorry, rough day for me :^)

$(file)  should be $($file) (on line 18).  Though I can repost the full script if needed..
0
 
USGLOBALAuthor Commented:
That worked to clear up the errors and the files were all transferred and archived but I still received a failure email.
0
 
sirbountyCommented:
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
 
USGLOBALAuthor Commented:
I am testing it through PowerShell ISE. does that matter?
0
 
sirbountyCommented:
yeah, it has a habit of keeping the variable value when re-running it.
Just use the clear-variable failure command (no preceeding $)
0
 
USGLOBALAuthor Commented:
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
 
USGLOBALAuthor Commented:
sirbounty,
I am not sure what to do with the code you posted "In the copy block"?
0
 
USGLOBALAuthor Commented:
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
 
USGLOBALAuthor Commented:
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
 
sirbountyCommented:
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
 
USGLOBALAuthor Commented:
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
 
USGLOBALAuthor Commented:
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
 
sirbountyCommented:
So, it doesn't like the .processed files, it appears?
And they are present?
0
 
USGLOBALAuthor Commented:
Yes, all three files are present in their appropriate locations.
0
 
pony10usCommented:
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
 
sirbountyCommented:
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
 
sirbountyCommented:
I see the issue - one moment and I'll have it sorted...
0
 
sirbountyCommented:
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
 
USGLOBALAuthor Commented:
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
 
sirbountyCommented:
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
 
USGLOBALAuthor Commented:
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
 
USGLOBALAuthor Commented:
Ok my bad.. I didn't clear the variable or close and reopen ISE. Once I did that it worked! Copies and email!
0
 
pony10usCommented:
Why not initialize the variables at the beginning so you don't have to remember to clear them?
0
 
USGLOBALAuthor Commented:
pony10us,
I'm a novice when it comes to PowerShell... did not know I can do that nor do I know how..
0
 
sirbountyCommented:
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
 
pony10usCommented:
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
 
pony10usCommented:
sirbounty: I mean that in the nicest way also, because I have been following along learning as well.  :)
0
 
USGLOBALAuthor Commented:
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
 
USGLOBALAuthor Commented:
Opened a new topic for the other server log files:

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

Glad I could help USGLOBAL.
0

Featured Post

[Webinar] Cloud and Mobile-First Strategy

Maybe you’ve fully adopted the cloud since the beginning. Or maybe you started with on-prem resources but are pursuing a “cloud and mobile first” strategy. Getting to that end state has its challenges. Discover how to build out a 100% cloud and mobile IT strategy in this webinar.

  • 44
  • 25
  • 8
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now