Not able to log deleted files in log file

Ask your Windows 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.
User avatar
Rajeshgvd
Posts: 7
Joined: Wed Jun 20, 2018 12:19 am

Not able to log deleted files in log file

Post by Rajeshgvd » Wed Jun 20, 2018 12:27 am

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

User avatar
jvierra
Posts: 13477
Joined: Tue May 22, 2007 9:57 am
Contact:

Re: Not able to log deleted files in log file

Post by jvierra » Wed Jun 20, 2018 12:47 am

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
Joined: Wed Jun 20, 2018 12:19 am

Re: Not able to log deleted files in log file

Post by Rajeshgvd » Wed Jun 20, 2018 1:46 am

Hi jvierra , thaks for your reply this script is not writing the file name in the logs(which was deleted)

User avatar
jvierra
Posts: 13477
Joined: Tue May 22, 2007 9:57 am
Contact:

Re: Not able to log deleted files in log file

Post by jvierra » Wed Jun 20, 2018 1:53 am

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
Joined: Wed Jun 20, 2018 12:19 am

Re: Not able to log deleted files in log file

Post by Rajeshgvd » Wed Jun 20, 2018 2:00 am

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

User avatar
jvierra
Posts: 13477
Joined: Tue May 22, 2007 9:57 am
Contact:

Re: Not able to log deleted files in log file

Post by jvierra » Wed Jun 20, 2018 2:03 am

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 

User avatar
jvierra
Posts: 13477
Joined: Tue May 22, 2007 9:57 am
Contact:

Re: Not able to log deleted files in log file

Post by jvierra » Wed Jun 20, 2018 2:07 am

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
Joined: Wed Jun 20, 2018 12:19 am

Re: Not able to log deleted files in log file

Post by Rajeshgvd » Wed Jun 20, 2018 4:05 am

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
Joined: Wed Jun 20, 2018 12:19 am

Re: Not able to log deleted files in log file

Post by Rajeshgvd » Tue Jun 26, 2018 4:07 am

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

User avatar
jvierra
Posts: 13477
Joined: Tue May 22, 2007 9:57 am
Contact:

Re: Not able to log deleted files in log file

Post by jvierra » Tue Jun 26, 2018 6:45 am

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

Locked