Saturday, August 11, 2012

PowerShell How to execute script file remotely on another machine

Scenario: You have a Books.ps1 file you want to execute the code in Books.ps1 file on another machine.How to do that?

Solution:
try
{

$securePassword= ConvertTo-SecureString "YourPassword" -AsPlainText -force

$cred = New-Object System.Management.Automation.PsCredential("Domain\username",$securePassword)

Invoke-Command -Authentication credssp -Credential $cred -FilePath "C:\Srini\Books.ps1" -ComputerName "Machine_FQDN"
}

catch

{
Write-Host $_
}

Friday, August 10, 2012

XML-PowerShell creating new child elements and attributes

Scenario: How to create new XML elements, attributes and write to a new/existing XML file using PowerShell

Solution:

$OutputXML = New-Object xml;

            [System.Xml.XmlDeclaration] $xmlDeclaration = $OutputXML.CreateXmlDeclaration("1.0", "UTF-16", $null);

            $OutputXML.AppendChild($xmlDeclaration) | Out-Null;


             #creates root element named Book

            $BookElement = $OutputXML.CreateElement("Book"); 

            $BookElement.SetAttribute("BookName", "Monk_Who_Sold_His_Ferrari");   

                  $OutputXML.AppendChild($BookElement) | Out-Null;


                  #creates and adds Author element as child element to Book

                  $AuthorElement = $OutputXML.CreateElement("Author"); 

                  $AuthorElement.InnerText = "Robin S Sharma";

                  $BookElement.AppendChild($AuthorElement)| Out-Null;

                  #Checks if the XML file exists else create a new XML file

                  $XMLFilePath = "c:\srini.xml"

                   if (-not (Test-Path $XMLFilePath))

                   {

                     New-Item $XMLFilePath  -Type File  | Out-Null;

                   }

                  $OutputXML.Save($XMLFilePath);
Output:
<?xml version="1.0" encoding="UTF-16"?>
<Book BookName="Monk_Who_Sold_His_Ferrari">
  <Author>Robin S Sharma</Author>
</Book>

Friday, August 3, 2012

Using Custom SharePoint Resource files in 2010

What: If you open any of the Sharepoint Pages or master pages most of the time you will see something similar to "$Resources:wss,language_value" but  have you ever wondered what it is and were it is coming from?

Solution: Its coming form a resource file  at 14 hive (14\Resources)
We can create our own resource files and store at this location. And we can reference it in our xml files, master pages, and aspx pages.

Once you have your custom *.resx file at 14\Resources, you can refer it as below in an aspx page
E.g. text=<%$Resource:myText%>

SharePoint2010 Get the list of Farm Scoped Features using object model

Scenario: How to programatically get the list of Farm Scoped features using object model

Solution: Getting the list of farm scoped features programatically is different than getting the list of site, web scoped features.
Here is how you can get the list of farm scoped features

SPFeatureCollection myFarmFeatures = mySite.WebApplication.WebService.Features;

With in Feature event handlers
using Microsoft.Sharepoint.Administration;

SPWebService svc = properties.Feature.Parent as SPWebService;

svc.Features.Add("Feature Guid");


foreach (var feature in svc.Features)
{
  //your code here           

} 

 "svc.Features.Add" will activate the feature at farm scope.