Showing posts with label Powershell. Show all posts
Showing posts with label Powershell. Show all posts

Saturday, August 17, 2013

Start User Profile Synchronization Service using PowerShell

Using Get-SPServiceInstance powershell commandlet you can get GUID details of all the Service instances running in your server. From that pick the GUID of your User Profile Synchronization service and use Start-SPServiceInstance commandlet to start the service in the server. 
 

Start-SPServiceInstance –Identity  GUID of the Service application
Sometimes your User Profile Synchronization service might stuck in “Starting” due to some issues in Synchronization. To Stop that service, use the below command
Stop-SPServiceInstance –Identity  GUID of the Service application

Wednesday, August 14, 2013

Triggering Workflow by Anonymous User in SharePoint Using Powershell

Problem: 
We have a Request for Information form that can be accessed anonymously and the anonymous users have Add and View privileges to the respective list. What we want is when the user submits the form to have a workflow kick off that sends an email to the user who submitted the form using the email address within the form data and also send an email to an administrator.
However, we are seeing some problems with anonymous users and workflow playing nice together.
·         If a user is logged in (windows authentication), then the form works as expected, the data is stored into the list, the user is directed to the appropriate page and the workflow runs.
·         If no workflow is tied to the list, the anonymous user can submit their request for information as expected. The data goes into the list and the user is directed to the appropriate page.
·         If a workflow is attached to the list, the user can still submit their request and the data makes it into the list, but the workflow is not started and remains with Failed on Starting error. 

Work Around:
Normally Anonymous Users doesn’t have permission to trigger a workflow automatically. Below are the steps to enable anonymous users to trigger workflow automatically.

