Wednesday 22 June 2011

BackConnectionHostNames PowerShell Script

- useful for SharePoint environments where you are accessing the site from the server itself:


You receive error 401.1 when you browse a Web site that uses Integrated Authentication and is hosted on IIS 5.1 or a later version




### Script ###

Param([string]$StringAdditionalUrls)
[System.String[]]$StringAdditionalUrlsArray = $StringAdditionalUrls.Split(",")
$ArrayAdditionalUrls = "$StringAdditionalUrlsArray".Split()

[string]$path = "HKLM:\SYSTEM\CurrentControlSet\Control\Lsa\MSV1_0\"
Write-Output "path=$path"
[string]$name = "BackConnectionHostNames"
Write-Output "name=$name"

[string]$currentvalue = (Get-Item "$path").GetValue("$name")
[System.String[]]$ArrayExistingValues = "$currentvalue".Split()
Write-Output "Current Values are: -"
ForEach ($currentAlias in $ArrayExistingValues)
                {
                Write-Output "- $currentAlias    "
                }

Write-Output "Additional Values are: -"
ForEach ($addAlias in $ArrayAdditionalUrls)
                {
                Write-Output "- $addAlias           "
                }
               
Write-Output "`nChecking current values..."
ForEach ($currentAlias in $ArrayExistingValues)
                                {
                                Write-Output "Checking $currentAlias"
                                If ($currentAlias)
                                                {             
                                                If ($newAlias)
                                                                {                                                             
                                                                If($ArrayNewUrls -notcontains $currentAlias)
                                                                                {
                                                                                Write-Output " -Adding: $currentAlias"
                                                                                $newAlias = $newAlias+" "+$currentAlias                                                            
                                                                                }
                                                                }
                                                Else
                                                                {
                                                                $newAlias = $currentAlias
                                                                Write-Output " -Adding $currentAlias"
                                                                }
                                                }                             
                                [System.String[]]$ArrayNewUrls = "$newAlias".Split()
                                }
Write-Output "`nComparing new values..."
ForEach ($addAlias in $ArrayAdditionalUrls)
                {
                Write-Output "Checking: $addAlias.."
                If ($ArrayExistingValues -contains $addAlias)
                                {
                                Write-Output " -$addAlias already exists"
                                }                             
                Else
                                {
                                Write-Output " -Adding: $AddAlias"
                                $newAlias = $newAlias+" "+$AddAlias   
                                }
                }
               
Write-Output "`nNow setting registry values..."

$newAlias = $newAlias.Trim()
[System.String[]]$ArrayNewUrls = "$newAlias".Split()



Write-Output "New list of values : -"
#ForEach ($newAlias in $ArrayNewUrls)
#             {
                Write-Output $newAlias
#             }

Set-ItemProperty -Path $path -Name $name -Value ([string[]]$ArrayNewUrls)

Write-Output "`ncompleted adding URLs to BackConnectionHostNames"

Write-Output "Completed Script: $scriptpath"


Tuesday 21 June 2011

Check crawl status - PowerShell script

### Crawl Status script - outputs crawl status, schedule details, start address, last start and completed times

###################################################################

Param ([string]$sspURL)

$description =@'
describes crawl status
'@
### error handling ###

$scriptname = $MyInvocation.MyCommand.Name
$scriptpath = $MyInvocation.MyCommand.Path

$transcriptFile = "C:\Install\Logs\Powershell_$scriptname.log"

Start-Transcript $transcriptFile -append -force

Write-Output "Starting Script: $scriptpath"
Write-Output $description

$error.Clear()
$erroractionpreference="Stop"
$ReportErrorShowExceptionclass=$true
$ReportErrorShowInnerexception=$true
$ReportErrorShowSource=$true
$ReportErrorShowStackTrace=$true

