我已经看了几天这个剧本了。解决了大多数错误但似乎无法捕获导致此错误的原因。该脚本是:
. $Env:JobDir\Scripts\DeleteMethods1.ps1
#Purge old logs
Remove-FilesCreatedBeforeDate -Path "$Env:JobDir\Logs\" -DateTime ((Get- Date).AddDays(-30))
#Backup log from last run
if(Test-Path $Env:JobDir\log_*.txt)
{
Move-Item -force $Env:JobDir\log_*.txt $Env:JobDir\Logs\
}
$logFile = $Env:JobDir + "\log_$(Get-Date -Format ""yyyyMMdd-HHmm"").txt"
echo "Removing files older than $Env:Days days" >> $logFile
if($Env:CheckType -eq "Created")
{
# Remove all files created more than 30 days ago
# Optionally use -DeletePathIfEmpty to remove empty directories
Remove-FilesCreatedBeforeDate -Path $Env:PathToCleanUp -DateTime ((Get- Date).AddDays($Env:Days)) -DeletePathIfEmpty "$Env:DeletePathIfEmpty" >> $logFile
}
if($Env:CheckType -eq "Modified")
{
# Delete all files that have not been updated in 30 days
Remove-FilesNotModifiedAfterDate -Path $Env:PathToCleanUp -DateTime ((Get- Date).AddDays($Env:Days)) "$Env:DeletePathIfEmpty" >> $logFile
}
if($Env:CheckType -eq "Accessed")
{
# Delete all files that have not been accessed in 30 days
Remove-FilesNotAccessedAfterDate -Path $Env:PathToCleanUp -DateTime ((Get-Date).AddDays($Env:Days)) "$Env:DeletePathIfEmpty" >> $logFile
}
echo "Done removing files older than $Env:Days days" >> $logFile
cat "$Env:JobDir\log.txt" >> $logFile
该脚本从以下文件中提取函数:
function Remove-EmptyDirectories([parameter(Mandatory=$true)][ValidateScript({Test-Path $_})][string] $Path, [switch] $DeletePathIfEmpty,
[DateTime] $OnlyDeleteDirectoriesCreatedBeforeDate = [DateTime]::MaxValue,
[DateTime] $OnlyDeleteDirectoriesNotModifiedAfterDate = [DateTime]::MaxValue,
[DateTime] $OnlyDeleteDirectoriesNotAccessedAfterDate = [DateTime]::MaxValue)
{
Get-ChildItem -Path $Path -Recurse -Force -Directory | Where-Object { (Get- ChildItem -Path $_.FullName -Recurse -Force -File) -eq $null } |
Where-Object { $_.CreationTime -lt $OnlyDeleteDirectoriesCreatedBeforeDate -and $_.LastWriteTime -lt $OnlyDeleteDirectoriesNotModifiedAfterDate
-and $_.LastAccessedTime -lt $OnlyDeleteDirectoriesNotAccessedAfterDate } |
Remove-Item -Force -Recurse
# If we should delete the given path when it is empty, and it is a directory, and it is empty, and it meets the date requirements, then delete it.
if ($DeletePathIfEmpty -and (Test-Path -Path $Path -PathType Container) -and (Get-ChildItem -Path $Path -Force) -eq $null -and
((Get-Item $Path).CreationTime -lt $OnlyDeleteDirectoriesCreatedBeforeDate) -and
((Get-Item $Path).LastWriteTime -lt $OnlyDeleteDirectoriesNotModifiedAfterDate) -and
((Get-Item $Path).LastWriteTime -lt $OnlyDeleteDirectoriesNotAccessedAfterDate))
{ Remove-Item -Path $Path -Force }
}
# Function to remove all files in the given Path that were created before the given date, as well as any empty directories that may be left behind.
function Remove-FilesCreatedBeforeDate([parameter(Mandatory)][ValidateScript({Test-Path $_})][string] $Path,[parameter(Mandatory)][DateTime] $DateTime,[switch] $DeletePathIfEmpty)
{
Get-ChildItem -Path $Path -Recurse -Force -File | Where-Object { $_.CreationTime -lt $DateTime } | Remove-Item -Force
Remove-EmptyDirectories -Path $Path -DeletePathIfEmpty $DeletePathIfEmpty -OnlyDeleteDirectoriesCreatedBeforeDate $DateTime -OnlyDeleteDirectoriesNotModifiedAfterDate $DateTime -OnlyDeleteDirectoriesNotAccessedAfterDate $DateTime
}
# Function to remove all files in the given Path that have not been modified after the given date, as well as any empty directories that may be left behind.
function Remove-FilesNotModifiedAfterDate([parameter(Mandatory)][ValidateScript({Test-Path $_})][string] $Path, [parameter(Mandatory)][DateTime] $OnlyDeleteDirectoriesNotModifiedAfterDate, [switch] $DeletePathIfEmpty)
{
Get-ChildItem -Path $Path -Recurse -Force -File | Where-Object { $_.LastWriteTime -lt $OnlyDeleteDirectoriesNotModifiedAfterDate } | Remove-Item -Force
Remove-EmptyDirectories -Path $Path -OnlyDeleteDirectoriesNotModifiedAfterDate $OnlyDeleteDirectoriesNotModifiedAfterDate -DeletePathIfEmpty $DeletePathIfEmpty
}
# Function to remove all files in the given Path that were created before the given date, as well as any empty directories that may be left behind.
function Remove-FilesNotAccessedAfterDate([parameter(Mandatory)][ValidateScript({Test-Path $_})][string] $Path, [parameter(Mandatory)][DateTime] $OnlyDeleteDirectoriesNotAccessedAfterDate, [switch] $DeletePathIfEmpty)
{
Get-ChildItem -Path $Path -Recurse -Force -File | Where-Object { $_.LastAccessTime -lt $OnlyDeleteDirectoriesNotAccessedAfterDate } | Remove-Item -Force
Remove-EmptyDirectories -Path $Path -OnlyDeleteDirectoriesNotAccessedAfterDate $OnlyDeleteDirectoriesNotAccessedAfterDate -DeletePathIfEmpty $DeletePathIfEmpty
}
现在,此脚本的对象是爬行目录并删除旧项目。这里的很多变量来自环保产品。现在当我运行这个时,我得到一个错误,指出:
Remove-FilesCreatedBeforeDate : A positional parameter cannot be found that
accepts argument ''.
+ Remove-FilesCreatedBeforeDate -Path $Env:PathToCleanUp -DateTime
((Get-Date) ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
~~~
+ CategoryInfo : InvalidArgument: (:) [Remove-FilesCreatedBeforeD
ate], ParameterBindingException
+ FullyQualifiedErrorId : PositionalParameterNotFound,Remove-FilesCreatedB
eforeDate
我认为我一直在看这个太久了,我看不出错误,所以如果它是简单的善意。任何建议都会很棒。在此先感谢!!!
*注意我不是编写剧本的人 - 一位同事让我纠错。
答案 0 :(得分:0)
它必须是"$Env:DeletePathIfEmpty"
的问题,因为这是您在这些语句中使用的唯一位置参数。基于你称之为函数的其他地方,我猜你需要改变这个:
"$Env:DeletePathIfEmpty"
到此:
-DeletePathIfEmpty "$Env:DeletePathIfEmpty"
看起来需要对“已修改”和“已访问”的检查类型进行更改。
根据您的评论指出-DeletePathIfEmpty
参数是[switch]
,看起来您需要调出-DeletePathIfEmpty
开关或将函数修改为{{1键入并传递[bool]
或$true
。