Answered by:
Powershell script does not do anything.

Question
-
I'm running the following powershell in a Administrator: Windows Powershell ISE
$dow = (Get-Date).dayofweek $deldate = (Get-Date).AddDays(-7) Set-Location C: $logbkupdir = "Microsoft.PowerShell.Core\FileSystem::\\servername\backups\sqlinstance\logs\" Get-ChildItem -Path $logbkupdir -Recurse -Include *.bak |Where{$_.CreationTime.Date -eq [datetime]::Today.AddDays(-7)} | Remove-Item -WhatIf
It doesn't return an error when I click Run Script. It just shows the following in the console:
PS D:\> D:\Untitled.ps1 PS C:\Windows\system32>
Even when I remove the -WhatIf, the files are not deleted.
I have also tried adding select-object -first 1 at the end and nothing.
Get-ChildItem -Path $logbkupdir -Recurse -Include *.bak | Where{$_.CreationTime.Date -eq [datetime]::Today.AddDays(-7)} | Select-Object -First 1
What am I doing wrong?
Thank you.
- Edited by Ami2013 Monday, March 5, 2018 4:29 PM
Monday, March 5, 2018 4:29 PM
Answers
-
No - we use "EQ" on the Date to match all files from that day.
Run this at a prompt. The link you posted is an old issue and you are not running batch at the console.
Get-ChildItem -Path \servername\backups\sqlinstance\logs\*.bak -Recurse |
Where{$_.CreationTime.Date -eq [datetime]::Today.AddDays(-7)}Be sure you are targeting the correct date:
[datetime]::Today.AddDays(-7).Date
Is that the date you want?
\_(ツ)_/
- Marked as answer by Ami2013 Monday, March 5, 2018 5:52 PM
Monday, March 5, 2018 5:27 PM
All replies
-
Obviously there are no files for that day.
Also:
$logbkupdir = "Microsoft.PowerShell.Core\FileSystem::\\servername\backups\sqlinstance\logs\"
Is bogus.
$logbkupdir ='\\servername\backups\sqlinstance\logs\*'
\_(ツ)_/
Monday, March 5, 2018 4:41 PM -
There are a lot of files with *.bak files with dates 2/25/2018 and 2/26/2018 in that folder. So, I don't think that is an issue.
When I have $logbkupdir ="\\servername\backups\sqlinstance\logs\"
I get error "Cannot Find Path '\\servername\backups\sqlinstance\logs\' because is does not exist. That is the reason, I added Filesystem:: per discussion here.
Monday, March 5, 2018 5:14 PM -
I think you want less than (-lt) instead of equal (-eq) for the date.
Monday, March 5, 2018 5:23 PM -
No - we use "EQ" on the Date to match all files from that day.
Run this at a prompt. The link you posted is an old issue and you are not running batch at the console.
Get-ChildItem -Path \servername\backups\sqlinstance\logs\*.bak -Recurse |
Where{$_.CreationTime.Date -eq [datetime]::Today.AddDays(-7)}Be sure you are targeting the correct date:
[datetime]::Today.AddDays(-7).Date
Is that the date you want?
\_(ツ)_/
- Marked as answer by Ami2013 Monday, March 5, 2018 5:52 PM
Monday, March 5, 2018 5:27 PM -
Yes, that is the date I want. Last Monday (2/26/2018).
And this seems to work - weird since this seems to just include *.bak to the -Path.
Get-ChildItem -Path \servername\backups\sqlinstance\logs\*.bak -Recurse |
Where{$_.CreationTime.Date -eq [datetime]::Today.AddDays(-7)} | Remove-Item -WhatIf
list out all the filesL
What if: Performaing the operation "Remove File" on target "\\servername\backups\sqlinstance\db1.bak".
What if: Performaing the operation "Remove File" on target "\\servername\backups\sqlinstance\db1.bak".
etc.
Thank you so much.
Next - run it from SQLAgent and make it dynamic.
- Edited by Ami2013 Monday, March 5, 2018 5:52 PM
Monday, March 5, 2018 5:52 PM -
I'm not sure why it doesn't work using the -include. I know if you don't do -recurse, -include won't work without a wildcard in -path.
Monday, March 5, 2018 6:28 PM