trap {

$errorRecord = $error[0]
Write-Output "ErrorRecord:"
$errorRecord | Format-List * -Force | Out-Default
Write-Output "CategoryInfo:"
$errorRecord.CategoryInfo | Format-List * -Force | Out-Default
Write-Output "ErrorDetails:"
$errorDetails = $errorRecord.ErrorDetails
Write-Output "Message:"
$errorDetails.Message | Out-Default
Write-Output "RecommendedAction:"
$errorDetails.RecommendedAction | Out-Default
Write-Output "FullyQualifiedErrorId:"
$errorRecord.FullyQualifiedErrorId | Out-Default
Write-Output "InvocationInfo:"
$errorRecord.InvocationInfo | Format-List * -Force | Out-Default
Write-Output "TargetObject:"
$errorRecord.TargetObject | Format-List * -Force | Out-Default
Write-Output "Exception:"
$Exception = $errorRecord.Exception
for ($i = 0; $Exception; $i++, ($Exception = $Exception.InnerException))
{$Exception | Format-List * -Force | Out-Default}
;break
}


#####################################################################
### loading assemblies ###

[void][System.Reflection.Assembly]::Load("Microsoft.Office.Server.Search, Version=12.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c")

[void][System.Reflection.Assembly]::Load("Microsoft.Sharepoint, Version=12.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c")

[void][System.Reflection.Assembly]::Load("Microsoft.Office.Server, Version=12.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c")

#####################################################################

### $sspURL: e.g. "http://ssp.mydomain/ssp/admin"

$site = new-object Microsoft.SharePoint.SPSite($sspURL)

### getting spsite from $Url

$searchContext = [Microsoft.Office.Server.Search.Administration.SearchContext]::GetContext($site)

### getting searchContext from spsite

$content = new-object Microsoft.Office.Server.Search.Administration.Content($searchContext)

### getting admin content from searchContext

### Initializes a new instance of the Content class.


#####################################################################

$df = "local office SharePoint server sites"

$ContentSourceCollection = $content.ContentSources

$local = $ContentSourceCollection[$df]

Write-output "The crawl status is: " $local.crawlstatus
Write-output "The start time of the last crawl: " $local.CrawlStarted
Write-output "The time of the last completed crawl of a content source is: " $local.CrawlCompleted
Write-output "The content source's schedule of full crawls is: " $local.FullCrawlSchedule
Write-output "The content source's schedule of incremental crawls is: " $local.IncrementalCrawlSchedule
Write-output "The StartAddresses: " $local.StartAddresses

stop-transcript





#####################################################################

using PowerShell v2 remoting to restart SharePoint services on all the WFE servers in the farm

First - put the servers into an array:
[Void][System.Reflection.Assembly]::Load("Microsoft.SharePoint, Version=12.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c")

$farm = ([Microsoft.SharePoint.Administration.SPFarm]::Local`

%{ $_.Services} `

?{$_.typename -eq "Windows SharePoint Services Web Application"}`

%{$_.instances} `

%{$_.Server} `

%{$_.Address}

)

$farm


Then use a Foreach loop to step through each server in the array and restart the services on each:

foreach ($i in $farm)


{

$session = New-PSSession -ComputerName $i

Invoke-Command -Session $session -ScriptBlock { Restart-Service Sptimerv3 }

Invoke-Command -Session $session -ScriptBlock { Restart-Service Spadmin }

}

Remember to configure the firewall correctly and to ensure WinRM is initialized and enabled for Kerberos on the remote machines

Tuesday 14 June 2011

PowerShell script to restart the SPadmin and the SPTimerv3 service

Restart-Service -DisplayName "Windows SharePoint Services*" -Exclude "*Writer","Windows SharePoint Services Tracing","Windows SharePoint Services Search" -Verbose

Tuesday 7 June 2011

PowerShell script to set the SharePoint office server search Performance Level to Maximum

### This script has has C# code wrapped inside PowerShell

### The first part is the C# code that does the work mapped to a variable $Source

$Source = @"

using Microsoft.SharePoint;
using Microsoft.Office.Server.Search;
using Microsoft.Office.Server.Search.Administration;

namespace PowerShellWrapper
{
public class Program
{
public static void PerformanceLevel_set(string[] args)
{
SPSite site = new SPSite(args[0]);
SearchService ss = site.WebApplication.Farm.Services.GetValue("OSearch");
ss.PerformanceLevel = SearchPerformanceLevel.Maximum;
ss.Update();
site.Dispose();
}
}
}

"@
##################################################################

### This next bit prepares Powershell for compiling the C# code

### Obtains an ICodeCompiler from a CodeDomProvider class.

$provider = New-Object Microsoft.CSharp.CSharpCodeProvider

## Get the location for System.Management.Automation DLL

$dllName = [PsObject].Assembly.Location

## Configure the compiler parameters

$compilerParameters = New-Object System.CodeDom.Compiler.CompilerParameters

### Next load the assemblies

$assemblies = @("System.dll", $dllName)

$compilerParameters.ReferencedAssemblies.AddRange($assemblies)

$compilerParameters.ReferencedAssemblies.Add("C:\Program Files\Common Files\Microsoft Shared\web server extensions\12\ISAPI\Microsoft.SharePoint.dll")

$compilerParameters.ReferencedAssemblies.Add("C:\Program Files\Common Files\Microsoft Shared\web server extensions\12\ISAPI\Microsoft.Office.Server.dll")

$compilerParameters.ReferencedAssemblies.Add("C:\Program Files\Common Files\Microsoft Shared\web server extensions\12\ISAPI\Microsoft.Office.Server.Search.dll")

$compilerParameters.IncludeDebugInformation = $true

$compilerParameters.GenerateInMemory = $true

### This next line does the work

$compilerResults = $provider.CompileAssemblyFromSource($compilerParameters, $Source)

### some error handling

if($compilerResults.Errors.Count -gt 0) {

$compilerResults.Errors
% { Write-Error ("{0}:`t{1}" -f $_.Line,$_.ErrorText) }

}

