Showing posts with label Script. Show all posts
Showing posts with label Script. Show all posts

Thursday, 22 March 2012

Setting ADFS Token Expiration times.

The SAML token lifetime is set by the token issuer (resource ADFS Server). You may find that this is too short and want to extend it.

Note that the valid session time is set by the ADFS SAML TokenLifeTime and the LogonTokenCacheExpirationWindow in SharePoint.
The SAML TokenLifeTime always needs to be greater than the LogonTokenCacheExpirationWindow in SharePoint.

The formula to apply here is as follows.
Valid Session time = TokenLifeTime - LogonTokenCacheExpirationWindow




To change the session times


On the resource ADFS server
The following script example shows you how to change the lifetime of the SAML token issued by the "SharePoint Adatum Portal" relying party in ADFS to 480 minutes.

Add-PSSnapin Microsoft.ADFS.PowerShell
Set-AdfsRelyingPartyTrust –TargetName "SharePoint Adatum Portal" –TokenLifeTime 480

On the Resource SharePoint Farm
The following script example shows you how to change the LogonTokenCacheExpirationWindow in SharePoint to two minutes.

$ap = Get-SPSecurityTokenServiceConfig
$ap.LogonTokenCacheExpirationWindow = (New-TimeSpan -minutes 2)
$ap.Update();
IIsreset


SAML Token Expiration in SharePoint on this page is worth a read to understand the principles
http://msdn.microsoft.com/en-us/library/hh446526.aspx


To display the current ADFS relying party trust settings
Set-AdfsRelyingPartyTrust

To display the SharePoint Security Token Service Config
Get-SPSecurityTokenServiceConfig

Monday, 19 March 2012

SharePoint Website Warm up options

There are many warm up scripts and exe's that can run against your SharePoint environments to speed up the first time access to any site. These options have worked the best for our infrastructure. personally the first works well and has useful options.

Setup a scheduled windows task which launches your selection, running under an account that has relevant access, every morning before start of business. The web application is hit and every page iterated in the site.




SPWakeup : http://spwakeup.codeplex.com/
A simple application that touches each site and site collection on a Sharepoint or WSS server to rebuild the IIS cache.
This is compatible with Win 2008 R2 and SharePoint 2010, nice touch is the email letting you know it's been run.

Powershell Script : http://www.jonthenerd.com/2011/04/19/easy-sharepoint-2010-warmup-script-using-powershell/
The script loads the SharePoint plugin, enumerates the zones, and sends a request to each one.


MS Application Warm-up : http://forums.iis.net/t/1176740.aspx
The MS IIS team had a nice warm up optiont that i've used before, but this has been removed at present (19/03/2012)
Keep an eye on any new developments as it was a good option.
SP George had a good how to : http://sharepointgeorge.com/2010/warm-sharepoint-web-applications-windows-2008-r2-iis-75-application-warmup-module/



For reasons on why we get IIS resets and reasons to use Warm up scripts check out this link : http://social.technet.microsoft.com/Forums/en-US/sharepointadmin/thread/8547eaed-5420-4cca-af29-206d71898322/

Monday, 5 March 2012

SharePoint 2010 Farm Backups via PowerShell

There are a few ways to do this but this method gave good results.

Overview :
Setup a share on a Server you will use to store the farm backups. This can be any one of your SP servers in the Farm. We normally backup to a share on the SQL box.
Setup scheduled tasks to trigger a batch file which runs the PS scripts to backup the farm, or cleanup the backups. This seems like an extra step but worked for me on Win 2008 R2 where nothing else would after a long day.
A good place to run this task would be an application server that most folk don't have access to as i set the script execution policy to unrestricted.



Pre-reqs :

Script execution status :
Check the server that you will be running the PS scripts on.
Ensure the execution policy for PS scripts are set to unrestricted.
(run PS as admin) PS commands in blue
To check : Get-ExecutionPolicy
To Set : Set-ExecutionPolicy Unrestricted


Setup your File share to store you backups :
Share the folder with the following settings
Everyone full access
NTFS permissions : SQL account & SP Admin account full access. (you need both accounts listed here)
Share name : \\Servername\FarmBackups



PS Scripts setup : 

Place you backup scripts in a relevant folder
\\Servername\FarmBackups\backupscripts


1. Setup a .bat file to be triggered by the scheduled task
File name : 1schedfarmbakup.bat
Insert the following line in the file and save. Batch file command in red
powershell -command "&{D:\FarmBackups\backupscripts\2backupSPfarm.ps1}"




2. Setup the powershell script file to backup the Farm.
File name : 2backupSPfarm.ps1
Insert the following lines and save
Add-PsSnapin Microsoft.SharePoint.Powershell
Backup-SPFarm -Directory \\Servername\FarmBackups -BackupMethod full -BackupThreads 10 -percentage 10 -verbose –force

Note : backup farm to specified directory using full backup with 10 threads, display the results every 10 percent verbosely and force the script to run even if there is not enough space on the drive. so make sure you have monitoring on you drive space!


3. Setup the bat file to clean out old Farm backups from the server.
File name : 3clean.bat
Insert line below and save
powershell -command "&{D:\FarmBackups\backupscripts\4cleanbackups.ps1 }"


4. Setup the powershell cleanup script

Filename  : 4cleanbackups.ps1
Insert the following, with correct path, in italic below, and your required number of backups and save

#///Start of ps script
# Location of spbrtoc.xml
$spbrtoc = "\\servername\FarmBackups\spbrtoc.xml"

# Days of backup that will be remaining after backup cleanup.
$days = 2

