[英]Inconsistent behavior: GCI / -Include
I'm working on an automated cleanup script that uses Get-ChildItem
pretty liberally. 我正在研究一个相当自由地使用
Get-ChildItem
的自动清理脚本。 I filter the contents to clean from an XML configuration with the -Include
flag since it takes a string array while -Filter
does not. 我使用
-Include
标志过滤要从XML配置中清除的内容,因为它采用字符串数组,而-Filter
不使用。 Here's the offending block in question: 这是有问题的块:
Function CleanFolders
{
Write-Log 'Cleaning Folders..' status
ForEach ($Folder in $Config.OS.$Build.Folders.Folder)
{
If (Test-Path $Folder.Name)
{
Try {
GCI $Folder.Name -Include $Folder.Include -Recurse -Force |
Where { ($_.Length -gt ([Int]$Folder.Threshold * 1MB)) -and
($_.LastWriteTime -lt (Get-Date).AddDays(-[Int]$Folder.Expiration)) } |
Sort $_.IsPSContainer -Descending |
% {
Try {
Write-Log "Deleting $($_.FullName)"
Remove-Item "$($_.FullName)" -Force
} Catch {
Write-Log 'Unable to delete item' error
Write-Log "[$($_.Exception.GetType().FullName)] - $($_.Exception.Message)" error
}
}
} Catch { Write-Log "[$($_.Exception.GetType().FullName)] - $($_.Exception.Message)" error }
} Else { Write-Log "$($Folder.Name) does not exist" error }
}
}
The script is run from a scheduled task as SYSTEM and for some reason, is not returning results from the -Include
flag that are not just a wildcard *
. 该脚本是从计划任务中以SYSTEM身份运行的,由于某种原因,它不会从
-Include
标志返回不只是通配符*
。 Are there known issues with -Include
? -Include
是否存在已知问题? This operation runs properly when run as a user account with admin privileges. 以具有管理员特权的用户帐户运行时,此操作可以正常运行。
I found the problem with my script was my method of getting the xml into a variable. 我发现脚本存在的问题是将xml转换为变量的方法。 I was using
Get-Content
which did not preserve any formatting or type information, rather than Import-CliXml
. 我使用的是
Get-Content
,它不保留任何格式或类型信息,而不是Import-CliXml
。 This was causing the Get-ChildItem -Include
arguments to be entered as a single string rather than a string array (since they were comma-separated in the xml). 这导致
Get-ChildItem -Include
参数作为单个字符串而不是字符串数组输入(因为它们在xml中用逗号分隔)。
My resolution around this was creating an object with defined types (such as the expected string array ( String[]
) rather than worrying about creating/exporting/importing an xml file. 我对此的解决方案是创建一个具有定义类型的对象(例如预期的字符串数组(
String[]
),而不用担心创建/导出/导入xml文件。
$global:Config = @{
OS = @{
Workstation = @{
Folders = @( @{ Name=..;Include=..;Expiration=..;Threshold=..;} );
};
};
};
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.