Showing posts with label powershell. Show all posts
Showing posts with label powershell. Show all posts

Sunday, June 22, 2014

Cancel Workflow Instances using powershell script

Hi All,

Recently, I worked on the requirement to terminate the running workflow instances associtated to the specific list using powershell script.

I used WorkflowManager and then Cancel method with passiing the workflow instance class,

Here  I terminated the workflows for which the workflow status is running.

here is the script.

 
#Site URL
$siteurl=Read-Host "Enter Site URL"
$web = Get-SPWeb $siteurl
$web.AllowUnsafeUpdates = $true;    

#List Name
$listname=Read-Host "Enter list Name"
$list = $web.Lists["$listname"];

if($list -ne $null)
{
# Iterate through all Items in List and all Workflows on Items.         
foreach ($item in $list.Items) {
foreach ($wf in $item.Workflows) {

#Cancel Workflows      

if($wf.InternalState - eq 'Running')
{  
[Microsoft.SharePoint.Workflow.SPWorkflowManager]::CancelWorkflow($wf);      
}
}
}
$web.Dispose();
}
else
{
 write-host "List does not exist"
}

Specific feature Activated sites report using Powershell

Hi All,

when you got the requirement to know the list of site collections where a specific feature got activated at the site collection level, the below script will be helpful.

For example, to know the list of site collection where the Nintex wokrflow got activated, use the below script.

 
$GC = Start-SPAssignment

$sites=$GC | Get-SPSite -Limit All


foreach($site in $sites)

{

$feature=Get-SPFeature -Site $site | Where-object {$_.DisplayName -eq "NintexWorkflow"}

if($feature -ne $null)
{
  write-host "Feature activated at site" $site.Url

}
$site.dispose()
}

Stop-SPAssignment $GC
 
If you want the spcific publishing feature or custom feature activated site collection list, replace the feature display name with the custom one.
 
 

Tuesday, September 24, 2013

SharePoint Health Analyser automated mail using Powershell

Hi guys,

Got a requirement to send the farm health report to the farm adminstrators group to get the updates automatically throgh the mail.

After some research, I found the basic power shell script which pulls the data from Health Reports list in the central administration web application

I modified and updated the script to get the severity incidents in the HTML tabular form ,

We can execute the script with task scheduler for automation.

Sunday, January 20, 2013

SharePoint Content DB size mismatch

Hi, Once you enable the quota limit in central admin for a site collection and do check the _layouts/storman.aspx page, you are able to see the site storage space. But, you may find some difference in the actual content db size and the statistics shown in the above page. To recalculate it again, use the below power shell script. $site=get-spsite -identity http://sitecollectionurl $site.RecalculateStorageUsed()

Saturday, January 19, 2013

Back up the farm using power shell

I got a requirement from my client to take the backup of the farm on a weekly basis using automated scheduler.

we do have a standard powershell cmdlet, that helps us in taking the back up of farm using backup-spfarm cmdlet.

We could do the job on a dialy or weekly basis with power shell script and windows task scheduler.

After I searched for some time , I got an excellent blog from http://sharepointpolice.com/ .

Here is the same script , I used for my farm.

Sunday, October 14, 2012

Powershell startup

Hi Guys,

I started learning powershell scriping recently and I noted my learning updates here for quick reference

  1.  To add powershell snap-in
           Add-PSSnapin Microsoft.Sharepoint.Powershell

If you are unable to add snapin to the powershell console, you may not have sufficient permissions to do the scripting.

Here are the steps to get the permissions to run the scripts.

You should have access to sharepoint shell configuration database and here is the cmdlet.

Add-SPShellAdmin -username contoso\user1

You have to perform the above step by using farm administrator privileges. The above command adds the user name specified to the Sharepoint_shell_access role on the configuration database.

If you want to give permission to a specific content database for a webapplication, specify the database parameter
Add-SPShellAdmin -username contoso\user1 -database guid



   2.    To get help on any command,
           help Get-SPSite

    3.   To get examples on the cmdlet.

          Get-SPSite -examples

4.        To get information about the site,

          Get-SPSite http://litware | select owner, Name, Description

5.       To know about the features in a site collection

         Get-SPFeature -site http://litware
6.      To activate the features

         Enable-SPFeature -identity GUID -url http://litware

7.    To install the feature,

        Install-SPFeature featurename
        e.g Install-SPFeature  MyCustomFeature
       Before installing the feature, make sure that, the feature files are deployed correctly in 14 hive features    folder.

8.     To add the solution to the farm,
       Add-SPSolution solutionname
       e.g. Add-SPSolution  "D:\mycustomsolution.wsp"

9.    To deploy the solution to the GAC
        Install-SPSolution -identity mycustomsolution.wsp -webapplication http://litware  -GACdeployment

 10. To update the solution
       Update-SPSolution -identiy mycustomsolution.wsp -literalpath "d:\mycustomsolution.wsp" -GACdeployment

11. To uninstall the solution from the web application
      Uninstall-SPSolution -identity mycustomsolution.wp -webapplication http://litware

12. To remove the solution from farm
      Remove-SPSolution -identity mycustomsolution.wsp

Below is the example
Powershell script to install a feature for all the site collections in a farm with known GUID

Add-PSSnapin microsoft.sharepoint.powershell
$webapplications=Get-SPWebapplication
foreach($webapp in $webapplications)
{
$id=$webapp.id;
write-host "processing webapplication with name, id" + $webapp.Name, $id

foreach($site in $webapp.Sites)
{
$name=$site.Name;
write-host "processing site collection -" + $name
foreach($web in $site.AllWebs)
{
write-host "processing web" +$web.title
$features=$web.features;
if($web.features[GUID] -eq $null)
{
enable-spfeature -identity GUID -url $web.url -confirm:$false
}

}
}

}

