Konstantin Vlasenko

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

Tag Archives: PowerShell

#PowerShell script to wait while any MSI process is running

   $mutex = [System.Threading.Mutex]::OpenExisting('Global\_MSIExecute')
   $mutex = $null
   Sleep 10
   'MSI running...' | Out-Default
 catch [System.Threading.WaitHandleCannotBeOpenedException]
   # Mutex not found; MSI isn't running

Disable the option to check for server certificate revocation on Internet Explorer

We are doing an extensive acceptance testing against SharePoint Online by using PowerSlim (PowerShell).
Unfortunately sometimes our automated tests are blocked by popup below:
As we truly believe that our DNS is not spoiled an we know exactly what we are looking for (pre-created data) – this verification doesn’t make any sense for us.
So we decided to disable it. I was not able to find the solution how to do this trough Windows Registry. Below is how you can do this trough Internet Explorer:

  1. Open Internet Options
  2. Go to Advanced tab
  3. Scroll down to the Security section
  4. Clear check-box below:
  5. Restart Internet Explorer

#PowerShell: Register AWS EC2 instances in Amazon Route53 (new way)

Amazon Route 53 is a highly available and scalable Domain Name System (DNS) web service.
Just in case – Amazon EC2
I use this script https://github.com/konstantinvlasenko/cloud/blob/master/Register-CNAME.ps1 to provide meaningful names for them.
Simple usage:

$config = @{ DomainName = 'mylab.com' }
$name = "www.$($config.DomainName)"
# get instances
$instance = (Get-EC2Instance $InstanceId).RunningInstance
# update R53
.\Register-CNAME.ps1 $config $name $instance.PublicIpAddress

Advanced usage (register in another account; registering A record):

$config = @{ DomainName = 'mylab.com'; AssumeRoles = @{ R53 = @{ ARN = 'arn:aws:iam::600021112340:role/Route53'; SessionName = 'Friends' } }; }
$name = "www.$($config.DomainName)"
# get instances
$instance = (Get-EC2Instance $InstanceId).RunningInstance
# update R53
.\Register-CNAME.ps1 $config $name $instance.PublicIpAddress 'A'

Find region from within EC2 instance by #PowerShell

(ConvertFrom-Json (Invoke-WebRequest -Uri -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

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, #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

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.


Get every new post delivered to your Inbox.