Not able to log deleted files in log file

Ask your PowerShell-related questions, including questions on cmdlet development!
Forum rules
Do not post any licensing information in this forum.

Any code longer than three lines should be added as code using the 'Select Code' dropdown menu or attached as a file.
This topic is 5 years and 9 months old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked
User avatar
Rajeshgvd
Posts: 7
Last visit: Fri Jun 29, 2018 1:53 am

Not able to log deleted files in log file

Post by Rajeshgvd »

Hi,

When i try to write script on deleting files olderthan 30 days, it should capture log files (deleted files) please do help

Below is the script:

# Change the value $oldTime in order to set a limit for files to be deleted.
$oldTime = 30 # 30 day(s)
Cls

foreach ($path in Get-Content "D:\Suresh\paths.txt")
{


$Logfile = "D:\Files\log.txt"


Function LogWrite
{
Param ([string]$logstring)

Add-content $Logfile -value $logstring
}

# deleting the old files

Get-ChildItem $path -Recurse | WHERE-Object {($_.LastWriteTime -le $(Get-Date).AddDays(-$oldTime))} | Remove-Item -Force -Recurse

$Now = Get-Date
$Days = $oldTime
LogWrite "Trying to delete files older than $Days days, in the folder $path" -ForegroundColor Green
LogWrite "Start delete process at $Now"
$LastWrite = $Now.AddDays(-$Days)
LogWrite "LastWrite $LastWrite"

} out-file D:\Files\log.txt -Append

LogWrite "COMPLETE ALL" -ForegroundColor yellow
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Not able to log deleted files in log file

Post by jvierra »

Start with the following:

Code: Select all

 Function LogWrite {
    Param (
        [string]$logstring,
        $Logfile = 'D:\Files\log.txt'
    )
    Add-content $Logfile -value $logstring
 }

$oldTime = 30 # 30 day(s)
$LastWrite = [datetime]::Today.AddDays(-$oldTime)
LogWrite "Trying to delete files older than $oldTime days, in the folder $path"
LogWrite "Start delete process at $Now"
LogWrite "LastWrite $LastWrite" 
foreach ($path in (Get-Content D:\Suresh\paths.txt)){
    Get-ChildItem $path -Recurse -File| 
        Where-Object {$_.LastWriteTime -le $LastWrite} | 
        Remove-Item -Force
}
LogWrite "COMPLETE ALL" 
Most of what you have posted is just very bad guesswork. Here is a link that will get you up to speed.

Learn PowerShell
PowerShell Documentation
PowerShell Style Guidelines
User avatar
Rajeshgvd
Posts: 7
Last visit: Fri Jun 29, 2018 1:53 am

Re: Not able to log deleted files in log file

Post by Rajeshgvd »

Hi jvierra , thaks for your reply this script is not writing the file name in the logs(which was deleted)
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Not able to log deleted files in log file

Post by jvierra »

You cannot get the filename when the delete is in a pipeline. This is why I posted the links. Start by learning how PowerShell and the pipeline work and how to design and write code that does what you need. Once you learn the basics you will be able to understand the issue and, perhaps, design the code you want.

Another way to delete files and get a complete log is to use RoboCopy which create a detailed log file.
User avatar
Rajeshgvd
Posts: 7
Last visit: Fri Jun 29, 2018 1:53 am

Re: Not able to log deleted files in log file

Post by Rajeshgvd »

Hi jvierra, I am new to power shell and started learning meanwhile can you please tell me how we can write deleted log name by using robocopy. Many thanks
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Not able to log deleted files in log file

Post by jvierra »

Here is one easy way to get a list of files being deleted:

Code: Select all

$paths = Get-Content D:\Suresh\paths.txt
Get-ChildItem $paths -Recurse -File| 
    Where-Object {$_.LastWriteTime -le $LastWrite} | 
    Tee-Object -FilePath deleted.txt |
    Remove-Item -Force -PassThru 
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Not able to log deleted files in log file

Post by jvierra »

RoboCopy is not PowerShell. It is a system utility. As with most system utilities you can get the help for the program by adding "/?" to the command line.

RoboCopy /?
User avatar
Rajeshgvd
Posts: 7
Last visit: Fri Jun 29, 2018 1:53 am

Re: Not able to log deleted files in log file

Post by Rajeshgvd »

I tried the code which you given but my bad, not able to get the result, anyway thanks for your help
User avatar
Rajeshgvd
Posts: 7
Last visit: Fri Jun 29, 2018 1:53 am

Re: Not able to log deleted files in log file

Post by Rajeshgvd »

Hi jvierra,

I need some help on copying the script to remote servers and then execute the same copying script.
I know how to copy files from source to destination, but not able to execute the file script . please help me.
Below is the script.

$computers = get-content "C:\psscripts\servers.txt"

$source = "C:\test\script.ps1"

$destination = "C$\temp\"

foreach ($computer in $computers) {
if ((Test-Path -Path \\$computer\$destination))
{Copy-item $source -Destination "\\$computer\$destination" -Recurse}
else
{
"\\$computer\$destination is not reachable or does not exist"
}
}

Thanks in advance
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Not able to log deleted files in log file

Post by jvierra »

How do you plan on executing the script? We do this in PowerShell using "Invoke-Command". It requires enabling and configuring PS remoting.

help enable-psremoting -online
This topic is 5 years and 9 months old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked