none
Script to delete items older than 7 days getting cannot index into a null array RRS feed

  • Question

  • Can someone help me with my script?

    It will delete items then give me a cannot index into a null array

    Try {

        foreach ($site in $opsSite)

        {

            Write-Host $site.Title;

            $oList = $site.Lists["Scans"];

           

            foreach ($list in $Lists)

                {

                    Write-Host "List is :" + $oList.Title + " with item count " + $oList.ItemCount;

                    $date = get-date;

                    $sevenDays = $date.AddDays(-7);

                    $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);

                                    }

                                   

                                }

     

                }

        }

    }

    Catch

    {

        $ErrorMessage = $_.Exception.Message

        $FailedItem = $_.Exception.ItemName

        write-host "Error message " $ErrorMessage

       

    }


    Saturday, November 30, 2019 2:24 AM

All replies