我将Get-ChildItem
包装在一个通用函数中,以添加支票并能够重新使用它。将-File
与-Exclude
结合使用时,即使外部对Get-ChildItem
的相同调用确实按预期工作,该函数似乎也会中断。
出了什么问题?我该如何解决?
Get-ChildItem Exclude and File parameters don't work together
function Get-Object {
[CmdletBinding ()]
param (
[Parameter (
Position = 1,
Mandatory = $true,
HelpMessage = "Path to the object"
)]
[String]
$Path,
[Parameter (
Position = 2,
Mandatory = $false,
HelpMessage = "Type of object"
)]
[ValidateSet ("All", "File", "Folder")]
[String]
$Type = "All",
[Parameter (
Position = 3,
Mandatory = $false,
HelpMessage = "Filter to apply"
)]
[String]
$Filter = "*",
[Parameter (
Position = 4,
Mandatory = $false,
HelpMessage = "Pattern to exclude"
)]
[String]
$Exclude = $null
)
begin {
if (-Not (Test-Path -Path $Path)) {
Write-Host "$Path does not exists."
exit 1
}
$ObjectType = [ordered]@{
"All" = "items"
"File" = "files"
"Folder" = "folders"
}
}
process {
# Get files
switch ($Type) {
"File" {
$Files = Get-ChildItem -Path $Path -Filter $Filter -Exclude $Exclude -File
}
"Folder" {
$Files = Get-ChildItem -Path $Path -Filter $Filter -Exclude $Exclude -Directory
}
default {
$Files = Get-ChildItem -Path $Path -Filter $Filter -Exclude $Exclude
}
}
# If no files are found, print hints
if ($Files.Count -eq 0) {
if ($Filter -ne "*") {
Write-Host "No $($ObjectType[$Type]) were found in $Path matching the filter ""$Filter""."
} elseif ($Exclude) {
Write-Host "No $($ObjectType[$Type]) corresponding to the criterias were found in $Path."
} else {
Write-Host "No $($ObjectType[$Type]) were found in $Path."
}
# exit 1
} else {
return $Files
}
}
}
$Path = Split-Path -Path $MyInvocation.MyCommand.Definition
$RelativePath = "\test"
$AbsolutePath = Join-Path -Path $Path -ChildPath $RelativePath
$Filter = "*"
$Exclude = $null
Get-ChildItem -Path $RelativePath -Filter $Filter -Exclude $Exclude -File
Get-ChildItem -Path $AbsolutePath -Filter $Filter -Exclude $Exclude -File
Get-Object -Path $RelativePath -Filter $Filter -Exclude $Exclude -Type "File"
Get-Object -Path $AbsolutePath -Filter $Filter -Exclude $Exclude -Type "File"
PS C:\> .\test.ps1
Directory: C:\test
Mode LastWriteTime Length Name
---- ------------- ------ ----
-a---- 21/09/2018 18:42 0 text.txt
-a---- 21/09/2018 18:42 0 text.txt
No files were found in \test.
No files were found in C:\test.
PS:欢迎对语法/最佳做法提出任何建设性的批评。
答案 0 :(得分:0)
根据PowerShell GIT存储库上的this issue,这似乎是一个已知问题。
同时使用-Filter
,-Exclude
和-File
时似乎存在冲突。通过删除一个或添加-Recurse
,该函数将按预期工作。
Get-ChildItem -Path $Path -Filter $Filter -File
Get-ChildItem -Path $Path -Filter $Filter -Exclude $Exclude -File -Recurse
显然,这不是最佳选择,在解决此问题之前,应将其视为解决方法。
function Get-Object {
[CmdletBinding ()]
param (
[Parameter (
Position = 1,
Mandatory = $true,
HelpMessage = "Path to the items"
)]
[String]
$Path,
[Parameter (
Position = 2,
Mandatory = $false,
HelpMessage = "Type of object"
)]
[ValidateSet ("All", "File", "Folder")]
[String]
$Type = "All",
[Parameter (
Position = 3,
Mandatory = $false,
HelpMessage = "Filter to apply"
)]
[String]
$Filter = "*",
[Parameter (
Position = 4,
Mandatory = $false,
HelpMessage = "Pattern to exclude"
)]
[String]
$Exclude = $null
)
begin {
if (-Not (Test-Path -Path $Path)) {
Write-Host "$Path does not exists."
exit 1
}
$ObjectType = [ordered]@{
"All" = "items"
"File" = "files"
"Folder" = "folders"
}
}
process {
# Get files
switch ($Type) {
"File" {
$Files = Get-ChildItem -Path $Path -Filter $Filter -Exclude $Exclude -File -Recurse
}
"Folder" {
$Files = Get-ChildItem -Path $Path -Filter $Filter -Exclude $Exclude -Directory
}
default {
$Files = Get-ChildItem -Path $Path -Filter $Filter -Exclude $Exclude
}
}
# If no files are found, print hints
if ($Files.Count -eq 0) {
if ($Filter -ne "*") {
Write-Host "No $($ObjectType[$Type]) were found in $Path matching the filter ""$Filter""."
} elseif ($Exclude) {
Write-Host "No $($ObjectType[$Type]) corresponding to the criterias were found in $Path."
} else {
Write-Host "No $($ObjectType[$Type]) were found in $Path."
}
# exit 1
} else {
return $Files
}
}
}
$Path = Split-Path -Path $MyInvocation.MyCommand.Definition
$RelativePath = "\test"
$AbsolutePath = Join-Path -Path $Path -ChildPath $RelativePath
$Filter = "*"
$Exclude = $null
Get-ChildItem -Path $RelativePath -Filter $Filter -Exclude $Exclude -File
Get-ChildItem -Path $AbsolutePath -Filter $Filter -Exclude $Exclude -File
Get-Object -Path $RelativePath -Filter $Filter -Exclude $Exclude -Type "File"
Get-Object -Path $AbsolutePath -Filter $Filter -Exclude $Exclude -Type "File"
PS C:\> .\test.ps1
Directory: C:\test
Mode LastWriteTime Length Name
---- ------------- ------ ----
-a---- 21/09/2018 18:42 0 text.txt
-a---- 21/09/2018 18:42 0 text.txt
-a---- 21/09/2018 18:42 0 text.txt
-a---- 21/09/2018 18:42 0 text.txt