Question

We have 100+ site collections on csv file and we need to mark all sites into "Read Only" mode access based on csv file using powershell script. Anyone suggestion highly appreciate. we am trying below script but facing some issue. SharePoint 2013 environment


Add-PSSnapin Microsoft.SharePoint.PowerShell -erroraction SilentlyContinue
$SitesColl = import-csv "C:\Users\xxxxxxx\test.csv"
foreach($Site in $SitesColl)
{
  Set-SPSite -Identity "$site.Url" -LockState "ReadOnly"
 }

Error :Set-SPSite : Cannot find an SPSite object with Id or Url:
@{Url=http://sharepoint.test.com/sites/xx}.Url.
At C:\Users\xxxxxxx\script.ps1:5 char:3
+   Set-SPSite -Identity "$site.Url" -LockState "ReadOnly"
+   ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidData: (Microsoft.Share...SPCmdletSetSite:
   SPCmdletSetSite) [Set-SPSite], SPCmdletPipeBindException
    + FullyQualifiedErrorId : Microsoft.SharePoint.PowerShell.SPCmdletSetSite

Was it helpful?

Solution

Tested Successfully with the followings:

  1. CSV file Sample:
Url
"http://myServer.com/sites/ABC/"
"http://myServer.com/sites/XYZ/"
"http://myServer.com/sites/VCS/"
"http://myServer.com/sites/BVC/"

  1. PS Script
Cls

Add-PSSnapin Microsoft.SharePoint.PowerShell

$SitesColl = import-csv "C:\Temp\2020\SiteCollections.csv"


# Set Read Only
foreach($Site in $SitesColl)
{
   
   Write-Host "Set Readonly:" $Site.Url
   
   Set-SPSite -Identity $Site.Url -LockState "ReadOnly"   
   #Set-SPSite -Identity $Site.Url -LockState "Unlock" 
}


# Check ReadOnly Status
foreach($Site in $SitesColl)
{
   $Mysite=Get-SPSite -Identity $Site.Url  

   Write-Host "ReadOnly Status:" $MySite.ReadOnly
}

OTHER TIPS

You need to remove the quotation mark around $site.Url.

Use this, it should work for you:

Set-SPSite -Identity $site.Url -LockState "ReadOnly"

You need to capitalize s in $site

Correct code:

Add-PSSnapin Microsoft.SharePoint.PowerShell -erroraction SilentlyContinue
$SitesColl = import-csv "C:\Users\xxxxxxx\test.csv"
foreach($Site in $SitesColl)
{
  Set-SPSite -Identity $Site.Url -LockState "ReadOnly"
}
Licensed under: CC-BY-SA with attribution
Not affiliated with sharepoint.stackexchange
scroll top