Friday, 27 July 2012

Setting multiple site collections to read only in SharePoint using PowerShell

A feature that has been around in SharePoint for a while now is the capability to set site collections and content databases to read only – the former being a property on the site collection in SharePoint and the latter by setting the database directly in SQL Server. This feature hides and disables all the write-based functions on a site, for example, uploading documents, creating and editing list items, etc.

image

It is commonly used in migration scenarios to prevent users from adding content whilst their sites are being moved to a new platform – once the migration has been completed then their site collections can be set back to read/write again.

The easiest, and perhaps most fool-proof way to do this is to set the database as read only in SQL Server, as this will disable the write function on all site collections in one quick change. However, there may be occasions where you may prefer to set it at the site collection level. Typically, this is preferred in scenarios where you wish to enable or disable the read only feature on individual site collections at different times during a migration, perhaps where there is more urgency to control read/write access for specific teams either earlier or later in the migration phase.

The script in this article walks through each Web application (except Central Administration), each content database mounted to the Web application, and finally each site collection in the content database and sets the ReadOnly property to either True (content cannot be created) or False (content can be created). I have also built in an “excluded paths” feature, that allows you to specify individual site collections or entire Web applications from being modified by the script.

First, launch the SharePoint Management Shell (or preferred alternative PowerShell editor) and type the following line to choose whether to set the ReadOnly property to True or False. In the example, I am going to set site collections to read only mode:

# Define whether ReadOnly will be set to true or false
[bool]$readOnly = $true

Next, build the $excludedPaths variable to specify any site collections or Web applications that you wish to exclude from modification by the script. If you would like the script to exclude entire Web applications, then you must add a trailing slash to the URL – e.g., “http://mysite.domain.lab/”. To exclude site collections only, leave off the trailing slash – e.g., “http://mysite.domain.lab”. This functionality allows you to exclude root site collections from being modified but not the entire Web application – for example, you may want to leave the My Site Host site collection read/write but set all the personal sites in the My Site Web application to read only.

Note: Each URL is in quotes and comma-separated:

# Set excluded paths as comma-delimited strings
# Note: Web applications must end in a trailing slash, where site collections do not
[array]$excludedPaths = "http://applications.domain.lab/",
                        "http://mysite.domain.lab",
                        "http://portal.domain.lab/sites/siteA",
                        “http://portal.domain.lab/search”

Now the remainder of the script, which will walk through each site collection in the farm, setting the ReadOnly property as explained earlier:

# Get all Web applications (except Central Admin)
Get-SPWebApplication | ForEach-Object {
   
    if ($excludedPaths -notcontains $_.Url)
    {
        # Enumerate all content databases in each Web application
        if ($_.ContentDatabases -ne $null) {
            $_.ContentDatabases | ForEach-Object {
           
                # Enumerate all site collections in each content database
                if ($_.Sites -ne $null) {
                    $_.Sites | ForEach-Object {
                       
                        # Check if there are sites where the property should not be changed
                        if ($excludedPaths -notcontains $_.Url)
                        {
                            Write-Host "Changing ReadOnly property for site" $_.Url
                           
                            # Set ReadOnly property
                            $_.ReadOnly = $readOnly
                            Write-Host "ReadOnly property for site" $_.Url "set to" $_.ReadOnly
                           
                            # Dispose site collection object
                            $_.Dispose()
                        }
                        else
                        {
                            # Confirm if no changes made on excluded sites
                            Write-Host "No changes made for site" $_.Url
                        }
                    }
                }
            }
        }
    }
    else
    {
        # Confirm if no changes made on excluded web applications
        Write-Host "No changes made for web application" $_.Url
    }
}

The script above will display a list of modified site collections in the PowerShell console as it changes them, and also confirm the Web applications and site collections that were excluded from the process. Should you lose track of which site collections have been set to read only and which ones haven’t, then you can easily generate a status report in the console by using the following script:

