#PowerShell #SCOM Preserve/Update Created By/Modified By

This example shows how to use SharePoint Client Object API through PowerShell to create a list item with specific Author and Editor.
Useful if you want to preserve identities while moving the SharePoint items.

$siteUrl = "http://mysite"
$listTitle = "My List"
$user = "9;#domain\user"


cd "$installFolder"
Add-Type -Path .<PATH>\Microsoft.SharePoint.Client.dll
$ctx = new-object Microsoft.SharePoint.Client.ClientContext($siteUrl)
$web = $ctx.Web
$list = $web.Lists.GetByTitle($listTitle)

$itemInfo = new-object Microsoft.SharePoint.Client.ListItemCreationInformation
$item = $list.AddItem($itemInfo)
$item['Title'] = "Hello World"
$item['Author'] = $user
$item['Editor'] = $user
$item.Update()
$ctx.ExecuteQuery()

The tricky part here is the $user value. It should be in the form of: {user Id in SharePoint};#{user Name}
UserId
You can automate the process of getting the user Id number by Name.

Enable SQL protocol by using PowerShell

A stand-alone installation of SharePoint usually utilizes SQL Server Express. The instance name is SHAREPOINT. (This is true for SharePoint 2013). You must enable TCP protocol to access the SharePoint back-end.
SQLTCP

PS> function EnableSQLProtocol($instance, $proto)
{
[reflection.assembly]::LoadWithPartialName("Microsoft.SqlServer.Smo") | out-null
[reflection.assembly]::LoadWithPartialName("Microsoft.SqlServer.SqlWmiManagement") | out-null
$smo = 'Microsoft.SqlServer.Management.Smo.'
$wmi = new-object ($smo + 'Wmi.ManagedComputer').
$uri = "ManagedComputer[@Name='$(hostname)']/ServerInstance[@Name='$instance']/ServerProtocol[@Name='$proto']"
$p = $wmi.GetSmoObject($uri)
$p.IsEnabled = $true
$p.Alter()
}

PS> 'Tcp','Np' | % {EnableSQLProtocol SHAREPOINT $_}