Get-ChildItem -File -Exclude 问题
Get-ChildItem -File -Exclude issue
问题
我将 Get-ChildItem
包装在一个通用函数中以添加检查并能够重新使用它。将 -File
与 -Exclude
结合使用时,该函数似乎会中断,即使在其外部对 Get-ChildItem
的相同调用确实按预期工作。
问题
出了什么问题?我该如何解决?
相关
Get-ChildItem Exclude and File parameters don't work together
MWE
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:欢迎对 syntax/best 做法提出建设性的批评。
根据 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
问题
我将 Get-ChildItem
包装在一个通用函数中以添加检查并能够重新使用它。将 -File
与 -Exclude
结合使用时,该函数似乎会中断,即使在其外部对 Get-ChildItem
的相同调用确实按预期工作。
问题
出了什么问题?我该如何解决?
相关
Get-ChildItem Exclude and File parameters don't work together
MWE
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:欢迎对 syntax/best 做法提出建设性的批评。
根据 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