PowerShell–Rename the ‘&’ in File Names

So recently I was moving large amounts of .DOC files into SharePoint libraries and it was occasionally causing an error due to invalid characters in the file name.  This was mostly due to having abbreviations in the names that included ampersands, for example “M&A Report 2010.doc”.  Now, looking at this, it’s an easy fix to replace the ‘&’ character with an underscore with PowerShell, this example does it for all files in a directory:

get-childitem *.* | foreach { rename-item $_ $_.Name.Replace("&", "_") }

Which works fine for the files within that folder, what if you want to do it recursively and include the subfolders?  no problem for PowerShell thanks to the ‘-recurse” flag:

get-childitem *.* -recurse | foreach { rename-item $_ $_.Name.Replace("&", "_") }

And there you have it, an easy and simple way to prep a file share or set of directories to move into a SharePoint library.

SharePoint 2010–Mobility Redirect Feature

SharePoint provides a clumsy support for mobile devices by providing for List Views designated for mobile devices and automatic page redirect if the site is accessed by a mobile device.  By rendering column data in a format suitable for mobile devices it works, but it’s not a great experience in my opinion. By default Windows SharePoint Services ships with a “Mobility Redirect” feature that creates a default.aspx page in the /m folder of each site.

For supporting users who what to access a mobile friendly version of the site, You have two ways to turn on the feature:

Using stsadm.exe command line tool

Stsadm -o activatefeature -name MobilityRedirect -URL “http://<URL>” 

Using PowerShell

Enable-SPFeature -Identity MobilityRedirect -Url http://<URL>

Remember that this is a SPWeb feature so you can turn it Off or On on an individual site.  Personally I don’t like the branding on the mobile view, so remember that when branding or developing sites for mobile devices, you can easily switch to mobile view of the page by using User Agent Switcher add-in for FireFox.

Replicating User Profile Data in SharePoint 2010

Having recently upgraded and migrated one of our farms from the old datacenter running 2007 to the new datacenter running 2010 there were some slight issues with the User Profiles and getting them moved to the new location.  Within SP2010 there are generally only two ways to get user profile data onto the new farm from an existing farm in SharePoint 2010. The first is to consume the User Profile Service of the host farm, but this causes issues if you want to host a My Site host (that contain personal sites) on the consuming farm. The second way is to use the Replication Engine. The Replication Engine uses audiences and trusted My Site Hosts to determine which farm “owns” the particular set of user profile data. The Replication Engine can also be used to keep data in synch with SharePoint 2007 data, which in our case was a good thing since we had to leave some sites behind on the 2007 farm until such time that we can update the custom coded web parts to be 2010 compliant.

Steps:

  1. Backup your Profile database.
  2. Download the SharePoint 2010 Administration Toolkit http://www.microsoft.com/downloads/en/details.aspx?FamilyID=718447d8-0814-427a-81c3-c9c3d84c456e&displaylang=en and install it on at least one server. If you want to push data in more than one direction you will need to install it on another server in the other farm as well.  I generally like to use the server we’ve set up for central admin or an application server and stay off the WFEs.
  3. Backup your Profile database.
  4. Create your audiences and Trusted My Site Host entries on all the servers involved.  If they’re not set up correctly you get lots of fun error messages which are typically MSCryptic.
  5. Backup your Profile database.
  6. Read the User Profile Replication Engine Overview at http://technet.microsoft.com/en-us/library/cc663011.aspx. The 2010 version is a PowerShell window with a few commands. Basically you can start and stop incremental replication, do full replications, and get a list of profile properties, because PowerShell is awesome that way.
  7. Backup your Profile database.
  8. Before you start replicating…turn off the smtp service, full replications will send out colleague notifications, which is not fun to try to explain to everyone why they got a bunch of notification for stuff they already had.
  9. Just for good measure, Backup your Profile database

A couple of points to remeber when trying to run a profile replication in SharePoint 2010.

  • The account running the replication needs the ‘Full Control’ permission on the User Profile Service Application on the destination SharePoint farm.
  • Although the documentation states that the URLs to use for the source and destination should be those of the my site host on each farm, I could not get it working using these URLs at all. Instead, the URLs for the Central Admin site (!) on each farm worked a treat. These values are running the Start-SPProfileServiceFullReplication or Start-SPProfileServiceIncrementalReplication cmdlets.
  • Again, the documentation implies that you should pass in the Properties to the replication cmdlet, either explicitly or via the pipeline (well, all the examples I could find did). I found this to be troublesome, and it is an optional parameter so try the most simple call to the replication cmdlet – if the User Profile Service on the source farm is customised, this seemed to cause issues if trying to replicate fields that don’t exist on the destination.

SharePoint 2010 – Migrating Search Admin to Different Servers in the Farm

