Skip to content

Instantly share code, notes, and snippets.

@mxrss
Last active January 3, 2016 12:39
Show Gist options
  • Save mxrss/8464680 to your computer and use it in GitHub Desktop.
Save mxrss/8464680 to your computer and use it in GitHub Desktop.
Powershell script to copy queues over for AWS
#make global to not inure this only once versus multiple times.
$aws_extensions = Get-Module -ListAvailable | where-object { $_.Name -eq "AWSPowerShell" }
<#
.SYNOPSIS
copy-awsqueue will allow you to copy messages from one queue to another without deleting
messages from teh queue.
.DESCRIPTION
Allows you to copy messages from one queue to another without deleting messages, make sure
however to set your $creds variable and $regions varaiable where you want this script to execute
You can set the following with AWS Powershell Snapin which is required to be installed prior to running this script.
If you do not have AWS powershell or the newest version of the framework it will send you to a link
to get the powershell extensions for visual studio.
#>
function copy-awsqueue(){
Param(
[Parameter(Mandatory=$true)]
[string]$sourceQueue,
[Parameter(Mandatory=$true)]
[string]$destinationQueue,
[Parameter(Mandatory=$true)]
[Object]$region,
[Parameter(Mandatory=$true)]
[Amazon.Runtime.AWSCredentials]$cred,
[Parameter(Mandatory=$false)]
[int]$NumberOfMessages,
[Parameter(Mandatory=$false)]
[int]$SecondsToHide
)
if ($aws_extensions -eq $null){
write-error "AWS Extenions must be installed"
_awswebsite
throw
}
if ($aws_extensions.Version.Major -lt 2){
write-err "You need to update you aws extenstions"
_awswebsite
throw
}
if ($NumberOfMessages -eq 0){
$NumberOfMessages = 10
}
if ($SecondsToHide -eq 0){
$SecondsToHide = 60
}
Get-SQSQueueUrl -OutVariable sourceQueueTemp -QueueName $sourceQueue -Region $region -Credentials $cred
Get-SQSQueueUrl -OutVariable destinationQueueTemp -QueueName $destinationQueue -Region $region -Credentials $cred
if ($sourceQueueTemp[0] -eq $null ){
Write-Error "The Source queue does not exist"
throw
}
if ($destinationQueueTemp[0] -eq $null ){
Write-Error "The Destination queue does not exist"
throw
}
$sourceQueueUrl = $sourceQueueTemp[0]
$destinationQueueUrl = $destinationQueueTemp[0]
$messagesCount = 0
$percentComplete = 0
$activity = "Copying From $sourceQueue to $destinationQueue"
while ($messagesCount -lt $NumberOfMessages){
$dequeSize = $NumberOfMessages
if ($NumberOfMessages -gt 10){
$dequeSize = 10
}
# grab the messages
$messages = Receive-SQSMessage -MaxNumberOfMessages $dequeSize -QueueUrl $sourceQueueUrl -Credentials $cred -Region $region
$entries = @()
write-progress -Activity $activity -CurrentOperation "Recieving messages from $sourceQueue" -PercentComplete $percentComplete
foreach ($message in $messages){
$entry = New-Object Amazon.SQS.Model.ChangeMessageVisibilityBatchRequestEntry
$entry.Id = $message.MessageId
$entry.ReceiptHandle = $message.ReceiptHandle
$entry.VisibilityTimeout = $SecondsToHide
$entries += $entry
}
foreach($messageToSend in $messages){
Send-SQSMessage -MessageBody $messageToSend.Body -QueueUrl $destinationQueueUrl -Region $region -Credentials $cred
}
Write-Host $entries.count
$entries
Edit-SQSMessageVisibilityBatch -Entries $entries -Credentials $cred -Region $region -QueueUrl $sourceQueueUrl
$messagesCount += $messages.Count
Write-Progress -PercentComplete $percentComplete -Activity $activity -CurrentOperation "Sending to $destinatioNQueue"
$percentComplete = ($messagesCount / $NumberOfMessages) * 100;
$entries= $null
}
}
function _awswebsite(){
$IE=new-object -com internetexplorer.application
$IE.navigate2("http://aws.amazon.com/powershell/")
$IE.visible=$true
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment