Hi all,
Our current automation tool has some built-in functionality to allow us to ‘cleanup’ our NAS folders. But we now have to move to a different tool that does not have that, so I have to use Powershell. The server has only PS ver 2 at this time. So, I have about 65 locations I monitor thru the automation to cleanup folders and files based on a certain age (how old they are). The first 2 in my test run are to delete entire directories, and the 2nd 2 affect only certain files types.
The first -WhatIf works fine showing exactly what it will delete once -WhatIf is removed.
But on the 2nd set of commands it stumbles with:
“Remove-Item : Cannot bind argument to parameter ‘Path’ because it is null”
The 2 sets of commands are identical with only the actual path being different. I literally did a copy\paste and changed that one literal string. Thus I am assuming I cannot execute multiple sets of commands like this in one script. My assumption is that I must use a different approach to somehow ‘separate’ these commands.
Script:
$HowOld = -30
#Path to the root folder
$Path = “\WDCNAQ513\NAS_Path1\Process_In”
$LastWrite = (Get-Date).AddDays($HowOld).ToString(‘MM/dd/yyyy’)
write-output $Path
Write-Output $LastWrite
#Delete folders older than Threshold Date
Get-ChildItem -Path $Path | Where-Object { $.PSIsContainer -and ($.LastWriteTime -lt $LastWrite)} | Remove-Item -Force -Recurse -WhatIf
$HowOld = -30
#Path to the root folder
$Path = “\WDCNAQ513\NAS_Path2\Process_In”
$LastWrite = (Get-Date).AddDays($HowOld).ToString(‘MM/dd/yyyy’)
write-output $Path
Write-Output $LastWrite
#Delete folders older than Threshold Date
Get-ChildItem -Path $Path | Where-Object { $.PSIsContainer -and ($.LastWriteTime -lt $LastWrite)} | Remove-Item -Force -Recurse -WhatIf
$HowOld = -365
$Path = “\WDCNAQ513\NAS_Path3\In”
$CreateDate = (Get-Date).AddDays($HowOld).ToString(‘MM/dd/yyyy’)
write-output $Path
Write-Output $CreateDate
#Get the files to delete
$filesToDelete = Get-ChildItem -Path $Path | Where-Object {$.Name -like “*.pdf”} | Where {($.creationtime -lt $CreateDate)}
$filesToDelete.Count
$filesToDelete | Remove-Item -Force -WhatIf
$HowOld = -180
$Path = “\WDCNAQ513\NAS_Path3\In”
$CreateDate = (Get-Date).AddDays($HowOld).ToString(‘MM/dd/yyyy’)
write-output $Path
Write-Output $CreateDate
#Get the files to delete
$filesToDelete = Get-ChildItem -Path $Path | Where-Object {$.Name -like “*.log”} | Where {($.creationtime -lt $CreateDate)}
$filesToDelete.Count
$filesToDelete | Remove-Item -Force -WhatIf
Any assistance would be greatly appreciated.