質問するのはこれが初めてなので、ご容赦ください。いくつかの基本的なメンテナンス スクリプトを作成して、powershell を独学しています。私の質問は、削除するターゲット ディレクトリとファイルを決定するための引数を受け取る、私が書いているクリーンアップ スクリプトに関するものです。
問題:
このスクリプトは、ファイルの削除を処理するときに検索するファイル拡張子のリストのオプションの引数を受け入れます。実際に削除を実行する前に、ファイルの存在をテストしようとしています。–include パラメーターを指定した test-path を使用して、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 を検索しましたが、この特定の問題の解決策が見つかりませんでした。コードに何か問題があるか、やりたいことを達成するためのより良い方法があると思います。
注:スクリプトの外部で複数のファイル拡張子を使用してテストパスを問題なく実行しようとしたことにも言及する必要があります。
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