Recently one of the farms I work with got some additional servers joined to the farm. Rather than provisioning another WFE, we were expanding some of the back-end capacity. The Search Admin was running on a WFE, and we wanted to move search off to some dedicated servers. Here is the PowerShell script for moving Search Admin:

$SSA = Get-SPEnterpriseSearchServiceApplication "Current Search Service App"
$SA = Get-SPEnterpriseSearchAdministrationComponent -SearchApplication $SSA
$SA | Set-SPEnterpriseSearchAdministrationComponent -SearchServiceInstance NewServerName –Force

After moving the Search Admin you’ll want to kick start the crawlers with the following:

Suspend-SPEnterpriseSearchServiceApplication $SSA
Resume-SPEnterpriseSearchServiceApplication $SSA

And as easy as that you can move search admin off to a new server!

SharePoint 2010–Blocked File Types

SharePoint has a nice little security feature for blocking certain files from being uploaded to SharePoint libraries because of security issues. Each Web application maintains a list of blocked file types that is based on file name extensions. For example, you can block files that have the .exe extension because those files can be run on the client computer and can contain malicious code. I was asked a question the other day about blocked file types in SharePoint 2010 and how to allow them into a SharePoint repository. Now I realize that this is a massive risk but the client had a reason for wanting to do this because they wanted to store installation packages in a SharePoint library. This is generally considered not best practices, but if you have a site that is trusted and only a few individuals have upload rights, you are effectively creating a network share for installation packages.

However, I will stress that you should never allow any executable file in your SharePoint site. But…there is an exception to every rule and this is probably one of them.

Using SharePoint 2010 Central Admin

First navigate to Central admin and once there click on the security section

image

Once in the security section you need to be in General Security | Define blocked file types

image

Here you will find a list of file with file type extensions just add or remove the files you wish to block or unblock and then click ok. This is a global setting across all site collections so happy blocking. And don’t forget to check and tighten up on the media files including WMA, MP3, MP4, MOV, WMV etc. it will save you gigabytes of storage later.

image

Using PowerShell Commandlets

Get-SPBlockedFileType

 

function Get-SPBlockedFileType {
<#
.Synopsis
 This function can be used to retrieve blocked file types for a SharePoint Web Application.
.Description
 This PowerShell function uses the SharePoint Object Model to get all file
 types in the BlockedFileExtensions property for a Web Application.
.Example
 C:\PS>Get-SPBlockedFileType -WebApplication http://intranet
 This example gets blocked file types for a SharePoint Web Application at http://intranet.
#>
[CmdletBinding()]
Param(
[Parameter(Mandatory=$true, ValueFromPipeline=$true, Position=0)]
[Microsoft.SharePoint.PowerShell.SPWebApplicationPipeBind]$WebApplication
)
$WebApp = Get-SPWebApplication $WebApplication
$Extensions = $WebApp.BlockedFileExtensions
$Extensions | ForEach-Object {Write-Host $_}
}

Add-SPBlockedFileType

Add-SPBlockedFileType
function Add-SPBlockedFileType {
<#
.Synopsis
 This function can be used to add a blocked file type for a SharePoint Web Application.
.Description
 This PowerShell function uses the SharePoint Object Model to add file
 types to the BlockedFileExtensions property for a Web Application.
.Example
 C:\PS>Add-SPBlockedFileType -WebApplication http://intranet -Extension ps1
 This example adds the ps1 file type to the BlockedFileExtensions collection
 for a SharePoint Web Application at http://intranet.
#>
[CmdletBinding()]
Param(
[Parameter(Mandatory=$true, ValueFromPipeline=$true, Position=0)]
[Microsoft.SharePoint.PowerShell.SPWebApplicationPipeBind]$WebApplication,
[Parameter(Mandatory=$true)][System.String]$Extension
)
$WebApp = Get-SPWebApplication $WebApplication
$Extensions = $WebApp.BlockedFileExtensions
$Extensions.Add($Extension)
$WebApp.Update()
}

Remove-SPBlockedFileType

function Remove-SPBlockedFileType {
<#
.Synopsis
 This function can be used to remove a blocked file type from a SharePoint Web Application.
.Description
 This PowerShell function uses the SharePoint Object Model to remove file
 types from the BlockedFileExtensions property for a Web Application.
.Example
 C:\PS>Remove-SPBlockedFileType -WebApplication http://intranet -Extension ps1
 This example removes the ps1 file type from the BlockedFileExtensions collection
 of a SharePoint Web Application at http://intranet.
#>
[CmdletBinding()]
Param(
[Parameter(Mandatory=$true, ValueFromPipeline=$true, Position=0)]
[Microsoft.SharePoint.PowerShell.SPWebApplicationPipeBind]$WebApplication,
[Parameter(Mandatory=$true)][System.String]$Extension
)
$WebApp = Get-SPWebApplication $WebApplication
$Extensions = $WebApp.BlockedFileExtensions
$Ext = $Extensions.Remove($Extension)
if($Ext -eq $true){
Write-Warning "Filetype $($Extension) has been removed from Web Application $(($WebApp).Name)"
}
else{
Write-Warning "Unable to delete filetype $($Extension) from Web Application $(($WebApp).Name)"
}
$WebApp.Update()
}

 

