Friday, January 22, 2010

Deleting a WebPart from a Page using PowerShell

I had come across a requirement where I had to automate the deletion of the webpart from the page and not from Webpart Gallery. I have written a PowerShell script that takes care of the above requirement. WebPart.txt file contains the name of the webparts that needs to be deleted from the page.

-------------------

[System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint")

[System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint.WebPartPages")
[System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint.Publishing")

$url = "http://myserver"
$WebPageUrl = "Pages/Default.aspx"
$wpNames = Get-Content "c:\Powershell\webparts.txt"
$SPSite = New-Object Microsoft.Sharepoint.SPSite($url)
$SPWeb = $SPSite.OpenWeb()
$SPWeb.Title

foreach ($wpName in $wpNames)
{
$SPWebPart = $SPWeb.GetWebPartCollection($WebPageUrl,
[Microsoft.SharePoint.WebPartPages.Storage]::Shared)


$SPWebPart | select Title
$count = $SPWebPart.Count

for ($k = 0; $k -lt $count; $k++)
{
    $wp = $SPWebPart[$k]


        if ($wp.Title -eq $wpName)


          {


           $SPWeb = $SPSite.OpenWeb()
           $pubweb = [Microsoft.Sharepoint.Publishing.PublishingWeb]::GetPublishingWeb($SPWeb)
           $pubweb
           $d = $pubweb.GetPublishingPages()
           $SPPage = $d.get_Item($WebPageUrl)
           $Page = $SPPage.ListItem.File

          if ($Page.Level -eq [Microsoft.SharePoint.SPFileLevel]::Checkout)
            {
             $SPWeb.CurrentUser.LoginName
             $Page.UndoCheckOut()
             $Page.CheckOut()
             $SPWebPart = $SPWeb.GetWebPartCollection($WebPageUrl,[Microsoft.SharePoint.WebPartPages.Storage]::Shared)


            $SPWebPart.Delete($wp.StorageKey)
            $SPWeb.Update()
            $SPPage.CheckIn("Checked in")
            $Page.Publish("Published")
            $SPWeb.Update()


Write-Host " $wpName Deleted"
}
else
{
   $SPPage.CheckOut()


$SPWebPart = $SPWeb.GetWebPartCollection   ($WebPageUrl,Microsoft.SharePoint.WebPartPages.Storage]::Shared)


$c = $wp.StorageKey
$c
$SPWebPart.Delete($c)
$SPWeb.Update()
$SPPage.CheckIn("Checked in")
$Page.Publish("Published")
$SPWeb.Update()


Write-Host "$wpName Deleted and Checked in"
}
}
else
{
Write-Host " $wpName not found"
}
}
}
$Page.Publish("Published")
$SPWeb.Update()

Deleting WebPart from a WebPart Gallery using PowerShell

Here I shall show you how to delete a webpart of a list of webparts from the WebPart Gallery of a particular Sharepoint Site. webparts.txt contains the list of webparts that needs to be deleted from the gallery. Note that the webpart name should be in the form of webpart1.webpart or webpart.dwp as in the url http://myserver/_catalogs/wp/Forms/AllItems.aspx 

-------------

[System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint")
[System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint.WebPartPages")
[System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint.Publishing")


$url = "http://myserver/"


$wps = Get-Content "c:\Powershell\webparts.txt"
$SPSite = New-Object Microsoft.SharePoint.SPSite($url)


foreach($wp in $wps)
{
$SPWeb = $SPSite.OpenWeb()
$WebPartGallery = $SPWeb.Lists["Web Part Gallery"]


$gall = $WebPartGallery.Items | select Name


$count = $gall.Count
$count
for ( $i =0; $i -lt $count;$i++)
  {
if ($gall[$i] -match $wp)
{
Write-Host "found"
$WebPartGallery.Items.Delete($i)
$SPWeb.Update()
Write-Host "$wp deleted"
}
  $SPWeb.Dispose()
  }
}
-------

Hope this helps someone....

Tuesday, January 19, 2010

Adding a Child node to an XML file using PowerShell

There has been a need to add a child node to an XML file. Doing that using powershell can be seen here
------
[xml]$Config = Get-Content "C:\tofolder\Files\myfolder\config.xml"
$element = $Config.CreateElement("MyKeyElement")
$element.setattribute("Value", "xxxyyyzzz")
$Config.get_firstChild().appendchild($element)
$Config.Save("C:\tofolder\Files\myfolder\config.xml")

---------
Thus, saving the config file appends the changes in the config file. Once you open the config.xml after running the above script you can see "MyKeyElement" element has been updated with its value in the config.xml file.


Hope this helps someone.

To find the port number of the Central administration using powershell

In Order to find the Central administration port number in MOSS 2007, you can run the below script to get the port number.
----
[void][reflection.assembly]::LoadWithPartialName("Microsoft.SharePoint")[Microsoft.SharePoint.Administration.SPAdministrationWebApplication]::Local.Sites.VirtualServer.Port

-----

In Order to get the same in SP2010, you can run the below powershell script

-----
[Microsoft.SharePoint.Administration.SPAdministrationWebApplication]::Local.Sites.VirtualServer.Port

----------


Also you can run the below powershell line. this shall give you the web application URLs of all web applications.

Get-SPWebApplication -IncludeCentralAdministration

 
Hope this helps..

Tuesday, January 12, 2010

copying of folders and subfolders from one location to another using powershell

There has been a need to copy folders and subfolders inside from one location to another. I have used powershell to do the same. First by creating a new folder and then copying the files, folders and sub folders inside that new folder.
-----------------------------
$server1 = "Server1"
$tofolder = "NewFolderName"
$fromfolder = "OldFolderName"
$frompath = "
\\server2\OldLocation"
$topath = "
\\$server1\c$"
New-Item -Name $tofolder -ItemType Directory -Path $topath
Copy-Item -Path $frompath\$fromfolder\* -Destination $topath\$tofolder -Force -Recurse
Write-Host Copying of the latest sharepoint build $tofolder is done

------------
Hope this helps...

Monday, January 4, 2010

finding out disk space and free space percentage for the servers using powershell

It is necessary for the server administrators to keep an eye on the disk space issues that come up now and then. Its nearly impossible to check the disk space by logging into the servers if there are about 50-100 of them, on a daily basis and verify the same. I have written a powershell script, that will help to keep a track of the disk space and also in terms of percentage the free space available and take actions accordingly.
------------------------------


del "C:\Powershell\lowdisk.xls"
echo "ServerName DriveName DriveSize FreeSpace PercentFree" >> "C:\Powershell\lowdisk.xls"
$computers = Get-Content "c:\Powershell\computerlistall.txt"
foreach ($computer in $computers)
{
$a = Get-WmiObject -ComputerName $computer -Class Win32_LogicalDisk Where-Object{$_.DriveType -eq 3}
if ($a.DeviceID -eq "C:")
{
$FSize = ($a.FreeSpace)/1GB
$TSize = ($a.Size)/1GB
$PerFree = ($FSize/$TSize)* 100
#echo "ServerName DriveName DriveSize FreeSpace PercentFree" >> "C:\Powershell\lowdisk.xls"
echo "$computer $driveName $TSize $FSize $PerFree" >> "C:\Powershell\lowdisk.xls"
}
else
{
$i = $a.Count
#echo "ServerName DriveName DriveSize FreeSpace PercentFree" >> "C:\Powershell\lowdisk.xls"
for ($t = 0)
{
$driveName = ($a[$t].DeviceID)
$FSize = ($a[$t].Freespace)/ 1GB
$TSize = ($a[$t].Size)/1GB
$PerFree = ($FSize/$TSize)* 100


echo "$computer $driveName $TSize $FSize $PerFree" >> "C:\Powershell\lowdisk.xls"
$t++
if ($t -eq $i)
{
break
}
}
}
if ($PerFree -lt "40.00")
{
Write-Host $PerFree -BackgroundColor Red >> "C:\Powershell\lowdisk.xls"
}
Write-Host "disk space check completed for " $computer
}
----------------