Managing unused Sharepoint 2013 site with a PowerShell script

Andy Panyanouvath 116 Reputation points
2020-08-14T14:54:52.89+00:00

Hello,

I need some help please !

I'd like to create a script which would execute the following steps :

  1. Display inactive sites (more than 365 days)
  2. Lock inactive sites if inactive for more than 100 days with command : Set-SPSite -Identity “SITE URL” -LockState “NoAccess”
  3. Delete locked inactive sites after 200 days

I managed to do steps 1 but doesn't have the skills to execute steps 2 and 3.

Here is the script I'm using for step 1 :

I $webApp = Get-SPWebApplication “WEB APPLICATION URL”
$daysInActive = Read-Host “Enter in number of days to check since last modified”
$date = (Get-Date).AddDays(-$daysInActive).ToString(“MM/dd/yyyy”)

Foreach ($web in $webApp | Get-SPSite -Limit All | Get-SPWeb -Limit All)
{

if ($web.LastItemModifiedDate -le $date)
{

Write-Host $web.Url
Write-Host $web.LastItemModifiedDate

$web.Url + ” | ” + “Last Modified Date: ” + $web.LastItemModifiedDate >> LastModified.txt
}

}

Any help wouldbe much appreciated.

Thank you very much

Best regards,

Andy

SharePoint Server Development
SharePoint Server Development
SharePoint Server: A family of Microsoft on-premises document management and storage systems.Development: The process of researching, productizing, and refining new or existing technologies.
1,608 questions
0 comments No comments
{count} votes

Accepted answer
  1. Andy Panyanouvath 116 Reputation points
    2020-08-18T09:14:36.347+00:00

    Hello charles-gerardlemetayer,

    Thank you for your answer.

    I thought that it would be possible to export in a csv the results of step 1 then execute another script by inputing the csv.

    Otherwise thank you for your answers !


4 additional answers

Sort by: Most helpful
  1. Charles Gerard - Le Metayer 151 Reputation points
    2020-08-15T19:53:06.537+00:00

    Hello,

    Regarding "3) Delete locked inactive sites after 200 days", is it 200 days after locked site by the script?

    I'm a bit confuse regarding your goal. Are you trying to lock inactive site collections or inactive sub-sites in site collections?

    I'm not a Sharepoint expert, but as far I can see :

    Regards,

    Charles

    0 comments No comments

  2. Amos Wu-MSFT 4,051 Reputation points
    2020-08-17T08:10:49.21+00:00

    We could traverse web application, site collection, site like this(If the site was modified within 100 days, site collection will not be locked):

    $WebApps = Get-SPWebApplication
    $daysInActive =100;
    $date = (Get-Date).AddDays(-$daysInActive).ToString("MM/dd/yyyy")
    foreach($webApp in $WebApps) {
        foreach ($spSite in $webApp.Sites) {       
           $check=$true;
            foreach($spWeb in $SPsite.AllWebs) {
               if($spWeb.LastItemModifiedDate -ge $date){
        $check=false;
        }            
            }
     if($check){
     Set-SPSite -Identity $spSite -LockState "NoAccess"
     }
        }
    }
    

    The third requirement should be difficult to achieve, there is no attribute value to store the lock time.

    0 comments No comments

  3. Andy Panyanouvath 116 Reputation points
    2020-08-17T08:57:16.087+00:00

    Hello charles-gerardlemetayer, Hello AmosWu-MSFT,

    Thank you for your replies.

    "3) Delete locked inactive sites after 200 days", is it 200 days after locked site by the script?",

    Yes, I would like to delete locked site from step 2, maybe with a second script which input all the locked sites ?

    Thank you very much for your help.

    0 comments No comments

  4. Charles Gerard - Le Metayer 151 Reputation points
    2020-08-17T13:34:19.513+00:00

    Hello,

    As explained by @Amos Wu-MSFT , there're no value about lock time.

    Possible workaround : you can create a NoSQL Database (as a Json file), which could be loaded when script start - or in another process, which contains the website ID, name and lock date.

    Another solution : you can add a tag in the website name as "INACTIVELOCKED-DDMMYYYY-SiteName".
    Then, you will have to split name by "-" symbole, where [0] is the process, [1] the locked date.

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.