[PowerShellWrapper.Program]::PerformanceLevel_set(http://localhost/)

### Because it points to localhost it will only work when run locally

################################################################### END

Monday 6 June 2011

edit content source powershell script for MOSS 2007

Param ([string]$sspURL, [string]$sal1)
$description =@'

configures the default content source - deletes all start addresses and then adds portal start address and sets incremental crawl schedule
'@


#Edit default content source to replace it with $Sal1 then and set incremental crawl schedule to 5PM every weekday

#####################################################################
[System.Reflection.Assembly]::Load("Microsoft.Office.Server.Search, Version=12.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c")
[System.Reflection.Assembly]::Load("Microsoft.Sharepoint, Version=12.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c")
[System.Reflection.Assembly]::Load("Microsoft.Office.Server, Version=12.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c")

#####################################################################

### e.g. $sspURL = http://breakfast/ssp/admin
$site = new-object Microsoft.SharePoint.SPSite($sspURL)

### getting spsite from $Url
$searchContext = [Microsoft.Office.Server.Search.Administration.SearchContext]::GetContext($site)

### getting searchContext from spsite

$content = new-object Microsoft.Office.Server.Search.Administration.Content($searchContext)

### getting admin content from searchContext
###Initialize a new instance of the Content class.

$df = "local office SharePoint server sites"

$ContentSourceCollection = $content.ContentSources

$local = $ContentSourceCollection[$df]

$sac = $local.startaddresses

$sac.clear();

### e.g. $sal1 = http://test2.com/

$sac.add($sal1)

#####################################################################

$ContentSourceCollection = $content.ContentSources

$local = $ContentSourceCollection[$df]

#set context

$dailyIncrementalSchedule = new-object Microsoft.Office.Server.Search.Administration.WeeklySchedule($searchContext)

#set start hour

$hour = 17

$dailyIncrementalSchedule.starthour = $hour

$local.incrementalcrawlschedule = $dailyincrementalschedule

$local.Update();

#set weekly schedule

$dailyIncrementalSchedule.DaysOfWeek = [Microsoft.Office.Server.Search.Administration.DaysOfWeek]::Weekdays

$local.incrementalcrawlschedule = $dailyincrementalschedule

$local.update()



#####################################################################

Saturday 4 June 2011

How many .pdf files are there in my farm?

Run this against your content DBs:


SELECT count(*) from alldocs
where Extension = 'pdf'

SharePoint Information Architecture Diagram

Here is the template I use for Information Architecture designs. It's built using Mindjet and I flesh the nodes out with the low level d...