Sunday, July 1, 2012

Audience compilation status not idle

Hi Guys,

We had a situation recently in our 2010 server, the audience compilation job status is always compiling and not changed to idle. After I tried several ways to investigate the issue, there is no luck.

After I tried in Google, found the below information to solve the issue.


Open SharePoint Management Shell

Note: Run as administrator

Execute:

$runjob=[Microsoft.Office.Server.Audience.AudienceJob]::RunAudienceJob($args);

From the output copy the Application ID of the respective User Profile Service Application

For e.g., f4c2b7e2-ce7b-465d-868f-66164af37013

The Audience Compilation is preceded by following executable: AudienceJob.exe and we can force stop or start the compilation using PowerShell.

To stop execute:

Audiencejob.exe f4c2b7e2-ce7b-465d-868f-66164af37013 0

Note: Application ID and 0 or 1 indicates Stop or Start

After we successfully stopped the audience job we then started a full compilation executing following

Audiencejob.exe f4c2b7e2-ce7b-465d-868f-66164af37013 1



MOSS 2007: Power shell script for finding sub sites memory

Hi Guys,

I had a requirement from my client to provide them the report contains all the sub sites memory consumption in a site collection.
In Moss 2007, we don't have any OOTB to find the memory consumption for each sub site.

After tried in Google, I found the below power shell script that could be useful .


 function GetWebSizes ($StartWeb)  
 {  
   $web = Get-SPWeb $StartWeb  
   [long]$total = 0  
   $total += GetWebSize -Web $web  
   $total += GetSubWebSizes -Web $web  
   $totalInMb = ($total/1024)/1024  
   $totalInMb = "{0:N2}" -f $totalInMb  
   $totalInGb = (($total/1024)/1024)/1024  
   $totalInGb = "{0:N2}" -f $totalInGb  
   write-host "Total size of all sites below" $StartWeb "is" $total "Bytes,"  
   write-host "which is" $totalInMb "MB or" $totalInGb "GB"  
   $web.Dispose()  
 }  


 function GetSubWebSizes ($Web)  
 {  
   [long]$subtotal = 0  
   foreach ($subweb in $Web.GetSubwebsForCurrentUser())  
   {  
     [long]$webtotal = 0  
     foreach ($folder in $subweb.Folders)  
     {  
       $webtotal += GetFolderSize -Folder $folder  
     }  
     write-host "Site" $subweb.Title "is" $webtotal "Bytes"  
     $subtotal += $webtotal  
     $subtotal += GetSubWebSizes -Web $subweb  
   }  
   return $subtotal  
 }  

 function GetFolderSize ($Folder)  
 {  
   [long]$folderSize = 0   
   foreach ($file in $Folder.Files)  
   {  
     $folderSize += $file.Length;  
   }  
   foreach ($fd in $Folder.SubFolders)  
   {  
     $folderSize += GetFolderSize -Folder $fd  
   }  
   return $folderSize  
 }  

After you run the above scripts together, you can call it with the following PowerShell command: GetWebSizes -StartWeb

The start url could be any subsite url, that may not be a top level site.

Note: The script is written om SharePoint 2010 version, by changing the Get-SPWeb cmdlet to 2007 version ( like $site=new object Microsoft.Sharepoint.SPSite('ur'))  ,  it could work well.


Thursday, March 8, 2012

sharepoint 2007 power shell script to delete old versions

Recently I had a requirement from customer to delete old versioned documents in the document library to get the free space.

Since share point saves each version as a new copy with occupying the memory in database server, we should keep the limit on major and minor versions to consume the memory space on the database server.

Its very cumbersome to manually go and delete each version of the document in the document library


Below power shell script allows to delete all the old version copies from document library.

 [System.Reflection.Assembly]::Load(”Microsoft.SharePoint, Version=12.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c”)
$siteurl="http://siteurl";
$spsite=new-object Microsoft.Sharepoint.SPSite("$siteurl");
$spweb=$spsite.OpenWeb("$weburl");
$splist=$spweb.GetList("/sites/mydocumentlibray");
foreach($splistitem in $splist.items){
$file=$splistitem.File;
$file.Versions.DeleteAll();
}
$spweb.dispose();
$spsite.Dispose();
write-host ">>>finished at "(get-date) `n;


If you want to keep the limited versions in a document library,
the below power shell script will help you.

 [System.Reflection.Assembly]::Load("Microsoft.Sharepoint,Version=12.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c”)
$siteurl="http://siteurl";
$listname="TempEmailDocument";
$spsite=new-object Microsoft.Sharepoint.SPSite("$siteurl");
$spweb=$spsite.OpenWeb();
$splist=$spweb.Lists["$listname"];
$listitems=$splist.items;
$VersionsToKeep=2;
foreach($item in $listitems)
{
$currentVersion=$item.Versions.Count;
if($currentVersion -gt $VersionsToKeep)
{
for($index=$currentVersion-1; $index -gt $VersionsToKeep-1;$index-- )
{
$file=$item.Versions[$index].ListItem.File;
$file.Versions[$index].Delete();
$splist.Update();
write-host "deleting ...." + $item.Name +$file.Versions[$index].VersionLabel+ "...";
}
}
}
$spweb.Dispose();
$spsite.Dispose();
write-host ">>>finished at "(get-date) `n;

In the above code, the number of limited versions are 2.