Arrays [Powershell/Sharepoint]是否有人可以查看我的脚本以删除超过7天的Sharepoint列表项?获得';无法索引到空数组';
我编写的脚本会删除每个名为“扫描”的文件夹中超过7天的项目。 它将删除,然后最终出错为“无法索引到空数组” 有人能帮我解决我的剧本有什么问题吗?或者有没有更好的方法Arrays [Powershell/Sharepoint]是否有人可以查看我的脚本以删除超过7天的Sharepoint列表项?获得';无法索引到空数组';,arrays,powershell,sharepoint,scripting,Arrays,Powershell,Sharepoint,Scripting,我编写的脚本会删除每个名为“扫描”的文件夹中超过7天的项目。 它将删除,然后最终出错为“无法索引到空数组” 有人能帮我解决我的剧本有什么问题吗?或者有没有更好的方法 foreach ($site in $opsSite) { Write-Host $site.Title; $oList = $site.Lists["Scans"]; foreach ($list in $Lists) {
foreach ($site in $opsSite)
{
Write-Host $site.Title;
$oList = $site.Lists["Scans"];
foreach ($list in $Lists)
{
$Query = New-Object Microsoft.SharePoint.SPQuery
$Query.Query = "@
<Where>
<Eq>
<FieldRef Name='Created' />
<Value Type='DateTime'>
[Today-7Day(s)]
</Value>
</Eq>
</Where>"
#Get List Items matching the query
$ListItems = $oList.GetItems($Query)
$collListItems = $oList.Items;
$count = $collListItems.Count;
for($intIndex = 0; $intIndex -lt $count; $intIndex++)
{
$listItem = $collListItems[$intIndex];
foreach ($Item in $Items)
{
write-host "Deleting" + $listItem.Name + $listItem["Created"];
$collListItems.Delete($intIndex);
}
}
}
foreach($optsite中的站点)
{
写入主机$site.Title;
$oList=$site.list[“扫描”];
foreach($列表中的列表)
{
$Query=新对象Microsoft.SharePoint.SPQuery
$Query.Query=”@
[今日-7日]
"
#获取与查询匹配的列表项
$ListItems=$oList.GetItems($Query)
$collListItems=$oList.Items;
$count=$collListItems.count;
对于($intIndex=0;$intIndex-lt$count;$intIndex++)
{
$listItem=$collListItems[$intIndex];
foreach($项目中的项目)
{
写入主机“删除”+$listItem.Name+$listItem[“已创建”];
$collListItems.Delete($intIndex);
}
}
}
请不要使用Foreach循环删除。您不能再次修改集合。
试试这个
for($intIndex = 0; $intIndex -lt $count; $intIndex++){
$listItem = $collListItems[$intIndex];
write-host "Deleting" + $listItem.Name + $listItem["Created"];
$collListItems[$intIndex].Delete();// it will delete from recyle bin too,better to use recycle method}
请按如下方式构建您的查询:
<Where>
<Eq>
<FieldRef Name='Created' />
<Value Type='DateTime'>
<Today Offset='-7' />
</Value>
</Eq>
</Where>
您可以尝试使用以下代码删除超过7天的项目:
$Query = New-Object Microsoft.SharePoint.SPQuery
$Query.Query = "@
<Where>
<Eq>
<FieldRef Name='Created' />
<Value Type='DateTime'>
<Today Offset='-7' />
</Value>
</Eq>
</Where>"
#Get List Items matching the query
$ListItems = $oList.GetItems($Query)
foreach($ListItem in $ListItems){
write-host "Deleting" + $listItem.Name + $listItem["Created"];
$ListItem.delete()
}
$Query=新对象Microsoft.SharePoint.SPQuery
$Query.Query=”@
"
#获取与查询匹配的列表项
$ListItems=$oList.GetItems($Query)
foreach($ListItems中的ListItem){
写入主机“删除”+$listItem.Name+$listItem[“已创建”];
$ListItem.delete()
}
更新:
将您的查询更改为:
<Where>
<Lt>
<FieldRef Name='Created' />
<Value Type='DateTime'>
<Today Offset='-7' />
</Value>
</Lt>
</Where>
守则:
$Query = New-Object Microsoft.SharePoint.SPQuery
$Query.Query = "@
<Where>
<Lt>
<FieldRef Name='Created' />
<Value Type='DateTime'>
<Today Offset='-7' />
</Value>
</Lt>
</Where>"
#Get List Items matching the query
$ListItems = $oList.GetItems($Query)
foreach($ListItem in $ListItems){
write-host "Deleting" + $listItem.Name + $listItem["Created"];
$ListItem.delete()
}
$Query=新对象Microsoft.SharePoint.SPQuery
$Query.Query=”@
"
#获取与查询匹配的列表项
$ListItems=$oList.GetItems($Query)
foreach($ListItems中的ListItem){
写入主机“删除”+$listItem.Name+$listItem[“已创建”];
$ListItem.delete()
}
在您的问题中添加以下内容后,请对我进行评论:在每个文件夹中添加一个名为“扫描”的项目示例。如果您的项目是当前文件夹及其子文件夹中的文件,则只需一行即可。让我了解一下您的目录结构。文件从何处开始,从何处结束?是否检查您的查询以及为什么有两个object对于所有项目一个有查询,一个没有查询Hey Michael,我复制了你的代码,它仍然返回7天范围内的项目。顺便说一句,我在sharpoint 2010环境中。我的错误。创建时查询将重新运行项目=今天-7。我已更新了我的答案。请检查它是否有效。谢谢你,Michael!你是最好的。