Konstantin Vlasenko

An engineer is someone who can make for a dollar what any fool could make for two. – Alan Kay

Tag Archives: PowerShell

Find region from within EC2 instance by #PowerShell

(ConvertFrom-Json (Invoke-WebRequest -Uri http://169.254.169.254/latest/dynamic/instance-identity/document -UseBasicParsing).Content).region

AWS EC2 Windows current spot price by PowerShell

Get-EC2SpotPriceHistory -ProductDescription Windows -InstanceType m1.large, cc1.4xlarge -s (Get-Date -format s) | sort SpotPrice | format-list

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

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=1.0.0.0, 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
$def.Update()

#PowerShell, #SharePoint: Enable DocumentSet content type

Enable-SPFeature -Identity DocumentSet -url http://YourSharepointServer

#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 $_}

Terminate EC2 Spot/On-Demand instance by name

Declare function to terminate instance by Id

function ec2-terminate-instance($instid){
$ec2.TerminateInstances((new-object Amazon.EC2.Model.TerminateInstancesRequest).WithInstanceId($instid)).TerminateInstancesResult.TerminatingInstance[0]
}

Get instance by name and terminate it

$filter = (new-object Amazon.EC2.Model.Filter).WithName('tag:Name').WithValue('YourInstanceName')
(Get-EC2Instance -Filter $filter -Region 'us-east-1').RunningInstance | % { ec2-terminate-instance $_.InstanceId }

Stop EC2 On-Demand instance by name

$filter = (new-object Amazon.EC2.Model.Filter).WithName('tag:Name').WithValue('YourInstanceName')
(Get-EC2Instance -Filter $filter -Region 'us-east-1').RunningInstance | % { Stop-EC2Instance $_.InstanceId -Region 'us-east-1'}
Follow

Get every new post delivered to your Inbox.