PowerShell; SharePoint 2010: Set Alternate Access Mapping

I need to set Alternate Access Mapping remotely as part of my automation. I can’t find how to specify PowerShell version for Invoke-Command CMDlet. So I decided to use this workaround:

Invoke-Command -computer sp2010.vlasenko.org -script {
  $command = "Add-PSSnapin Microsoft.SharePoint.PowerShell; New-SPAlternateURL 'http://vlasenko.nmsp.org' -Zone 'Internet' -WebApplication 'SharePoint - 80'"
  PowerShell -v 2 -Command "& { $command }"

PowerShell: Register AWS EC2 instances in AWS R53

UPDATE: #PowerShell: Register AWS EC2 instances in Amazon Route 53 (new way)

Suppose you have a variable $lab:

$lab = @{id='ami-7300091a'; name='1.dc.mylab.com'; type='m1.small'; maxbid='0.02'; CNAME='ec2-54-000-117-7.compute-1.amazonaws.com'},
@{id='ami-49000e20'; name='1.sql.mylab.com'; type='cc1.4xlarge'; maxbid='0.09'; CNAME='ec2-184-73-000-182.compute-1.amazonaws.com'}

Let’s update AWS R53 entries by using instances CNAME:

$zoneId = 'ZB00X5FI00YBB'
foreach($computer in $lab){
  "[$($computer.name)]`t[R53] update... " | Out-Default
  $result = Get-R53ResourceRecordSet -HostedZoneId $zoneId -StartRecordName $computer.name -MaxItems 1
  $rs = $result.ResourceRecordSets[0]
  if($rs.Name -eq "$($computer.name).") {
    "[$($computer.name)]`t[R53] entry found" | Out-Default
    "[$($computer.name)]`t[R53] delete entry" | Out-Default
    $action = (new-object Amazon.Route53.Model.Change).WithAction('DELETE').WithResourceRecordSet($rs)
    Edit-R53ResourceRecordSet -HostedZoneId $zoneId -ChangeBatch_Changes $action | Out-Null
  } else { "[$($computer.name)]`t[R53] entry not found (returned entry is for $($rs.Name))" | Out-Default }
  "[$($computer.name)]`t[R53] create entry" | Out-Default
  $record = (new-object Amazon.Route53.Model.ResourceRecord).WithValue($computer.CNAME)
  $rs = (new-object Amazon.Route53.Model.ResourceRecordSet).WithName($computer.name).WithType('CNAME').WithTTL('10').WithResourceRecords($record)
  $action = (new-object Amazon.Route53.Model.Change).WithAction('CREATE').WithResourceRecordSet($rs)
  Edit-R53ResourceRecordSet -HostedZoneId $zoneId -ChangeBatch_Changes $action | Out-Null

Bind SharePoint event receiver by PowerShell

$list = (get-spweb http://sharepoint/sites/test).lists['somelist']
$def = $list.EventReceivers.Add()
$def.Assembly = "MyReceiverAssembly, Version=, Culture=Neutral,PublicKeyToken=a00000000a000ce0"
$def.Class = "MyReceiverAssembly.MyReceiverClass"
$def.Type = [Microsoft.SharePoint.SPEventReceiverType]::ItemAdded
$def.Name = "My ItemAdded Event Receiver";
$def.Synchronization = [Microsoft.SharePoint.SPEventReceiverSynchronization]::Synchronous

#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

The tricky part here is the $user value. It should be in the form of: {user Id in SharePoint};#{user Name}
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.

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

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