[英]Passing an argument to a powershell script to be used for the Test-Path -include option?
这是我第一次问一个问题,请耐心等待。 我通过编写一些基本的维护脚本来自学Powershell。 我的问题是关于我正在编写的清理脚本,该脚本接受用于确定目标目录和要删除文件的参数。
问题:
该脚本接受用于文件扩展名列表的可选参数,以在处理文件删除时查找。 我试图在实际运行删除之前测试文件是否存在。 我将test-path与–include参数一起使用,以在ValidateScript块中运行检查。 如果我传递单个文件扩展名或不传递任何文件扩展名,它都可以工作,但是当我尝试传递多个文件扩展名时,它将失败。
我已经尝试在脚本中的代码上使用以下变体:
[ValidateScript({ Test-Path $targetDirChk -include $_ })]
[ValidateScript({ Test-Path $targetDirChk -include "$_" })]
[ValidateScript({ Test-Path $targetDirChk -include ‘$_’ })]
对于以上每种可能性,我都使用以下多种扩展名文件列表的变种从命令行运行了脚本:
& G:\batch\DeleteFilesByDate.ps1 30 G:\log *.log,*.ext
& G:\batch\DeleteFilesByDate.ps1 30 G:\log “*.log, *.ext”
& G:\batch\DeleteFilesByDate.ps1 30 G:\log ‘*.log, *.ext’
错误消息的示例:
chkParams : Cannot validate argument on parameter 'includeList'. The " Test-Path $targetDirChk -include "$_" " validation script for the argument with value "*.log, *.ext" did not return true. Determine why the validation script failed and then try the command again.
At G:\batch\DeleteFilesByDate.ps1:81 char:10
+ chkParams <<<< @args
+ CategoryInfo : InvalidData: (:) [chkParams], ParameterBindingValidationException
+ FullyQualifiedErrorId : ParameterArgumentValidationError,chkParams
完整脚本如下。 我尚未添加实际的代码来删除文件,因为我仍在努力接受和验证传入的参数。
我已经搜索了google和stackoverflow,但没有找到解决此特定问题的方法。 我假设我在代码中做错了什么,或者有一种更好的方法来完成我想做的事情。
注意:我应该提到,我也尝试过在脚本外部运行带有多个文件扩展名的test-path,没有问题:
PS G:\batch\powershell> test-path G:\log\* -include *.log
True
PS G:\batch\powershell> test-path G:\log\* -include *.log, *.ext
True
脚本:
# Check that the proper number of arguments have been supplied and if not provide usage statement.
# The first two arguments are required and the third is optional.
if ($args.Length -lt 2 -or $args.Length -gt 3 ){
#Get the name of the script currently executing.
$ScriptName = $MyInvocation.MyCommand.Name
$ScriptInstruction = @"
usage: $ScriptName <Number of Days> <Directory> [File Extensions]
This script deletes files from a given directory based on the file date.
Required Paramaters:
<Number of Days>:
This is an integer representing the number of days worth of files
that should be kept. Anything older than <Number of Days> will be deleted.
<Directory>:
This is the full path to the target folder.
Optional Paramaters:
[File Extensions]
This is the set of file extensions that will be targeted for processing.
If nothing is passed all files will be processed.
"@
write-output $ScriptInstruction
break
}
#Function to validate arguments passed in.
function chkParams()
{
Param(
[Parameter(Mandatory=$true,
HelpMessage="Enter a valid number of days between 1 and 999")]
#Ensure the value passed is between 1 and 999.
#[ValidatePattern({^[1-9][0-9]{0,2}$})]
[ValidateRange(1,999)]
[Int]
$numberOfDays,
[Parameter(Mandatory=$true,
HelpMessage="Enter a valid target directory.")]
#Check that the target directory exists.
[ValidateScript({Test-Path $_ -PathType 'Container'})]
[String]
$targetDirectory,
[Parameter(Mandatory=$false,
HelpMessage="Enter the list of file extensions.")]
#If the parameter is passed, check that files with the passed extension(s) exist.
[ValidateScript({ Test-Path $targetDirChk -include "$_" })]
[String]
$includeList
)
#If no extensions are passed check to see if any files exist in the directory.
if (! $includeList ){
$testResult = Test-path $targetDirChk
if (! $testResult ){
write-output "No files found in $targetDirectory"
exit
}
}
}
#
if ($args[1].EndsWith('\')){
$targetDirChk = $args[1] + '*'
} else {
$targetDirChk = $args[1] + '\*'
}
chkParams @args
-Include
Test-Path
上的-Include
是一个string[]
。 您可能想镜像该定义:
[ValidateScript({ Test-Path $targetDirChk -include $_ })]
[String[]]
$includeList
并在其中放置""
,因为它们将强制参数为字符串,从而尝试匹配看起来像`foo.log blah.ext
的文件。
您还必须在调用函数时在该参数周围加上括号或删除空格。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.