0

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

0

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
}
1
  • Thank you so much Matiur. Incase, if we have subsites means, is there any change on script? – murali kuruva Oct 19 '20 at 7:27
0

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

Use this, it should work for you:

Set-SPSite -Identity $site.Url -LockState "ReadOnly"
2
  • removed quotation, the same issue coming. If run only command it working. But if I want to fetch data from CSV and run command, it's not working. Please share if any alternate code. Our requirement is simple. PowerShell script needs to fetch data from csv files and list of site collections needs to lock as "Read-only" mode. – murali kuruva Oct 16 '20 at 9:25
  • Once a site collection is set to read-only, sub-sites are automatically set to read only. There's no command to separately set sub-sites read-only. – Matiur Rahman Oct 19 '20 at 12:36
0

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"
}
1
  • Thank you Raf, Incase if we have subsites means what we need to do? – murali kuruva Oct 19 '20 at 7:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.