Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Help with simple script

Status
Not open for further replies.

techseek

MIS
Nov 5, 2010
97
US
Hello
I'm trying to copy a SQL08 .bak file from a dir E:\backups on my app server to my file server S:\P10bu directory (via a scheduled task on W2008R2)
I want to identify only the previous night's backup.
I've fiddled around with lastwritetime/creationtime/addDays(-1) type of lines in my script but can't get it to work for me.
Can someone help me out - PS is new to me

Thanks
 
I don't have a PowerShell solution right off hand but do have a batch file:
Code:
rem Set variable for the date
rem This assumes the batch file is run the same day the backup file is made
for /f "Tokens=1-4 Delims=/ " %%i in ('date /t') do set dt=%%l%%j%%k

rem Set variable for the source
set backfold=G:\MSSQL\MSSQL.1\MSSQL\Backup

rem Use Robocopy to copy the latest databases to BNAN01
robocopy %backfold%\ProdDB \\bnan01\ServerBackups *.bak /MAXAGE:%dt%
If you'll post the PS code you've tried maybe we can figure out why it didn't work
 
Hello
Thx for helping me out
Here it is.
By the way how do I test for my count variable outside the foreach loop (once all files are tested for date and a file has (or has not been copied)?
thanks


#Drive letter "S" is mapped to P10BU share on Lanserv

#clear the remote folder
Remove-Item S:\*

#set count variable

[int]$filescopied=0

foreach ($i in Get-ChildItem E:\Backups )

{

if ($i.LastWriteTime.Date -eq $(Get-Date).AddDays(-1))

{
Copy-Item $i.FullName S:\
$filescopied++
}



if($filescopied -eq 0)
{
"No files have been copied!"
}

Else
{
"good-bye"
break
}

}
 
you dont want the Else{"good bye" break} inside the loop?
otherwise if the first $i file doesnt need copying your foreach loop will get terminated almost straight away

I Hear, I Forget
I See, I Remember
I Do, I Understand

Ronald McDonald
 
I apologize for the delay in responding. A couple of things:
1) It's no problem to check $filescopied outside of the Foreach loop. Just put an If statement outside the loop
2) To compare just the date portion of the time, you also need to get only the date portion of the Get-Date clause. You can do this the same way you did for LastWriteTime.

See if this (only partially tested) code gets you any farther:
Code:
foreach ($i in Get-ChildItem E:\Backups)
{
    if ($i.LastWriteTime.Date -eq $(Get-Date).AddDays(-1).Date)
    {
        Copy-Item $i.FullName S:\
        $filescopied++
    }
} 

if ($filescopied -eq 0)
{
    "No files have been copied!"
}
else
{
    "Files copied: $filescopied"
}

LastWriteTime and Get-Date both return the date plus time down to the second. Adding the .Date qualifier still includes the time, but sets it to 12:00:00 AM. That allows your comparison statement to work against just the date.
 
Hello!
Thank you all
I've been away and I'm just getting back into the swing of things
I will edit my code and test soon
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top