Thursday, July 28, 2011

Powershell script to use RunAs for SharePoint Add-Ins

I am in a environment where I want to use a Service Account ( say Farm account) for doing any command line task (for eg, add, install, retract solutions ….). In MOSS 2007, we used simple runas different user command to execute STSAM.

How am I going to achieve this, to use my lovely SharePoint Shell..

Anyway, i don't want to use Powershell profile because of some restrictions i have.

here is something I did..

1) Create a Powershell script with below entries and save it with RunAs.ps1

runas /user:domain\useraccount D:\MaintenanceScripts\Powershell\Runas\SPShell.Bat



2)Create a batch file named SPShell.Bat (you can name it as you wanted and ensure you update the same in the above RunAs.PS1) with the below content.

C:\Windows\system32\windowspowershell\v1.0\PowerShell.exe -NoExit " & ' C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\14\CONFIG\POWERSHELL\Registration\\sharepoint.ps1 ' "


Procedure to execute

1) Have both the files in a common folder
2) Create a shortcut of RunAs.ps1 to your desktop
3) Now double click on your RunAs.PS1 and work is done :)

If RunAS.PS1 opens in a notepad, use the following step.

i) Right click RunAs.PS1 and say open with and select Powershell, ensure you have selected "Always use the Selected program to open this kind of file"


Now you are ready to execute the SharePoint Shell as a different user (Farm admin).

Hope this helped you..

Sunday, July 3, 2011

Powershell script to monitor multiple SharePoint / IIS Sites

###########################################################################This script helps you in monitoring the availablity of your hosted sites
# and triggers an email for any IIS failures.
# It reads a file and executes the function for each URL's in the URL.txt # file
# written by KannaGanesh 03July11
###########################################################################

function SiteMonitor {
param($URL)
trap{
"Failed.Details: $($URL) $($_.Exception)"
$emailFrom = ""
# Seperate email addresses with comma's
$emailTo = ""
$subject = "$($URL) is un available"
$body = "$($URL) site is down around $endTime

Error Details

$($_.Exception)"
$smtpServer = ""
$smtp = new-object Net.Mail.SmtpClient($smtpServer)
$smtp.Send($emailFrom, $emailTo, $subject, $body)
}
$webclient = New-Object Net.WebClient
$webclient.Credentials = [System.Net.CredentialCache]::DefaultCredentials
}

$startTime = get-date
$endTime = get-date
$webclient.DownloadString($URL) | Out-Null
}
$readfile = Import-Csv D:\MaintenanceScripts\URL.txt
foreach ($line.URL in $readfile){
sitemonitor $line.URL }

Points to Note
1) Ensure you are able to access those sites from the machine where this script will be scheduled
2) Provide the proper file location in $readfile, the URL.txt should be listing all url's under column URL. Something like below.
URL
http://1234.com
http://5678.com
3) This script reports problem based on any IIS errors.

Disadvantage

This script is not supporting for Web Application hosted on Claims Authentication.

Wish you all the best !