16 April 2016

How do I use Powershell with IIS 7 or later?

If you're into any form of automation, you know that Powershell rocks!
That said, the real strength of Powershell comes from it's snap-ins.  These extensions allow you to do all manner of things.  One of those is the ability to script actions in IIS.  To get started you'll need to ensure you have the Feature installed on your flavor of Windows.  If you don't have it installed, you can either download it here:

https://www.microsoft.com/en-us/download/details.aspx?id=7436

or add it via Windows' Programs and Features.  Generally on anything late Windows 7 or Server 2008, it's already loaded.

Once installed, you'll need to load the snap-in thus:

Import-Module WebAdministration
From here, you're only limited by the available calls and your imaginations.

Happy scripting.
C

10 March 2016

What is the SharePoint hive?

Almost a decade ago I posted an article explaining what the 60/12 hive is.  Since then, SharePoint has undergone many new versions so I thought it was time I updated my hive article just a bit to include the three additional major releases since then.  For more information on the origination of the "hive", refer to the original article linked above.

SharePoint Version
Hive Server Disk Location
2007C:\Program Files\Common Files\Microsoft Shared\web server extensions\12
2010C:\Program Files\Common Files\Microsoft Shared\web server extensions\14
2013C:\Program Files\Common Files\Microsoft Shared\web server extensions\15
2016C:\Program Files\Common Files\Microsoft Shared\web server extensions\16

Enjoy
C

22 February 2016

How do I - Get a list of email addresses of all users who belong to a given SharePoint security group?

One of mentees asked me how to extract the emails of all the owners of a given site.  With some Powershell magic and the use of the -ExpandProperty switch, this is actually quite easy.

$url = "http://{webapp}/sites/{site}";
$owners = Get-SPWeb $url | Select -ExpandProperty SiteGroups | Where {$_.Name.ToLower() -match ((Get-SPWeb $url).Title.ToLower() + " owners")} | Select -ExpandProperty Users | Select Email;
foreach ($owner in $owners)
{
   {do something with $owner.Email}
}

In the above script, we start off by setting the $url variable.  Then we make the big call to get the owners list of emails into $owners.
The command calls Get-SPWeb for the given URL.  The return of that is then piped to a Select command using the -ExpandProperty switch to expand the SiteGroups.  This would get you all site groups, but piping it to the Where command allows us to filter the returned groups down to the one we want.
The Where command is fed the name of the site group (in lower case) and is asked to match it against the spweb object's title (in lower case) with " owners" concatenated.  A couple of things to note:

  1. It is good practice to ALWAYS make the case of text the same on both sides of a text based comparison.  I prefer lower case, hence the dispersion of .ToLower() in my code.
  2. This part of the code does depend on default security groups being used in SharePoint.  This is 95% of the time the case, but you should be aware that different naming conventions may have been used for the name of the site owners group.
Now that we have the owners security group, the result is piped to another Select command expanding to return the users of that group.  Finally, we pipe to a Select statement that selects a single field i.e. the email address.

You now have an array of email addresses that can be iterated and used via the .Email field reference.

Enjoy
C

18 January 2016

How to load a dot net class in Powershell when you don't know the DLL name.

Most of us know that the main SharePoint classes live in Microsoft.SharePoint.dll.  The easiest way to load this assembly is by using the ::LoadWithPartialName method thus releasing you from having to use the exact DLL name.  In this case its obvious, but there are a couple of classes that are in DLLs that are not so obvious.  Simply call the following:

[System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint");

You can replace the "Microsoft.SharePoint" with any class name, provided its accessible on the system.

Enjoy
C

10 December 2015

How do I - Allow someone the ability to TRUNCATE TABLE in SQL Server without granting them DBO rights?

There comes a time where a developer produces a stored procedure (sproc) that tries to clear a table quickly. In the case of permanent tables, this is usually done using the TRUNCATE TABLE statement in TSQL. Of course in a production setting, you will have limited rights on your SQL Server and databases. As such, we generally grant users DB_DATAREADER and DB_DATAWRITER rights. Additionally, we'll grant users EXECUTE rights on sprocs. The problem is that in order to do a TRUNCATE TABLE, SQL actually drops that table in order to clear it so quickly. That won't be possible when a users is trying to execute the sproc in production. Since they have EXECUTE rights, they'd be able to execute it, but it will fail when the system tries to drop the table in question. For that, the executor needs to have DBO rights. Of course we're not going to grant people DBO rights in production! So the question becomes, how do you solve the problem?

I'm assuming that well defined change management and process control is in place already. If it is not, you need to start there first. Once you have a proper method for reviewing changes to sprocs going into production, you can apply this quick fix:

TSQL BEFORE

ALTER PROCEDURE [dbo].[BlaBlaBla]
@TableName varchar(60)
AS    
DECLARE @QUERY NVARCHAR(200);
...

TSQL AFTER

ALTER PROCEDURE [dbo].[BlaBlaBla]
@TableName varchar(60)
WITH EXECUTE AS OWNER
AS
DECLARE @QUERY NVARCHAR(200);
...

By simply adding the WITH EXECUTE AS OWNER line to the sproc, it will execute as owner level permissions, thus allowing the TRUNCATE statement to succeed.  Now it's important to understand that this creates a security risk that YOU must manage.  That's why it's important to review any and all changes that are made to the sprocs, especially if they're going to have this statement in them.  It may be better to just create a generic sproc that you can feed a table name to, which elevates like this and truncates the table passed to it.

Enjoy
C

16 November 2015

How do I - Determine what type of site template was used when a SharePoint site was created?

If you work in SharePoint regularly, you may develop the ability to look at a SharePoint site and correctly guess which template was used in the creation of said site.  Of course, given how SharePoint works, you'd probably only have a 50/50 shot at best of actually being correct.  Given how most everything is connected to Features and the fact that site templates in most cases just have a given sub set of Features that are turned on during the creation process, it's really anybody's guess.
Powershell to the rescue!
Of course, if you're an administrator with shell access to your farm, you can always resort to Powershell to get that answer.  All you have to do is get the WebTemplate property of the SPWeb object for the site thus:

Write-Host (Get-SPWeb http://sharepoint.crayveon.com/sites/site123 | Select WebTemplate);

Alternatively, you could use the Get-SPSite cmdlet at the beginning of the pipe to get all the sites and sub sites and their templates thus:

Write-Host (Get-SPSite http://sharepoint.crayveon.com/sites/site123 | Get-SPWeb | Select URL, WebTemplate);

Enjoy
C

17 October 2015

My "Save site as template" option is gone from Site Settings in SharePoint 2013... where did it go?

A common practice in the world of SharePoint is to create sub site templates.  A site collection administrator will customize a template sub site and then via the Site Settings use the "Save site as template" option to create a new template that can be used for new sub sites.  The problem is that this option isn't always available.  So why not?
The answer lies in the activation of Publishing Features on the site collection.  If at any point in time during the entire life cycle of the site, you had turned Publishing on for the site collection, this option is removed permanently.
See https://support.microsoft.com/en-us/kb/2492356 for more detail.

Later
C

SharePoint Remote Event Receivers are DEAD!!!

 Well, the time has finally come.  It was evident when Microsoft started pushing everyone to WebHooks, but this FAQ and related announcement...