Blog Image

PowerShell

SharePoint Powershell Commands

GoSharePoint Website
Documentation Blog

This blog is based on SharePoint 2013 PowerShell Commands. Please visit my other blog which contains SharePoint Documentation.

Connect to an existing Server Farm

Installation Posted on Mon, June 27, 2016 19:08:41

<#====================================================================

Copyright © 2015, June. Michael Pomfret

Connect to an existing Server Farm

You should replace the “<Server name>” with the correct windows server name.

You should replace the “<SharePoint Configuration DB>” with the correct SharePoint Configuration database name.

You should replace the “<YourPassPhase>” with the correct passphase.

====================================================================#>

cls

Write-Host “SharePoint 2013 – Connect to an existing Server Farm…”

$DBServer = ‘<Server name>’

$ConfigDB = ‘<SharePoint Configuration DB>’

$PassPhrase = ‘<YourPassPhase>’

$SecPassPhrase = ConvertTo-SecureString $PassPhrase –AsPlaintext –Force

Write-Host ” – Enabling SP PowerShell cmdlets…”

If ((Get-PsSnapin |?{$_.Name -eq “Microsoft.SharePoint.PowerShell”})-eq $null)

{

Add-PsSnapin Microsoft.SharePoint.PowerShell | Out-Null

}

Start-SPAssignment -Global | Out-Null

Write-Host ” – Connecting to server farm…”

Connect-SPConfigurationDatabase -DatabaseServer $DBServer -DatabaseName $ConfigDB -Passphrase $SecPassPhrase

Write-Host ” – Installing Help Collection…”

Install-SPHelpCollection -All

Write-Host ” – Securing Resources…”

Initialize-SPResourceSecurity

Write-Host ” – Installing Services…”

Install-SPService

Write-Host ” – Installing Features…”

$Features = Install-SPFeature –AllExistingFeatures -Force

Write-Host ” – Installing Application Content…”

Install-SPApplicationContent

Stop-SPAssignment -Global | Out-Null



Configure People Picker for cross-forest or cross-domain queries when you use a one-way trust

Installation Posted on Mon, June 27, 2016 18:56:28

<#====================================================================

Copyright © 2016, June. Michael Pomfret

Configure People Picker for cross-forest or cross-domain queries when you use a one-way trust

You should replace the “xxxxxx” with the correct passwords for the user.

Check and amend correct AD domain .

https://technet.microsoft.com/en-us/library/gg602075.aspx?f=255&MSPPError=-2147217396

====================================================================#>

Set-ExecutionPolicy -ExecutionPolicy “Unrestricted” -Force

Add-PSSnapin “Microsoft.SharePoint.PowerShell”

stsadm -o setapppassword -password xxxxxxx

STSADM -o setproperty -pn peoplepicker-searchadforests -pv “domain:yourdomain.com,AD\pBI_SP_Profile,8Pgk59sKH;forest:ad.domain.com,AD\xxx_SP_Profile,xxxxxxxxx” -url http://SharePoint_Site

STSADM -o setproperty -pn peoplepicker-searchadforests -pv “domain:yourdomain.com,AD\pBI_SP_Profile,8Pgk59sKH;forest:ad.domain.com,AD\xxx_SP_Profile,xxxxxxxxx” -url http://central_admin:port



Get the SharePoint variables from a text file

Installation Posted on Mon, June 27, 2016 18:47:55

<#====================================================================

Copyright © 2015, September. Michael Pomfret

Get the SharePoint variables from a text file

====================================================================#>