1.       Create a user or use the existing user and make sure that user has given proper write permissions to the list, to which the workflow is attached.
2.       Through Powershell we can iterate through the items in the list by impersonating the anonymous user with the user mentioned in the first step.
3.       Copy the below code to the notepad and save it as “filename.ps1”, Change the SiteCollectionUrl, Site title and List name to the appropriate one.


         [System.reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint")
         Add-PSSnapin Microsoft.SharePoint.Powershell
         Start-SPAssignment -Global
         $Site = Get-SPSite http://servername:portnumber/
         $web = $Site.AllWebs | where { $_.title -eq "Site Title"}
                if ($web -ne $null)
                {             
                   $list =$web.Lists["List Name"]
                  if($list -ne $null)
                   {
foreach ($item in $list.Items | where {$_.Workflows.InternalState -ne "Completed"})
                             {
                                          $item.Update()
                              }
                               $list.Update()
                                   start-sleep 45                                                                                
                        }                                                     
                        $Web.Dispose()     
                }
         $Site.Dispose ()
         Stop-SPAssignment –Global
4.       Now we need to schedule this script in Task scheduler. Navigate to Start à Administrative tools à Task Scheduler 
5.       Click on the Create Task from the Actions pane in the right.


6.       Give the name of the job and the select the account which we mentioned in the first point which has the write permissions to the list.

7.       In the Triggers tab, create a schedule which suits the requirement.

8.       In the Actions tab, click new to specify the action.


9.       Select “Start a Program” from the dropdown and type Powershell.exe in the Program/Script.
Then in arguments, mention the path of the script file we saved in 3rd step.
Example:

-command "& 'D:\WorkflowAnonymous.PS1'"



10.   Once the Job has been scheduled, try to add a new item to the list using the anonymous access and wait for the scheduled job to run. Once the job has completed then check workflow status, Now the workflow has been triggered by anonymous access.



Friday, August 9, 2013

Empty SharePoint Recycle bin using PowerShell

Below script will delete all the Items from the Recycle bin and Site Collection recycle bin in SharePoint.

if ((Get-PSSnapin -Name Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue) -eq $null)
{
Add-PSSnapin Microsoft.SharePoint.PowerShell
}
[System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint") | Out-null

$sitecollectionUrl = http://sitename:portnumber
$siteCollection = New-Object Microsoft.SharePoint.SPSite($sitecollectionUrl)
write-host("Items to be deleted : " +$siteCollection.RecycleBin.Count.toString())
$now = Get-Date
write-host("Deleting started at " +$now.toString())
$siteCollection.RecycleBin.DeleteAll();
$now = Get-Date
write-host("Deleting completed at " +$now.toString())
$siteCollection.Dispose();

Get or Delete SharePoint List Item using Powershell

Below script will be helpful to get the SharePoint List Item with given Item ID.

Inputs:
  • Site URL
  • List Name
  • Item ID

if ((Get-PSSnapin -Name Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue) -eq $null)
{
Add-PSSnapin Microsoft.SharePoint.PowerShell
}
[System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint") | Out-null
$web = Get-SPWeb http://sitename:portnumber/
$listName = "Discussion"
$list = $web.Lists[$listName]
$items = $list.Items | Where {$_["ID"] -eq 741}
foreach($item in $items)
{
$item["ID"]
$item["Title"] = "Using Powershell"
$item.Update()
#Function comes here #
}


Add the below line inside above foreach to delete the List Item,
$item.Delete()



Delete SharePoint Timer Job using PowerShell

Below PowerShell script can be used to delete the Timer Jobs deployed in SharePoint.

Inputs:
Give the Timer Job name as input for your search.
  • Searching listing and find Timer job GUID to delete it .
                       Get-SPTimerJob | where { $_.name -like “*<your search criteria>*” } |ft id,name
  • Get the Timer job Object using the GUID we got in the previous command result and Set job to a variable
                     $job = Get-SPTimerJob -id <GUID>
  • And call the Delete() method to the Delete the timer job.
                     $job.Delete()

Execute SharePoint Timer Job using PowerShell

Below PowerShell script can be used to execute the Hidden Timer Jobs in Central Administration Timer Job Definitions list.

Inputs:
WebApplication url : url of the Web application to which the timer job is added.
Timer Job name : name of the Timer Job

$webApp = Get-SPWebApplication "http://site:portnumber/
$job = $webApp.JobDefinitions | Where-Object { $_.Name -eq "Timer Job name"}
$job.Execute([System.Guid].GUID)

Get SharePoint Timer job using PowerShell

Below PowerShell script can be used to identify the Hidden Timer Jobs in Central Administration Timer Job Definitions list.

Inputs:
WebApplication : name of the Web application to which the timer job is added.
Identity : name of the Timer Job
Get-SPTimerJob -WebApplication "SharePoint - 1234" -Identity "TimerJobName"

Include dependent dlls to SharePoint Solution Package

Steps to Include the Dependent dll’s to the Solution Package: 

1.      Open the Packages from the Solution explorer.

2.      Click the Advanced tab in the Packages sections.

3.      Click on Add à Add Existing Assembly.

4.      Select the dll to the deployed with the solution.

5.      Choose appropriate option in Deployment Target

·         GAC Deployment
·         WebApplication

6.      Add the Safe control entries if required.

7.      Click Ok to save the changes.

8.      Now the selected dll is included with the Solution and will be deployed into the GAC everytime when the solution is deployed.

Add List Item to SharePoint Discussion board using PowerShell


Discussion board is a predefined list template which is used to store the discussions and replies as List Items. But these discussion list Items are slightly varies from the normal list Item, were every top level Discussion Items are treated as Folder and all the replies are stored under those folder.  

Below is the code snippet to create a discussion item in Discussion board.

$site=Get-SPSite http://site/
$web=$site.OpenWeb()
$list=$web.Lists.TryGetList("ListName")
if($list -ne $null)
{
$newTopic = [Microsoft.SharePoint.Utilities.SPUtility]::CreateNewDiscussion($list, "Service Request");
$newTopic["Body"] = "Test";
$newTopic["Subject"] = "Service Request";
$newTopic["ColumnName"] = "Value";
$newTopic.Update();
Write-Host -ForegroundColor Green $newTopic.Title " discussion topic is created successfully"
}
else
{
Write-Host -ForegroundColor Yellow "List does not exists."
}


Related Posts Plugin for WordPress, Blogger...