# Import the Sharepoint backup report xml file
[xml]$sp = gc $spbrtoc

# Find the old backups in spbrtoc.xml
$old = $sp.SPBackupRestoreHistory.SPHistoryObject |
? { $_.SPStartTime -lt ((get-date).adddays(-$days)) }
if ($old -eq $Null) { write-host "No reports of backups older than $days days found in spbrtoc.xml.`nspbrtoc.xml isn't changed and no files are removed.`n" ; break}

# Delete the old backups from the Sharepoint backup report xml file
$old | % { $sp.SPBackupRestoreHistory.RemoveChild($_) }

# Delete the physical folders in which the old backups were located
$old | % { Remove-Item $_.SPBackupDirectory -Recurse }

# Save the new Sharepoint backup report xml file
$sp.Save($spbrtoc)
Write-host "Backup(s) entries older than $days days are removed from spbrtoc.xml and harddisc."

#///end of ps script



Setup Scheduled tasks.

Login to the Server running the backups as SP setup account


Setup new task (Backup the farm)

General:
Name - Backup SharePoint Farm
Run whether user is logged in or not
Run  with highest privileges
Configure for : windows 2008 R2

Trigger :
Schedule
Daily at 18:00
Stop if runs longer than 5 hours (enter appropriate time)
Enabled

Actions :
start a program
Browse to the 1schefarmbackup.bat
Fill in appropriate start in dir

Conditions :
Start task only if computer is on AC

Settings :
Allow run on demand
Stop if runs longer than 3 days
If task does not end, force to stop.

Click OK to create task and enter PWD for SP Setup account.



Setup new task  (cleanup old backups)

General:
Name – Cleanup SP Farm Backups
Run whether user is logged in or not
Run  with highest privileges
Configure for : windows 2008 R2

Trigger :
Schedule
Daily at 04:00
Stop if runs longer than 2 hours (enter appropriate time)
Enabled

Actions :
start a program
Browse to the 3clean.bat
Fill in appropriate start in dir

Conditions :
Start task only if computer is on AC

Settings :
Allow run on demand
Stop if runs longer than 4 hours
If task does not end, force to stop.

Click OK to create task and enter PWD for SP Admin account.



Lastly, test your scheduled tasks and ensure the backup folders and .xml files are created on your share. Then check your cleanup scripts run successfully and cleanup the relevant folders a day or two later.



This came from somewhere but never kept the reference, apologies if came from you.

PowerShell script not running as Scheduled Task Server 2008
http://social.technet.microsoft.com/Forums/en-US/ITCG/thread/0ea10ab8-8dd3-4c4c-977c-bbfd0bb17a1d/

PowerShell remoting

PowerShell remoting gives you the ability to run powershell scripts remotely on another machine.
Pretty cool if you have to run recurring scripts on multiple machines from your own PC.



On your target server ::
Open a powershell window as admin and run the following commands in blue

Enable-PSRemoting
Accept all prompts

Set-Item WSMan:\localhost\Shell\MaxMemoryPerShellMB 1000
Accept all prompts

Enable-WSManCredSSP –Role Server
Accept all prompts



If your account running the script needs ShellAdmin permissions to run the script, check if the account is shell admin with the following on your target server. This was only relevant to what I was doing in SharePoint, ignore if not applicable.
get-spshelladmin

If its not listed, add the account to shell admin on the target server.
add-spshelladmin accountname


Add the account (or SP account in my case) used to run code to the server local admin group.
In case of a multi-server farm, add on all servers. This again may not be relevant in your case.



On your Client Server ::
(where you want to run your script)
Open a powershell window as admin and run the following commands in blue
 
Enable-WSManCredSSP -Role client -DelegateComputer *


Test access from the client server to the target server by entering the following PS command in a PS window
Enter-PSSession -ComputerName servername -Authentication CredSSP -Credential domain\accountname




All thanks to Abid the code Yoda and this URL :
http://blogs.msdn.com/b/opal/archive/2010/03/07/sharepoint-2010-with-windows-powershell-remoting-step-by-step.aspx

PowerShell cheat sheet

Random basic commands I keep forgetting, this list will surely keep growing


Add the SharePoint snap in :

Add-PSSnapIn Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue


Set a path :

Set-location "path"

Output the PS command in a formatted table

"randomPS-command" | Format-Table –AutoSize

Check DB status for upgrade status

Get-SPDatabase | ?{$_.NeedsUpgrade –eq $true} | Select Name

Install SP Solution to all web applications  
Add-SPSolution -LiteralPath "c:\source\CodeMonkeySolution.wsp"

Install SP Solution to all web applications, to the GAC and force it in
Install-SPSolution -Identity CodeMonkeySolution.wsp -allwebapplications -GACDeployment -force

Adding and Removing dll's from the GAC using Powershell

To add a .dll to the GAC

Run the SharePoint PowerShell console as Administrator

type : Set-location <path to dll's here>

[System.Reflection.Assembly]::Load("System.EnterpriseServices, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a")

$publish = New-Object System.EnterpriseServices.Internal.Publish

$publish.GacInstall( <path>\RandomcodemonkeyDLL.dll)


 
To remove a .dll from the GAC

$publish.GacRemove( <path>\RandomcodemonkeyDLL.dll)





A good way of getting the names of you assemblies from  :
 Simplest way to get the strong name of an assembly
http://nileshmandekar.blogspot.co.uk/2012/03/simplest-way-to-get-strong-name-of.html


A very elegant way of getting the dll's in and out via script is like this from Fred :
http://fredericloud.com/2011/01/08/no-gacutil-no-problem/