if ($AppPoolAcc -eq $null){

Set-ExecutionPolicy -ExecutionPolicy Unrestricted -Scope CurrentUser

# PowerShell script to get common Varaible from a file

$Data = “C:\temp\SP_Variables.txt”

$lines = Get-Content $Data | Where {$_.trim() -ne “” -and $_ -notmatch ‘#’}

foreach ($line in $lines) {

$variablename = $line -split ‘=’

if (!$variablename)

{ New-Variable -Name variablename[0] -Value $Global:variablename[1]}

else

{

set-Variable -Name $variablename[0] -Value $variablename[1]

}

}

}else

{

write “variables ok”

}



Create a new Server Farm

Installation Posted on Mon, June 27, 2016 18:46:25

<#====================================================================

Copyright © 2015, September. Michael Pomfret

Creating a new Server Farm

The script only targets the server you are running the script on

and does the following:

-Creates the SharePoint Configuration content database.

-Creates the SharePoint Administration content database.

-Install SharePoint services

-Install SharePoint features

-Copies shared application data to existing Web application folders.

====================================================================#>

Write-Host “- Enabling SP PowerShell cmdlets…”

If ((Get-PsSnapin |?{$_.Name -eq “Microsoft.SharePoint.PowerShell”})-eq $null)

{

Add-PsSnapin Microsoft.SharePoint.PowerShell | Out-Null

}

Write-Host “SharePoint 2013 – Creating a new Server Farm…”

$SecPassPhrase = ConvertTo-SecureString $PassPhrase –AsPlaintext –Force

$FarmAccPWD = ConvertTo-SecureString $FarmPassword –AsPlaintext –Force

$cred_FarmAcc = New-Object System.Management.Automation.PsCredential $FarmAcc,$FarmAccPWD

Start-SPAssignment -Global | Out-Null

Write-Host “====================================================================”

Write-Host “- Creating configuration database…”

Write-Host “- Using DBServer…”$DBServer

Write-Host “- Using Central Admin Content DB…”$CentralAdminContentDB

Write-Host “- Using Port…”$CentralAdminPort

Write-Host “====================================================================”

Write-Host

New-SPConfigurationDatabase –DatabaseName “$ConfigDB” –DatabaseServer “$DBServer” –AdministrationContentDatabaseName “$CentralAdminContentDB” –Passphrase $SecPassPhrase –FarmCredentials $cred_FarmAcc

Write-Host “- Installing Help Collection…”

Install-SPHelpCollection -All

Write-Host “- Securing Resources…”

Initialize-SPResourceSecurity

Write-Host “- Installing Services…”

Install-SPService

Write-Host “- Installing Features…”

$Features = Install-SPFeature –AllExistingFeatures -Force

Write-Host “- Creating Central Admin…”

$NewCentralAdmin = New-SPCentralAdministration -Port $CentralAdminPort -WindowsAuthProvider “NTLM”

Write-Host “- Waiting for Central Admin to provision…”-NoNewline

Write-Host ” Created!”

Write-Host “- Installing Application Content…”

Install-SPApplicationContent

Stop-SPAssignment -Global | Out-Null



Checks whether the script is running as admin

Installation Posted on Mon, June 27, 2016 06:27:42

#***************************************************************************************

# Written by Michael Pomfret

#

# This script checks whether the script is running as admin, if not then starts as admin.

#

If (-NOT ([Security.Principal.WindowsPrincipal][Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole] “Administrator”))

{

$arguments = “& ‘” + $myinvocation.mycommand.definition + “‘”

Start-Process powershell -Verb runAs -ArgumentList $arguments

Break

}

cls

Write-Host -f Cyan “Red ugly warning about set-executionPolicy..”

sleep 2

Write-Host -f Green “OK”

Write-Host “”



Disabling the Loopback Check

Installation Posted on Mon, June 27, 2016 06:26:17

#***************************************************************************************

# Written by Michael Pomfret

#

# This script disables the Loopback Check

#
New-ItemProperty HKLM:\System\CurrentControlSet\Control\Lsa -Name “DisableLoopbackCheck” -Value “1” -PropertyType dword



Download and installs SharePoint 2013 Prerequisites

Installation Posted on Mon, June 27, 2016 06:24:16

#***************************************************************************************

# Written by Michael Pomfret

#

# This script downloads and instals SharePoint 2013 Prerequisites

#

# -Only run this script on Windows Server 2012 (RTM, either Standard or Datacenter)

# -Do not run this script on a Windows Server 2008 R2 SP1 Server!

# —These are the Prerequisites for Windows Server 2012

# -Run this script as a local server Administrator

# -Run PowerShell as Administrator

#

# The SharePoint 2013 prerequisite installer (prerequisiteinstaller.exe) installs the following software, if it has not already been installed on the target server, in this order:

#

# 1.Microsoft .NET Framework version 4.5

# 2.Windows Management Framework 3.0

# 3.Application Server Role, Web Server (IIS) Role

# 4.Microsoft SQL Server 2008 R2 SP1 Native Client

# 5.Windows Identity Foundation (KB974405)

# 6.Microsoft Sync Framework Runtime v1.0 SP1 (x64)

# 7.Windows Identity Extensions

# 8.Microsoft Information Protection and Control Client

# 9.Microsoft WCF Data Services 5.0

# 10.Windows Server AppFabric

# 11.Cumulative Update Package 1 for Microsoft AppFabric 1.1 for Windows Server (KB 2671763)

#

# IMPORTANT NOTES:

# -The $SharePoint2013Path is the path to your SharePoint 2013 installation media

# -This script utilizes the PrerequisiteInstaller.exe in your SharePoint 2013 media

# -The script points to your $SharePoint2013Path\PrerequisiteInstallerFiles directory.

# —BE SURE TO COPY THE PREREQ FILES INTO THIS DIRECTORY IF THEY ARE NOT ALREADY THERE FROM THE Download-SP2013PreReqFiles.ps1 script

#

# Don’t forget to: Set-ExecutionPolicy RemoteSigned

# If you have not done so already within you Windows Server 2012 server

#****************************************************************************************

param([string] $SharePoint2013Path = $(Read-Host -Prompt “Please enter the directory path to where your SharePoint 2013 installation files exist.”))

function InstallPreReqFiles()

{

$ReturnCode = 0

Write-Host “”

Write-Host “=====================================================================”

Write-Host “Installing Prerequisites required for SharePoint 2013”

Write-Host “”

Write-Host “This uses the supported installing offline method”

Write-Host “”

Write-Host “If you have not installed the necessary Roles/Features”

Write-Host “this will occur at this time.”

Write-Host “=====================================================================”

Try

{

Start-Process “$SharePoint2013Path\PrerequisiteInstaller.exe” -ArgumentList “`

/SQLNCli:`”$SharePoint2013Path\PrerequisiteInstallerFiles\sqlncli.msi`” `

/NETFX:`”$SharePoint2013Path\PrerequisiteInstallerFiles\NDP451-KB2858728-x86-x64-AllOS-ENU.exe`” `

/IDFX:`”$SharePoint2013Path\PrerequisiteInstallerFiles\Windows6.1-KB974405-x64.msu`” `

/IDFX11:`”$SharePoint2013Path\PrerequisiteInstallerFiles\MicrosoftIdentityExtensions-64.msi`” `

/Sync:`”$SharePoint2013Path\PrerequisiteInstallerFiles\Synchronization.msi`” `

/AppFabric:`”$SharePoint2013Path\PrerequisiteInstallerFiles\WindowsServerAppFabricSetup_x64.exe`” `

/KB2671763:`”$SharePoint2013Path\PrerequisiteInstallerFiles\AppFabric1.1-RTM-KB2671763-x64-ENU.exe`” `

/MSIPCClient:`”$SharePoint2013Path\PrerequisiteInstallerFiles\setup_msipc_x64.msi`” `

/WCFDataServices:`”$SharePoint2013Path\PrerequisiteInstallerFiles\WcfDataServices.exe`”

/PowerShell:`”$SharePoint2013Path\PrerequisiteInstallerFiles\Windows6.1-KB2506143-x64.msu`””

}

Catch

{

$ReturnCode = -1

Write-Error $_

break

}

return $ReturnCode

}

function CheckProvidedSharePoint2013Path()

{

$ReturnCode = 0

Try

{

# Check if destination path exists

If (Test-Path $SharePoint2013Path)

{

# Remove trailing slash if it is present

$script:SharePoint2013Path = $SharePoint2013Path.TrimEnd(‘\’)

$ReturnCode = 0

}

Else {

$ReturnCode = -1

Write-Host “”

Write-Warning “Your specified download path does not exist. Please verify your download path then run this script again.”

Write-Host “”

}

}

Catch

{

$ReturnCode = -1

Write-Warning “An error has occurred when checking your specified download path”

Write-Error $_

break

}

return $ReturnCode

}

function InstallPreReqs()

{

$rc = 0

$rc = CheckProvidedSharePoint2013Path

# Install the Pre-Reqs

if($rc -ne -1)

{

$rc = InstallPreReqFiles

}

if($rc -ne -1)

{

Write-Host “”

Write-Host “Script execution is now complete!”

Write-Host “”

}

}

InstallPreReqs

New-ItemProperty HKLM:\System\CurrentControlSet\Control\Lsa\MSV1_0 -Name “BackConnectionHostNames” -value