Using Visual Studio 2010:

  • Open Visual Studio 2010.
  • Go to File => New => Project.
  • Select Console Application from the installed templates.
  • Enter the Name and click Ok.
  • Add the following references.

    o Microsoft.SharePoint.dll
  • Add the following namespaces.

    o using Microsoft.SharePoint;

    o using System.Collections.ObjectModel;

  • Replace the code with the following.

    string webAppUrl = "http://<server>/";

    string addBlockFileType ="aspx";

    string removeBlockFileType ="asp";

    SPWebApplication webApp =SPWebApplication.Lookup(new Uri(webAppUrl));

    Collection<string> blockFileTypes = webApp.BlockedFileExtensions;

    foreach (string fileExtension in blockFileTypes)

    {

    // displaying all the file extensions that are blocked

    Console.WriteLine(fileExtension.ToString());

    }

    Console.ReadLine();

    // adding blocked file type

    blockFileTypes.Add(addBlockFileType);

    // removing blocked file type

    blockFileTypes.Remove(removeBlockFileType);

    webApp.Update();

  • Build the solution.

  • Hit F5.

  • All the file extensions from the blocked file type will be displayed as shown in the following.

    Block3.gif

  • A new file extension "aspx" is added and the existing file extension "asp" is removed successfully.

Default Blocked File Extensions

The following table shows the file types that are blocked by default and their corresponding file name extensions.

File name extension File type

.ade

Microsoft Access project extension

.adp

Microsoft Access project

.app

Application file

.asa

ASP declarations file

.ashx

ASP.NET Web handler file. Web handlers are software modules that handle raw HTTP requests received by ASP.NET.

.asmx

ASP.NET Web Services source file

.asp

Active Server Pages

.bas

Microsoft Visual Basic class module

.bat

Batch file

.cdx

Compound index

.cer

Certificate file

.chm

Compiled HTML Help file

.class

Java class file

.cmd

Microsoft Windows NT command script

.com

Microsoft MS-DOS program

.config

Configuration file

.cpl

Control Panel extension

.crt

Security certificate

.csh

Script file

.dll

Windows dynamic-link library

.exe

Program

.fxp

Microsoft Visual FoxPro compiled program

.hlp

Help file

.hta

HTML program

.htr

Script file

.htw

HTML document

.ida

Internet Information Services file

.idc

Internet database connector file

.idq

Internet data query file

.ins

Internet Naming Service

.isp

Internet Communication settings

.its

Internet Document Set file

.jse

JScript Encoded script file

.ksh

Korn Shell script file

.lnk

Shortcut

.mad

Shortcut

.maf

Shortcut

.mag

Shortcut

.mam

Shortcut

.maq

Shortcut

.mar

Shortcut

.mas

Microsoft Access stored procedure

.mat

Shortcut

.mau

Shortcut

.mav

Shortcut

.maw

Shortcut

.mda

Microsoft Access add-in program

.mdb

Microsoft Access program

.mde

Microsoft Access MDE database

.mdt

Microsoft Access data file

.mdw

Microsoft Access workgroup

.mdz

Microsoft Access wizard program

.msc

Microsoft Common Console document

.msh

Microsoft Agent script helper

.msh1

Microsoft Agent script helper

.msh1xml

Microsoft Agent script helper

.msh2

Microsoft Agent script helper

.msh2xml

Microsoft Agent script helper

.mshxml

Microsoft Agent script helper

.msi

Microsoft Windows Installer package

.msp

Windows Installer patch package file

.mst

Visual Test source files

.ops

Microsoft Office profile settings file

.pcd

Photo CD image or Microsoft Visual Test compiled script

.pif

Shortcut to MS-DOS program

.prf

System file

.prg

Program source file

.printer

Printer file

.pst

Microsoft Outlook personal folder file

.reg

Registration entries

.rem

ACT! database maintenance file

.scf

Windows Explorer command file

.scr

Screen saver

.sct

Script file

.shb

Windows shortcut

.shs

Shell Scrap object

.shtm

HTML file that contains server-side directives

.shtml

HTML file that contains server-side directives

.soap

Simple Object Access Protocol file

.stm

HTML file that contains server-side directives

.url

Uniform Resource Locator (Internet shortcut)

.vb

Microsoft Visual Basic Scripting Edition file

.vbe

VBScript Encoded Script file

.vbs

VBScript file

.ws

Windows Script file

.wsc

Windows Script Component

.wsf

Windows Script file

.wsh

Windows Script Host settings file