Freitag, 27. Januar 2012

performance counter registry hive consistency -> failed

I had the proeblem, when installing SQL server, that the performance counter registry hive consistency failed.
Error
Searched the internet quite a lot, but then found the correct solution.
http://geekswithblogs.net/robz/archive/2008/08/10/possible-performance-counter-corruption-or-performance-counters-are-just-disabled.aspx

You will have to go into the registry-editor and make there some changes.

1. Go to the HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Perflib and check there in the subfolders, if your language is existing. There seems to be a strange calculation for the LCID.
Take the usual LCID, trim it to the last three digits and then replace the first with 0 and you have the folder-name.
E.g. German (Germany) = LCID 0407 ... trim to the last three digits = 407 ... then replace the first one with 0 = 007. That means, for Germany/German you will need to have a folder called 007 there.
E.g. English (US) = LCID 0409 = 409 = 009 folder-name
Find a list of LCIDs here:
http://msdn.microsoft.com/en-us/goglobal/bb964664

Registry folder structure

2. If your language folder is there, then go into it and you will need to have two keys in there: Counter and Help.
Just in case you don´t, then create them as multiline strings and copy the values from the 009 folder keys in there.
I also had one case, where I had to set the 'counter' and 'help' keys into every language folder.
Change value

3. Then go into both keys and check the last entry value. Usually both seem to incremental. In my case Counter had 2178 and Help 2179.
Check for values

4. Write down the values and go into the root folder and place those values as a decimal value into the keys Last Counter and Last Help.
Modify key Last Counter and Last Help

This change made it work in my case.

Donnerstag, 26. Januar 2012

Powershell script how to run a timer-job

If you want to run a specific timer-job, you can use this code (replace the value in the %%):

$myJob = Get-SPTimerJob "%Add the timer-job-name in here%"
$myJob.RunNow()

Powershell script how to add a field to a list

There might be the need for you to add a field to a list. Here is a script how to manage. Replace the values in %% with your values:

$isRequired = $%enter the boolean value in here%
$listName = "%enter your list-name%"
$siteUrl = "%enter the url of your site%"
$fieldName = "%enter the field-name%"
$fieldtype = "%enter the field-type%"