# Get all Web applications (except Central Admin)
Get-SPWebApplication | ForEach-Object {
    # Enumerate all content databases in each Web application
    if ($_.ContentDatabases -ne $null) {
        $_.ContentDatabases | ForEach-Object {
            # Enumerate all site collections in each content database
            if ($_.Sites -ne $null) {
                $_.Sites | ForEach-Object {
                    Write-Host "ReadOnly property for site" $_.Url "set to" $_.ReadOnly
                    # Dispose site collection object
                    $_.Dispose()
                }
            }
        }
    }
}

For additional info, the scripts in this article could be used to get or set other site collection properties, too. For example, the Portal Site Connection property, SharePoint Designer permissions, or Site Collection Administrators. See this article on MSDN for a full list of properties available for configuration on site collections, including ReadOnly.

63 comments:

  1. Thanks for a great blog. What about using a printlist.aspx using codebehind that would be installed into sharepoint and keeping the sharepoint layout?

    ReplyDelete
  2. This blog is fantastic. Thank you! Might we post you as one of our favorite resources on our site? Keep up the great work!

    ReplyDelete
  3. It can be almost impossible to find well-qualified users on this matter however you look like you be aware of exactly what you’re covering!
    SharePoint Online Training

    ReplyDelete
  4. Wow amazing i saw the article with execution models you had posted. It was such informative. Really its a wonderful article. Thank you for sharing and please keep update like this type of article because i want to learn more relevant to this topic.


    Java Training

    ReplyDelete
  5. Wow amazing i saw the article with execution models you had posted. It was such informative. Really its a wonderful article. Thank you for sharing and please keep update like this type of article because i want to learn more relevant to this topic.

    Digital Marketing Training

    ReplyDelete
  6. Really an awesome post. I wondered by reading this blog post. Thanks a lot for posting this unique post which you have shared with us. Keep on posting like this exclusive post with us.

    SEO Training in Chennai

    ReplyDelete
  7. Thanks a lot for all your valuable article! We are really happy about the your...
    SEO Training in Chennai

    ReplyDelete
  8. Great blog..Step by step explanation is too good to understand..Its very useful for me to understand..Keep on sharing..
    SEO Training in Chennai

    ReplyDelete
  9. very informative blog. Helps to gain knowledge about new concepts and techniques. Thanks for posting information in this blog
    Java Training in chennai

    ReplyDelete
  10. I like this topic.This site has lots of advantage.I found many interesting things from this site. It helps me in many ways.Thanks for posting this again.


    Dot Net Training in Chennai

    ReplyDelete
  11. This articles has very nice and most of comments can be peas full for me.I really enjoyed the post.

    SEO training in Chennai

    ReplyDelete
  12. Great and really helpful article! Adding to the conversation, providing more information, or expressing a new point of view...Nice information and updates. Really i like it and everyday am visiting your site..

    J2ee Training in Chennai Mylapore

    ReplyDelete
  13. Thanks of sharing this post…Java is the fastest growing language that helps to get your dream job in a best way, so if you wants to become a expertise in Java get some training on that language.
    Regards,
    Core JAVA Training in Chennai | JAVA Training in Chennai

    ReplyDelete
  14. The NFL Flag for Tennessee Titans is viewable from both sides with the opposite side being a reverse image.Fly this flag with any of our tailgate poles or 6' aluminum flagpoles and adjustable flag brackets.
    house divided flags nfl
    house divided flags nhl
    nfl house flags
    nfl flags wholesale, House Divided Flags,Sports Flags 3x5, Sports flags and banners,nfl House Divided Flags

    ReplyDelete
  15. Benvenuti su ordinazione Maglia Sporting CP 2016/2017 ←_← Clicca per acquistare!~~
    Dimensione: S, M, L, XL.
    Materiale di superficie: Tessuto 100% poliestere.
    Utilizzare la squadra: Sporting CP
    Utilizzando l’oggetto: uomomaglia AC milan 2017
    Maglia AS Roma a poco prezzomaglie calcio poco prezzo,
    maglie da calcio a poco prezzo,

    ReplyDelete
  16. I still liked this article, good, good content, and unique design.Thank you for sharing the article.
    deep sea fish oil softgel

    ReplyDelete


  17. In your blog I was happy to see your article, better than last time, and have made great progress, I am very pleased. I am looking forward to your article will become better and better.
    hotmail login | red ball

    ReplyDelete
  18. In this blog article very inspire me and read we get a post for all blog is really good.I like that's our sharing processes.
    Selenium Training in Chennai | Selenium Training Course in Chennai

    ReplyDelete
  19. cara menyembuhkan pilek dengan air garam larutan garam bilas dapat membantu mengatasi hidung tersumbat, dan untungnya lebih cair garam dapat juga menyingkirkan partikel virus dan bakteri dari hidung. Ini adalah salah satu obat alami batuk pilek tapi manjur, di sini adalah bagaimana: campur 1/4 sdt garam dan 1/4 sendok teh baking soda dalam 8 oz (236 ml) air hangat. baca selengkapnya
    obat stroke ringan dengan ramuan herbal Ace Maxs merupakan pengobatan alternatif yang paling tepat karena kandungan alami yang ada didalamnya mampu menangkal radikal bebas penyebab penyumbatan pembuluh darah baik yang menuju ke jantung maupun ke otak. Klik Disini Untuk Membaca
    Gunung sawal yaitu gunung yg jadi icon Kabupaten Ciamis. apalagi salah tunggal kemuncaknya yg( sompak untuk sketsa di atas) dijadikan yang merupakan bahagian latar alamat simbol wilayah Kudat. Tidak seperti Sila tonton sketsa di lembah seandainya tak yakin. baca lagi

    ReplyDelete
  20. I still liked this article, good, good content, and unique design.Thank you for sharing the article.
    pengobatan tradisional tumor mamae

    ReplyDelete
  21. شركة ركن الاندلس من افضل الشركات التى لها باع طويل فى مجال الخدمات المنزلية ولانه افضل شركة متخصصة نقدم لكم اليوم بعض من خدمتنا المميزة مثل شركة تنظيف بالقصيم
    شركة تنظيف خزانات بالقصيم
    شركة تنظيف مجالس بالقصيم
    شركة نقل عفش بالرياض
    شركة تنظيف بالرياض

    ReplyDelete
  22. نجن افضل شركة متخصصة فى جميع الخدمات المنزلية فى الدمام نقدم لك افضل الخدمات من تنظيف ومكافحة حشرات وتسليك مجارى وكشف تسربات ونقل عفش كل ما عليك هو تصفح الموقع من خلال الروابط المذكورة ادن المنشور وتقيمكم عليه نحن سعداء بتعامل معاكم ، شركة مكافحة حشرات بالجبيل
    شركة تنظيف مجالس بالجبيل
    شركة عزل اسطح بالجبيل
    شركة كشف تسربات المياه بالجبيل
    شركة تنظيف بالجبيل
    شركة تسليك مجارى بالدمام
    شركة تنظيف بالدمام
    شركة نقل عفش بالدمام
    شركة مكافحة حشرات بالدمام
    شركة كشف تسربات المياه بالدمام

    ReplyDelete
  23. Thanks your posting. Your topic is very interesting, Please see the best here:

    Bọc ghế da xe innova

    ReplyDelete
  24. Thanks your posting. Your topic is very interesting, Please see the best here:

    http://rambovietnam.vn/tong-hop-mau-boc-ghe-da-xe-innova.html
    Bọc ghế da xe innova

    ReplyDelete
  25. ان شركة قمة الدقة للتنظيف شركة رائدة فى جميع انواع النظافة العامة. والنظافة الان من الامور المهمة والاساسية فى حياة كل شخص ى شركة من اهم شركات التنظيف بالمنطقة وذلك لكونها تمتلك احدث المعدات الخاصة بالتنظيف وتتميز كذلك شركة تنظيف بالجبيل تسعي الي تقديم خدمات تنظيف متميزة لدي عملاء الجبيل باسعار مناسبه وتخفيضات رائعه.
    شركة قمة الدقة للتنظيف افضل شركة تنظيف بالجبيل ونقدم خدمات النظافة بالمنطقة الشرقية وخاصة الجبيل ,ولدينا امكانيات وخبرة 20 عاما في مجال التنظيف شركة تنظيف منازل بالجبيلتقدم خدمات تنظيف عالي الزوق والرقة والدقة والخفة وعمالة مرموقة فلبنية عالية افضل شركة تنظيف بالجبيل و الجبيل الصناعية تقدم كافة خدمات التنظيف للمنازل والشركات والقصور
    شركة قمة الدقة للخدمات المنزلية
    شركة تنظيف بالجبيل هى شركة من أهم شركات التنظيف بالمنطقة وذلك لكونها تمتلك أحدث المعدات الخاصة بالتنظيف وتتميز كذلك شركة تنظيف فلل بالجبيل
    لا احد ينكر ان عمليات التنظيف للمنازل والشقق والفلل والمسابح وغيرها من الاماكن الاخرى يحتاج لمجهود كبير للغايه لذلك نقدم لك عزيزى العميل شركة قمة الدقة للتنظيف هل تبحث عميلنا العزيز عن طرق مميزة لتنظيف منزلك ؟ اليك الان شركة تنظيف شقق بالجبيل وهى مجموعة مميزة من الشركات المميزة التى توفر افضل خدمات التنظيف زيزى العميل تعددت شركات التنظيف بمدينة الجبيل ولكن من بين هولاء الشركات لا يمكنك الا ان

    ReplyDelete
  26. I like your site and content. thanks for sharing the information keep updating, looking forward for more posts. Thanks
    barney | abcya

    ReplyDelete
  27. الكثير من سكان مدينة القصيم والمناطق التابعه لها يحتاج والى شركة متخصصة فى خدمات التنظيف والخدمات المنزلية لرفع العناء الذى يشتكو منه الكثير من سكانة المدينة لذالك تاتى شركة فارس الخليج بالحل السريع وه توفر افضل الخدمات المنزلية وذالك من خلالة عمالة فنية مدربة على اعلى مستوى من الخبرة والكفائة ايضا تمتلك الشركة افضل واحدث المعدات التى تجعل الشركة الافضل والاسرف فى مجال الخدمات المنزلية بالمملكة العربية السعودية شركة رش مبيدات ببريدة
    شركة تنظيف خزانات ببريدة
    شركة تنظيف مكيافات بالقصيم
    شركة تسليك مجارى بالقصيم
    شركة مكافحة حشرات بالقصيم
    شركة عزل بالقصيم
    شركة نقل عفش بالقصيم
    شركة تنظيف بالطائف
    شركة تنظيف خزانات بالطائف
    شركة نقل عفش بالطائف

    ReplyDelete
  28. Nice blog with having good information. It’s very useful for everyone. Thanks and keep posting this type of blog
    CCNA jobs in Hyderabad .

    ReplyDelete
  29. Good one, very informative.. thanks for sharing your views and ideas.. it is very useful to me.. thanks once again

    Java Training in chennai | Java Training institute in chennai

    ReplyDelete
  30. I am very excited to be able to get this very useful information, This is one of the best articles I just found today, I hope you keep updating other information that is still related to this, I'm really looking forward to something new like that, Thank you very much because you have shared this news to us all, best regards

    Cara Alami Menyembuhkan Demam Obat Abses Pada Payudara Cara Mengobati Nyeri Pada Tulang Ekor Obat Sakit Gigi Berlubang Obat Benjolan Di Rahim Obat Ginjal Bengkak Obat Nyeri Punggung Obat Nyeri Lambung Obat Nyeri Kencing Cara Alami Menghilangkan Jerawat Pasir Di Wajah

    ReplyDelete
  31. Good one, very informative.. thanks for sharing your views and ideas.. it is very useful to me.. thanks once again..

    Software Testing Training in chennai

    ReplyDelete
  32. Interesting and informative article.. very useful to me.. thanks for sharing your wonderful ideas.. please keep on updating..

    Android Training in chennai | Dot Net Training in chennai | IOS Training in chennai

    ReplyDelete
  33. LG G7 pouch cases are genuinely handy covers that provide both security and comfort to your device. Check Out LG G7 Features.

    ReplyDelete
  34. Thanks for your information and trick. You are the best

    ReplyDelete