Posts

Showing posts from September, 2017

Powershell Scripts 2

Image
Lets see continuation of my previous PowerShell series. This time will start from the basic commands. Load Assembly references in PowerShell Declare Global Variables in PowerShell Pass/Read Command Line arguments Convert DateTime based on regional settings in PowerShell Read RESX value in PowerShell Perform CAML Query in PowerShell Add SharePoint Snapin if (( Get-PSSnapin "Microsoft.SharePoint.PowerShell" -ErrorAction SilentlyContinue) -eq $null ) { Add-PSSnapin "Microsoft.SharePoint.PowerShell" -ErrorAction SilentlyContinue } Load default/custom Assembly references [void][System.Reflection.Assembly] :: LoadWithPartialName( "System.Net" ) [void][System.Reflection.Assembly] :: LoadWithPartialName( "System.IO" ) [void][System.Reflection.Assembly] :: LoadWithPartialName( "System.Xml" ) [void][System.Reflection.Assembly] :: LoadWithPartialName( "System.Collections.Generic" ) [void][System.Reflection.Assem

List All Servers in SharePoint Farm - C#

Image
Some times we need to login to all the servers in SharePoint farm just to restart selected Application or Site Pool. Let's see a C# code how to retrieve and recycle selected pools from all the servers in our SharePoint farm. In this series will see How to retrieve all servers in SharePoint farm using C# PS: Please check the NOTE at the very end on this post. First things first import dll's using System ; using Microsoft.SharePoint ; using Microsoft.SharePoint.Administration ; using Microsoft.Web.Administration ; Declare global variables // Global enum Variables public enum Action { Start, Stop, Invalid } ; public enum PoolType { AppPool, SitePool } ; // Global Server variable public List<string> servers = null ; // Global Pool ' s collection variables public List<ApplicationPool> appPoolCollection = null ; public List<Site> siteCollection = null ; Get All SharePoint Farm server names public

PowerShell Scripts

Image
Let see some of the most useful PowerShell commands in SharePoint to perform below activities. Read SMTP Outgoing Mail Address from Central Administration Get/Set Primary and Secondary Site Administrator Remove user from Site Collection Admin Remove user from Site Groups Set user from direct web Roles Permissions Read SMTP Outgoing Mail Address from CA function LoadEmailConfig() { try { $caWebApp = ( Get-SPWebApplication -IncludeCentralAdministration) | ? { $_ .IsAdministrationWebApplication -eq $true } Write-Host ( "SmtpServer: " + $caWebApp .OutboundMailServiceInstance.Server.Address) Write-Host ( "FromAdress: " + $caWebApp .OutboundMailSenderAddress) } catch { WriteToLog ( "Trying to get eMail Configuration from Central Admin. Exception:- " + $_ .exception.message) } Get/Set Primary/Secondary Site Administrator function GetAdmins() { $site =