Konstantin Vlasenko

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

Tag Archives: PowerShell

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'}

New-ASLaunchConfiguration with IAM Role

This post is good addition to my previous post Schedule your Amazon EC2 spot instance startup time.
You can slightly modify call to New-ASLaunchConfiguration cmdlet to use the powerfull IAM roles feature.
roles

New-ASLaunchConfiguration Fitnesse -ImageId ami-00000000 -IamInstanceProfile Fitnesse -SecurityGroups Fitnesse -InstanceType t1.micro -SpotPrice 0.011

Schedule your Amazon EC2 spot instance startup time

Here I want to explain how to start your Amazon EC2 instance recurrently by scheduling.


I have used the AWS Tools for Windows PowerShell. You can use java command line or python library. The main goal is to explain the idea:

I am going to automate the starting of my Fitnesse server.

Create launch configuration

New-ASLaunchConfiguration Fitnesse -ImageId ami-00000000 -SecurityGroups Fitnesse -InstanceType t1.micro -SpotPrice 0.011

Create Auto Scaling group with MinSize=MaxSize=0

New-ASAutoScalingGroup Fitnesse-AS -LaunchConfigurationName Fitnesse -AvailabilityZones us-east-1a -MinSize 0 -MaxSize 0

Set the Start scheduling action
The core thing is the MinSize=MaxSize=1 and the -Recurrence argument which is in the cron scheduler time format

Write-ASScheduledUpdateGroupAction -AutoScalingGroupName Fitnesse-AS -ScheduledActionName Start -MinSize 1 -MaxSize 1 -Recurrence '15 13 * * *'

Optionl (stop event)

Write-ASScheduledUpdateGroupAction -AutoScalingGroupName Fitnesse-AS -ScheduledActionName Stop -MinSize 0 -MaxSize 0 -Recurrence '59 23 * * *'

Installing AWS Tools for Windows PowerShell through PowerShell

AWS Tools for Windows PowerShell

(new-object System.Net.WebClient).DownloadFile('http://sdk-for-net.amazonwebservices.com/latest/AWSToolsAndSDKForNet.msi', ‘c:\downloads\AWSToolsAndSDKForNet.msi’)
(start c:\downloads\AWSToolsAndSDKForNet.msi '/qn' -wait -PassThru).ExitCode

HOWTO: Create AWS EC2 spot instance with the specific IAM Role

Get IAMInstanceProfile

$iam=[Amazon.AWSClientFactory]::CreateAmazonIdentityManagementClient($awsKeyId, $awsKey)
$req = (new-object Amazon.IdentityManagement.Model.GetInstanceProfileRequest).WithInstanceProfileName('S3Reader')
$res = $iam.GetInstanceProfile($req1).GetInstanceProfileResult
$iamprofile = (new-object Amazon.EC2.Model.IAMInstanceProfile).WithArn($res.InstanceProfile.Arn).WithId($res.InstanceProfile.InstanceProfileId)

Create LaunchSpecification

$spec = (new-object Amazon.EC2.Model.LaunchSpecification).WithInstanceProfile($iamprofile).WithSecurityGroup('your security group')
$spec.ImageId = 'ami0000000'
$spec.InstanceType = 'm1.small'

Create spot request

$ec2=[Amazon.AWSClientFactory]::CreateAmazonEC2Client($awsKeyId, $awsKey)
$req = new-object Amazon.EC2.Model.RequestSpotInstancesRequest
$req.InstanceCount = 1
$req.SpotPrice = '0.02'
$req.LaunchSpecification = $spec
$ec2.RequestSpotInstances($req).RequestSpotInstancesResult.SpotInstanceRequest[0].SpotInstanceRequestId

PowerShell: Wait till time service initiated synchronization with the time source (Windows 2008)

$ew = new-object system.management.ManagementEventWatcher                                                         
$ew.query = "Select * From __InstanceCreationEvent Where TargetInstance ISA 'Win32_NTLogEvent'"                   
while(!(get-eventlog -logname 'System' -Source 'Microsoft-Windows-Time-Service' | ? {$_.EventId -eq 35})){ $ew.WaitForNextEvent() }

PowerShell: Wait till time service initiated synchronization with the time source (Windows 2003)

$ew = new-object system.management.ManagementEventWatcher                                                         
$ew.query = "Select * From __InstanceCreationEvent Where TargetInstance ISA 'Win32_NTLogEvent'"                   
while(!(get-eventlog -logname 'System' -Source 'W32Time' | ? {$_.EventId -eq 35})){ $ew.WaitForNextEvent() }

Windows Core: Install PowerShell 3.0 by using PowerShell 2.0

Run the script below in the PowerShell 2.0 on your Windows Core

(new-object System.Net.WebClient).DownloadFile('http://download.microsoft.com/download/3/6/1/361DAE4E-E5B9-4824-B47F-6421A6C59227/dotNetFx40_Full_x86_x64_SC.exe', 'c:\dotNetFx40_Full_x86_x64_SC.exe')
.\dotNetFx40_Full_x86_x64_SC.exe
(new-object System.Net.WebClient).DownloadFile('http://download.microsoft.com/download/E/7/6/E76850B8-DA6E-4FF5-8CCE-A24FC513FD16/Windows6.1-KB2506143-x64.msu', 'c:\Windows6.1-KB2506143-x64.msu')
.\Windows6.1-KB2506143-x64.msu

It is not unattended installation. You will be prompted several times to click Next.
net45
net45

Follow

Get every new post delivered to your Inbox.