這是我第一次問一個問題,所以我承擔。我通過編寫一些基本的維護腳本來教授自己的PowerShell。我的問題是關於我寫的清理腳本,它接受參數來確定要刪除的目標目錄和文件。將參數傳遞給PowerShell腳本將用於測試的路徑選擇-include?
問題:
腳本接受的文件擴展名列表的可選參數看處理文件的刪除時。我試圖在實際運行刪除之前測試文件的存在。我使用帶-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
完整的腳本如下。我還沒有添加實際的代碼來刪除文件,因爲我仍在努力接受和驗證傳入的參數。
我已經搜索谷歌和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
謝謝你的迴應喬伊。如果我的理解正確,我只需要爲我的字符串定義添加括號並刪除$ _變量的引號。我這樣做了,但仍然出現錯誤。 – nydba
chkParams:無法驗證的參數「includeList」的說法。 「測試路徑$ targetDirChk -include $ _」驗證 腳本與價值參數「* .LOG,* .EXT」沒有返回true。確定驗證腳本失敗的原因,然後再次嘗試該命令。 在G:\ batch \ DeleteFilesByDate。PS1:81字符:10個 + chkParams <<<< @args + CategoryInfo:InvalidData:(:) [chkParams],ParameterBindingValidationException + FullyQualifiedErrorId:ParameterArgumentValidationError,chkParams – nydba
如果值包括是:「* .LOG,*。分機「比它無法工作。只有第一次調用是有效的,仍然 - 不明白爲什麼依賴$ args而不是添加param()與整個腳本的強制參數?另外:如果文件不存在,將不會刪除任何東西,所以我認爲驗證是不必要的,Test-Path for folder應該足夠IMO。 – BartekB