Friday, 11 April 2014

Email functionality with Power Shell

There are multiple methods to send an emails using power shell script, below are the few methods i used to follow.......

Use below script to send test email from Power Shell...


$To = "SPAdmin@org.com" #change your email id to send an email
$fromemail = "SPReports@org.com" #Add your from email address
$server = "smtp.org.com"  #change your SMTP address
$TodayDate = Get-Date

send-mailmessage -from $fromemail -to $users -subject "Test Reports" -body "Test Report for $TodayDate" -priority Normal -smtpServer $server


Permissions for Power Shell



Getting Started SharePoint 2013 Management Shell

Click Start, click Microsoft SharePoint 2013 Products, and then click SharePoint 2013 Management Shell

Permissions

Before you can use the Add-SPShellAdmin cmdlet to grant permissions for users to run SharePoint 2013 cmdlets, verify that you meet all of the following minimum requirements:


  •  You must have membership in the securityadmin fixed server role on the SQL Server instance
  • You must have membership in the db_owner fixed database role on all databases that are to be updated.
  • You must be a member of the Administrators group on the server on which you are running the Windows PowerShell cmdlet.
  •   If you do not have membership in the SharePoint_Shell_Access role or WSS_Admin_WPG local group, use the Add-SPShellAdmin cmdlet to add the WSS_Admin_WPG group in all front-end web servers in the SharePoint farm and the SharePoint_Shell_Access role. If the SQL Server database does not have a SharePoint_Shell_Access role, the role is automatically created when you run the Add-SPShellAdmin cmdlet. After you run the Add-SPShellAdmin cmdlet, users can run SharePoint Windows PowerShell cmdlets in a multiple-server farm environment.
  •  When you install SharePoint 2013, the user account from which you run the installation is granted the appropriate permissions to run Windows PowerShell cmdlets. If any users have not been added to run a Windows PowerShell cmdlet, you can use the Add-SPShellAdmin cmdlet to add them.
  •  You must run the Add-SPShellAdmin cmdlet for all databases to which you want to grant access. If no database is specified, the farm configuration database is used. If you do specify a database, the farm content database will be included in addition to the farm configuration database that you specify.
  • To see a list of all of the SPShellAdmin cmdlets, from a Windows PowerShell command prompt, type Get-Command -Noun SPShellAdmin or Get-SPShellAdmin
Reference: http://technet.microsoft.com/en-us/library/ee806878%28v=office.15%29.aspx 

Configure ISE in SharePoint Server



The Windows PowerShell Integrated Scripting Environment (ISE) is a host application for Windows PowerShell. In Windows PowerShell ISE,

  • You can run commands and write, test, and debug scripts in a single Windows-based graphic user interface with multiline editing, tab completion, syntax coloring, selective execution, context-sensitive help, and support for right-to-left languages.
  •  You can use menu items and keyboard shortcuts to perform many of the same tasks that you would perform in the Windows PowerShell console.  For example, when you debug a script in the Windows PowerShell ISE, to set a line breakpoint in a script, right-click the line of code, and then click Toggle Breakpoint. 
  •  PowerShell is a great companion for SharePoint 2010 and 2013. As a developer or Administrator you can go through the Integrated Scripting Environment (ISE) to write scripting in best way. The only problem is, the SharePoint snap-in doesn't load automatically when you start PowerShell ISE.

Below are the steps to get ISE installed and make sure the SharePoint snap-in always loads.

1. Open Server Manager
2. Click the Features node
3. Click the Add Features link
4. Check the box for "Windows PowerShell Integrated Scripting Environment".
5. Click Next, then Install
6. Wait until installation is complete
7. Open Powershell ISE from Programs>Accessories>PowerShell folder
8. Run the following code from the immediate window in ISE to create a new profile for all users
 if (!(test-path $profile.AllUsersAllHosts))

       {new-item -type file -path $profile.AllUsersAllHosts-force}

9. Run the following code to edit the new profile
psEdit $profile.AllUsersAllHosts
10. When profile1.ps1 opens, add the following code to attach the SharePoint snap-in every time PowerShell is run.
 If ((Get-PSSnapIn -Name Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue) -eq $null )

{ Add-PSSnapIn -Name Microsoft.SharePoint.PowerShell }
 11. Save profile.ps1 and close PowerShell ISE.
12. Start PowerShell ISE again and type the following command to verify that the SharePoint snap-in loaded. You should see SharePoint-specific command listed.

Get-Command Get-SP*


Reference: http://technet.microsoft.com/en-us/library