我正在使用此代码删除超过 30 天的文件
Function Remove_FilesCreatedBeforeDate {
$Path = "\\servername\path"
$Date = (Get-Date).AddDays(-30)
$ValidPath = Test-Path $Path -IsValid
If ($ValidPath -eq $True) {
"Path is OK and Cleanup is now running"
Get-ChildItem -Path $path -Recurse | Where-Object { $_.LastWriteTime -lt $Date } | Remove-Item -Recurse -force -Verbose
}
Else {
"Path is not a ValidPath"
}
}
Remove_FilesCreatedBeforeDate
现在我想记录哪些文件被删除,以及是否有错误或路径无效。有人可以帮我吗?
//编辑
我现在正在使用此代码(感谢 Efie 的帮助)
[Cmdletbinding()]
param(
[Parameter()]$LogPath = 'C:\Admin\scripts\Clean_Folder\Log\log.txt',
[Parameter(ValueFromPipeline)]$Message
)
process {
$timeStampedMessage = "[$(Get-Date -Format 's')] $Message"
$timeStampedMessage | Out-File -FilePath $LogPath -Append
}
}
Function Remove-FilesCreatedBeforeDate {
[Cmdletbinding()]
param(
[Parameter()]$Path = '\\servername\path\',
[Parameter()]$Date = $(Get-Date).AddDays(-30)
)
process {
if(-not (Test-Path $Path -IsValid)) {
"Path $Path was invalid" | Write-MyLog
return
}
"Path $Path is OK and Cleanup is now running" | Write-MyLog
try {
Get-ChildItem -Path $Path -Recurse |
Where-Object {
$_.LastWriteTime -lt $Date
} | Remove-Item -recurse -force -verbose | Write-MyLog
}
catch {
"Remove-Item failed with message $($_.Exception.Message)" | Write-MyLog
}
}
}
Write-MyLog
Remove-FilesCreatedBeforeDate
两个文件被删除,但我只在我的日志中看到这个
[2021-07-22T16:27:53] Path \\servername\path\ is OK and Cleanup is now running
我没有看到哪些文件被删除了
答案 0 :(得分:0)
Start-transcript
和 Try and catch
可以成为您的解决方案吗?
Start-Transcript 记录您所做的一切以及错误。
我试过了,这符合你的要求
Start-Transcript -Path "$PSScriptRoot\RemoveAccountLog.txt" -Force -Append
Get-Date -Format "yyyy-mm-dd HH:MM"
Try
{ # Start Try
$Path = "\\servername\path"
$Date = (Get-Date).AddDays(-30)
$TestPath = Test-Path -Path $Path -PathType Container
If ( $TestPath -Eq $Null )
{ # Start If
Write-Host "The $TestPath String is empty, Path is not a valid"
} # End If
Else
{ # Start Else
Write-host "Path is OK and Cleanup is now running... 0%"
$GetFiles = Get-ChildItem -Path $Path -Recurse -Force |
Where-Object { $_.LastWriteTime -lt $Date } |
Remove-Item -Recurse -force -Verbose |
Write-host "Path is OK and Cleanup is now running... 100%" -ForegroundColor Green
} # End Else
} # End Try
Catch
{ # Start Catch
Write-Warning -Message "## ERROR## "
Write-Warning -Message "## Script could not start ## "
Write-Warning $Error[0]
} # End Catch
答案 1 :(得分:0)
您的示例的简单实现如下所示:
Function Remove-FilesCreatedBeforeDate {
[Cmdletbinding()]
param(
[Parameter(Mandatory)]$Path = '\some\default\path',
[Parameter()]$Date = $(Get-Date).AddDays(-30)
)
process {
if(-not (Test-Path $Path -IsValid)) {
"Path $Path was invalid" | Write-MyLog
return
}
"Path $Path is OK and Cleanup is now running" | Write-MyLog
try {
Get-ChildItem -Path $Path -Recurse |
Where-Object {
$_.LastWriteTime -lt $Date
} | Remove-Item -Recurse -Force -Verbose
}
catch {
"Remove-Item failed with message $($_.Exception.Message)" | Write-MyLog
}
}
}
function Write-MyLog {
[Cmdletbinding()]
param(
[Parameter()]$LogPath = 'default\log\path\log.txt',
[Parameter(ValueFromPipeline)]$Message
)
process {
$timeStampedMessage = "[$(Get-Date -Format 's')] $Message"
$timeStampedMessage | Out-File -FilePath $LogPath -Append
}
}
process { }
、[Cmdletbinding()]
和 [Parameter()]
是将您的函数变成“高级”函数的原因。您可以通过这种方式使用通常为已编译 cmdlet 保留的大量内置功能。</p>
例如,您现在可以使用 $ErrorActionPreference = 'SilentlyContinue'
抑制错误,就像您过去使用原生 Powershell cmdlet 所做的那样。
您可以通过将 ValueFromPipelin
添加到您的参数来将您的消息传送到您的日志记录功能。
那些实际上只是刷了您获得的额外功能的表面。
这里有一些信息。如果您打算将来使用它们,我建议您养成这样编写它们的习惯。
我建议您查看 Microsoft 提供的有关错误处理的文档:
我还建议您查看有关 PowerShell 函数命名约定的内容:
根据 PowerShell 标准,将函数命名为 Remove-FilesCreatedBeforeDate
更有意义,用破折号分隔动词-动作而不是下划线。
如果您想要更多的控制和更多的功能来记录您的函数,这里有一些关于使用 PSFramework 的 PowerShell 久经考验的真实解决方案的信息:
https://adamtheautomator.com/powershell-logging/
祝你好运!希望其中一些有所帮助。