Archive for July, 2013

Running SharePoint PowerShell Scripts from a Batch File (.bat)

You would think there would be a ton of information on how to do this. However, it took a good bit of research to figure this one out. So now I’m going to pass this information on to you!

It’s pretty simple. First you need a way to run the PowerShell engine from the DOS prompt. Use the syntax:

@powershell -command yourcommand

Obviously, if  you want to run a ps1 file, you just use the syntax:

@powershell -command path\filename.ps1

This works just fine with a plain PowerShell command/file but if you are going to run a SharePoint specific PowerShell command, you are going to have to add the SharePoint snapin. You can do this as such:

@powershell -command Add-PSSnapin ‘Microsoft.SharePoint.PowerShell’

@powershell -command yoursharepointpowershellcommand

Or just add the Add-PSSnapin command to the top of your ps1 file.

I wanted to run a bunch of .ps1 files from a .bat file so I could use task scheduler to kick them off. You simply use this syntax and your good to go!

 

, , , ,

1 Comment

Backward inheritance in SharePoint 2010 and 2013

My users are “challenging”. You guys know what that is code for, right? For some reason they decided that they want the extranet to have over 40 site collections but all the users would have access to are sub-sites of these site collections. Why? Only they know and they aren’t sharing that information with yours truly.

Here’s the problem. They want to store images, etc. at the site collection level and use those images in the sub-sites. Problem is, the users don’t have access to the site collection level so when they bring up a page with the image, you guessed it, they get the BFRX (Big F***ing Red X). Bummer.

So the users are screaming “Fix it, John” (best whinny voices here). Guess what, I said, there is no backward inheritance of permissions. Too bad for you. “Fix it, John” they continue to whine. Crap. So here is the solution: copy all the users from all the sub-sites to the visitors group in the site collection. That way they have read-only rights to the site collection and thus the images. By happy accident this also fixes a bug in the Google Analytics feature I downloaded from Codeplex (more on that later). So it’s off to PowerShell world to take care of business (TCB, baby!).

To pull this off, it’s a pretty simple matter to iterate through all the sub-sites, then iterate through all the users and add them to the visitors group. Not a bad solution but it takes forever. I know because I tried it. You are pulling duplicate users which eats up time (no need to worry about creating duplicate users in the visitors group, though. SharePoint won’t let you). So one of my users said “Why not use the All People list?”. I said they were aggravating, not stupid.

The All People list contains every user from the target site collection and all users from the sub-sites. It’s a pretty handy thing and is really called the User Information List. Below is the PowerShell code I used to pull out all the users and add them to the site collection visitors group.

I add the SharePoint snapin because I’m going to run this from Task Manager. The next blog will be on how to run this in batch. Next we define the site collection URL. Next, we iterate through the “User Information List” (All People) and finally, put them in the site collection Visitors group. Works like a charm!

The downside is that I had to create a separate .ps1 file for each site collection. I suppose if I was smart (and had more time), I could write some code to iterate through the site collections and make life easier. Maybe when I have some spare time…

So there you have it. Backward inheritance! This works in both 2010 and 2013. Next I’ll show you how to run PowerShell from the DOS command prompt. Stay tuned!

Add-PSSnapin ‘Microsoft.SharePoint.PowerShell’
$SiteUrl = “http://yoursitecollectionurl”
$web = Get-SPWeb $SiteUrl
$ListName = “User Information List”
$List = $web.Lists[$ListName]
#$List.Items
foreach ($ListItem in $List.Items)
{
$prefix=”i:05″
if($ListItem.Name.StartsWith($prefix))
{
Write-Host “username=” $ListItem.Name
Set-SPUser -Identity $ListItem.Name -Web http://yoursitecollectionurl -Group ‘group name (visitors in this case)’
}
}

, , , , , , , , ,

Leave a comment