[void][System.reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint")
$site = new-object Microsoft.SharePoint.SPSite($siteUrl)
$web = $site.rootweb
$spList = $web.Lists[$listName]
if($spList -ne $null)
{
$spList.Fields.Add($fieldName,$fieldtype,$isRequired)
}
$web.Dispose()
$site.Dispose()

Powershell script how to create a list

You might want to create a list with Powershell. Here is some code you can use (Replace the values in % with your values):

[void][System.reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint")
$siteUrl = %your site-url%
$site = new-object Microsoft.SharePoint.SPSite($siteUrl)
$web = $site.rootweb
$templateType = $web.ListTemplates["Custom List %or add another list-template%"]
$web.Lists.Add("%enter the list-name you want to use%", "%enter the description%", $templateType)
$web.Dispose()
$site.Dispose()

Freitag, 20. Januar 2012

Powershell script how to change a property of list(s) or page(s)

Sometimes you might have to change a property of a list or page in a site-collection. Maybe the amount of items is so large, that handling this manually would take ages to do.
Here is a script, where I change all the list-titles to add the " 1" to it.

$sites = Get-SPSite
foreach($site in $sites)
{
$web = $site.RootWeb
foreach($list in $web.lists)
{
$title = $list.Title
$list.Title = "$title 1"
$list.Update()
}
}


This code changed now the title of each list in your web-pplications to it's origin name plus the string " 1". You can also check for a specific site-collection to make the changes at:
$sites = Get-SPSite
foreach($site in $sites)
{
if($site.Url -eq "%url of the site-collection you want to make the changes in%")
{
$web = $site.RootWeb
foreach($list in $web.lists)
{
$title = $list.Title
$list.Title = "$title 1"
$list.Update()
}
}
}

Powershell script how to add a webpart to a page

This script in no. 1 will add a webpart to a single page, no 2 will iterate through a site-collection and add the webpart to each sub-site/page. Please change the values in the %%- marked areas according to your settings/environment. Best practice would be to copy the code into PowerGui or any power-shell editor to see the code structured and the comments highlighted.

1. Adding a webpart to a single SPWeb
#load the assembly of your webpart
[void][reflection.assembly]::LoadWithPartialName("%your webpart assembly-name%")

#this code will do the publishing of the file, if publishing/versioning is disabled, you will not need this code
function PublbishSPFile([Microsoft.SharePoint.SPFile] $spfile)
{
if ($spfile.Level -eq [Microsoft.SharePoint.SPFileLevel]::Checkout)
{
$spfile.CheckIn("%your comment%", [Microsoft.SharePoint.SPCheckInType]::MajorCheckin)
}
if ($page.ListItem.ParentList.EnableModeration)
{
if($page.ListItem.ModerationInformation.Status -eq [Microsoft.SharePoint.SPModerationStatusType]::Pending)
{
$spfile.Approve("%your comment%")
}
if($page.ListItem.ModerationInformation.Status -eq [Microsoft.SharePoint.SPModerationStatusType]::Draft)
{
$spfile.Publish("%your comment%")
}
}
}

#you have to get the web, where you want to add the webpart, you can also include everything into a function and call the function, when iterating, this is explained in no. 2
$site = Get-SPSite -Identity "%url of the site%"
$web = $site.RootWeb

#first you have to check out the file/page, if publishing/versioning is disabled, then you can skip this step
$file = $web.GetFile("/Pages/%your page%.aspx")
$file.CheckOut()
#initialize the webpart-manager and locate it to the page
$webpartmanager = $web.GetLimitedWebPartManager("%complete url%/Pages/%your page%.aspx", [System.Web.UI.WebControls.WebParts.PersonalizationScope]::Shared)

$webpart = new-object %add the object/class with namespace here, that corresponds to your webpart (e.g. Mynamespace.MyWebpartClass)%
$webpart.Title = "%title of the webpart%"
$webpart.ChromeType = [System.Web.UI.WebControls.WebParts.PartChromeType]::None;
#if your webpart does need some properties to be set, do it now
$webpartmanager.AddWebPart($webpart, "Zone1",0); #the code in the brakets adds the $webpart to the mentioned zone and sets the sorting of the webpart on the first place
$webpartmanager.Dispose();
#last but not least you have to check in the file/page, if publishing/versioning is disabled, then you can skip this step
PublbishSPFile($file)


2. Adding a webpart to multiple SPWebs
#load the assembly of your webpart
[void][reflection.assembly]::LoadWithPartialName("%your webpart assembly-name%")

#this code will do the publishing of the file, if publishing/versioning is disabled, you will not need this code
function PublbishSPFile([Microsoft.SharePoint.SPFile] $spfile)
{
if ($spfile.Level -eq [Microsoft.SharePoint.SPFileLevel]::Checkout)
{
$spfile.CheckIn("%your comment%", [Microsoft.SharePoint.SPCheckInType]::MajorCheckin)
}
if ($page.ListItem.ParentList.EnableModeration)
{
if($page.ListItem.ModerationInformation.Status -eq [Microsoft.SharePoint.SPModerationStatusType]::Pending)
{
$spfile.Approve("%your comment%")
}
if($page.ListItem.ModerationInformation.Status -eq [Microsoft.SharePoint.SPModerationStatusType]::Draft)
{
$spfile.Publish("%your comment%")
}
}
}

#I used the code from no 1 and enclosed it into a function, that will be called in an iteration
function AddWebpart([string] $web)
{
#first you have to check out the file/page, if publishing/versioning is disabled, then you can skip this step
$file = $web.GetFile("/Pages/%your page%.aspx")
$file.CheckOut()
#initialize the webpart-manager and locate it to the page
$webpartmanager = $web.GetLimitedWebPartManager("%complete url%/Pages/%your page%.aspx", [System.Web.UI.WebControls.WebParts.PersonalizationScope]::Shared)

$webpart = new-object %add the object/class with namespace here, that corresponds to your webpart (e.g. Mynamespace.MyWebpartClass)%
$webpart.Title = "%title of the webpart%"
$webpart.ChromeType = [System.Web.UI.WebControls.WebParts.PartChromeType]::None;
#if your webpart does need some properties to be set, do it now
$webpartmanager.AddWebPart($webpart, "Zone1",0); #the code in the brakets adds the $webpart to the mentioned zone and sets the sorting of the webpart on the first place
$webpartmanager.Dispose();
#last but not least you have to check in the file/page, if publishing/versioning is disabled, then you can skip this step
PublbishSPFile($file)
}

$site = Get-SPSite -Identity "[url of the site]"
foreach($subsite in $site.AllWebs)
{
AddWebpart $subsite
}

Powershell script how to change the master-page

If you want to change your master-page in your whole site-collection with all sub-sites, then you can use this powershell-script to manage this quite easily (change the %%-values according to your settings or needs):

$site = Get-SPSite -Identity "%url of the site%"
foreach($subsite in $site.AllWebs)
{
$subsite.AllowUnsafeUpdates = "True"
$subsite.CustomMasterUrl = "/_catalogs/masterpage/%my master-page%.master"
$subsite.Update()
$subsite.Dispose()
}