Tag Archives: PowerShell

PowerShell: Retrieve All Sites With or Without Personal Sites (MySites)

I love using the pipeline in PowerShell.  Recently I was in a situation where, if a PS script what a one-liner I didn’t have to submit it through the review/approval/change management process.  Its amazing what you can do in PowerShell in a single line of code!

Retrieve all site collections, for all web applications in the current farm.

$oSites = Get-SPWebApplication | Get-SPSite -Limit All

That one is pretty simple and I’m sure you’ve done it a bazillion times.  Lets add a little more and return all site collections, for all web applications except personal sites!

$oSites = Get-SPWebApplication | Get-SPSite -Limit All | where {($_.RootWeb.WebTemplateId -ne 54) -and ($_.RootWeb.WebTemplateId -ne 21)}

The only thing I’ve added here is a where clause that excludes sites with a template ID of 54 (personal site host) and 21 (personal sites).

Debugging SharePoint Issues and ULS Log Files

I often see administrators and developers new to SharePoint find debugging difficult and complex.

When working with SharePoint, log files are your friend.  In large on-premise farms, locating issues within large log files can be time consuming and sometimes difficult.

When I am presented with an error that contains a correlation ID, I first resort to PowerShell instead of a ULS Viewer.

Two PowerShell cmdlets that are your friend are: Get-SPLogEvent and Merge-SPLogFile.

Before you can use these cmdlets in your PowerShell scripts, make sure to load the SharePoint PowerShell snapin.

if((Get-PSSnapin -Name Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue) -eq $null)
{
    Add-PSSnapin Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue
}

Get-SPLogEvent

The Get-SPLogEvent cmdlet will retrieve specific events from a ULS Log File.  For example, the following call will retrieve all entries that occurred during a specified time range:

Get-SPLogEvent -StartTime "12/04/2007 17:00" -EndTime "12/04/2007 18:00"

If you wish to retrieve ULS entries associated with a specific correlation ID, you can use the following:

Get-SPLogEvent | ? {$_.Correlation -eq "<Correlation ID>"} | Select Area, Category, Level, EventID, Message

Where <Correlation ID> is the id you wish to filter.

If you wish to display the results in a nicely formatted list, add Format-List:

Get-SPLogEvent | ? {$_.Correlation -eq "<Correlation ID>"} | Select Area, Category, Level, EventID, Message | Format-List

Be patient when running the Get-SPLogEvent cmdlet as it can take quite a long time to traverse through all the ULS log files.

I have a diagnostics PowerShell library that contains many functions that simplify diagnosing issues, writing log files, etc.  One of the functions in this library is my Get-SPLogEventByCorrelationID.  Which simply calls the Get-SPLogEvent cmdlet and filters the results by a specified correlation ID.

function Get-SPLogEventByCorrelationID
{
    [CmdletBinding()]
    Param([Parameter(Mandatory=$true)]
        [string]$CorrelationID
    )
    $logEntries = Get-SPLogEvent | ? {$_.Correlation -eq $CorrelationID} | Select Area, Category, Level, EventID, Message
    return($logEntries)
}

For more information on using the Get-SPLogEvent cmdlet, see the following:

Merge-SPLogFile

The Merge-SPLogFile cmdlet combines ULS log entries, from all servers in a SharePoint farm, to a single (specified) log file.

The following example will merge all ULS log files for the last hour:

Merge-SPLogFile -Path "C:\Logs\FarmMergedLog.log" -Overwrite

If you wish to merge all ULS log events for a specific correlation ID, you can use the following call:

Merge-SPLogFile -Path "C:\Logs\FarmMergedLog.log" -Correlation "<Correlation ID>" -Overwrite

Where <Correlation ID> is the id you wish to filter.

As with the Get-SPLogFile, I have included some common functions in my diagnostics library.  One that I use on a regular basis is Merge-SPLogFileByCorrelationID

function Merge-SPLogFileByCorrelationID
{
    [CmdletBinding()]
    Param(
        [Parameter(Mandatory=$true)]
            [string]$CorrelationID,
        [Parameter(Mandatory=$false)]
            [bool]$Overwrite=$false,
        [Parameter(Mandatory=$false)]
            [int]$LeadingSpaceCount=0
)

    $ls = "".PadRight($LeadingSpaceCount," ")
    $diagConfig = Get-SPDiagnosticConfig
    $ulsLogLocation = $diagConfig.LogLocation + "\MergeLog-Correlation (" + $CorrelationID + ").log"
    Write-Verbose ([string]::Format("$ls- Writing merged logs to file [{0}].", $ulsLogLocation))
    if($Overwrite)
    {
        Merge-SPLogFile -Path $ulsLogLocation -Correlation $CorrelationID -Overwrite
    }
    else
    {
        Merge-SPLogFile -Path $ulsLogLocation -Correlation $CorrelationID
    }
}

Other References

Conclusion

With a little knowledge and tools, you can become efficient at debugging issues in SharePoint.  If you would like a copy of my diagnostic script, please contact me; I will be happy to send it to you.

Happy SharePointing!

AutoSPInstaller: SharePoint 2013 March 10, 2015 CU (KB2956166)

The AutoSPInstaller tool is not something I’ve written about in the past.  I’m not sure why because its a fantastic tool and I use it on a regular basis.  In general, it is a PowerShell based SharePoint installation tool.  If you are unfamiliar with it, I do recommend you take a look at it here.

I use AutoSPInstaller to build SharePoint farms including the creation of web applications, site collections, installation of PU’s and CU’s, etc.  The beauty of using a scripted approach is its consistent.  If your farm ever burns to the ground, its a way to rebuild it just as it was.

I recently used AutoSPInstaller to build a SharePoint 2013 farm for a client.  We then made the decision to install SharePoint 2013 March 10, 2015 CU.  As with all farm-level modifications I make, I added this CU to the AutoSPInstaller updates directory then ran the installer again.  It ran the CU, psconfig and ensured the farm was in an operational state.  It worked flawlessly!

To accomplish this, download the (3) SharePoint 2013 March 10, 2015 CU installation files from the Microsoft site and place them in the SP\AutoSPInstaller\2013\Updates directory.  You then run the launch script again, on all servers, and the cumulative update will be installed for you.  In addition, the script will run psconfig; so you don’t need to do than manually!