-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclearMSMQ.ps1
More file actions
109 lines (101 loc) · 2.91 KB
/
clearMSMQ.ps1
File metadata and controls
109 lines (101 loc) · 2.91 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
###
# remove old messages for all local msmq queues
# msmq = microsoft messaging queuing
#
# 20190530 122800
# add options $showMsg and $dryRun
# 20190521 122800
# initial
#
#
# config
#
[System.Reflection.Assembly]::LoadWithPartialName("System.Messaging") | Out-Null
$utf8 = new-object System.Text.UTF8Encoding
$now = Get-Date
# remove messages older than
# $old = $now.AddDays(-1)
$old = $now.AddMinutes(-10)
# show details of message to be removed
$showMsg = 1
# run without removing
$dryRun = 1
#
# run
#
# get queues
$queuePaths = Get-MsmqQueue -QueueType Private | Select -Property QueueName;
if ($dryRun)
{
echo "dry run; would be "
}
echo "removing old messages for all local msmq queues"
echo ""
echo "nbr queues: $($queuePaths.Length); checking messages older than $($old)"
$queueCounts = Get-MsmqQueue -QueueType Private | Format-Table -Property QueueName,MessageCount;
echo $queueCounts
echo ""
pause
foreach ($queuePath in $queuePaths)
{
# for proper permissions, prepend .\
$localQueuePath = ".\$($queuePath.QueueName)"
echo "queue: $localQueuePath"
$queue = New-Object System.Messaging.MessageQueue $localQueuePath
# to read ArrivedTime property
$queue.MessageReadPropertyFilter.SetAll()
# get snapshot of all messages, but uses memory, and slower
# $msgs = $queue.GetAllMessages()
# echo " $($msgs.Length) messages"
# get cursor to messages in queue
$msgs = $queue.GetMessageEnumerator2()
# add a message so can test
# $queue.Send("<test body>", "test label")
# pause
$removed = 0
# foreach ($msg in $msgs)
while ($msgs.MoveNext([timespan]::FromSeconds(1)))
{
$msg = $msgs.Current
if ($msg.ArrivedTime -and $msg.ArrivedTime -lt $old)
{
if ($showMsg)
{
echo "--------------------------------------------------"
echo "ArrivedTime: $($msg.ArrivedTime)"
echo "BodyStream:"
if ($msg.BodyStream)
{
echo $utf8.GetString($msg.BodyStream.ToArray())
}
echo "Properties:"
echo $msg | Select-Object
echo ""
}
try {
if (!$dryRun)
{
# receive ie remove message by id from queue
$queue.ReceiveById($msg.Id, [timespan]::FromSeconds(1))
}
$removed++
} catch [System.Messaging.MessageQueueException] {
$errorMessage = $_.Exception.Message
# ignore timeouts
if (!$errorMessage.ToLower().Contains("timeout"))
{
throw $errorMessage
}
}
}
}
if ($dryRun)
{
echo "dry run; would have "
}
echo "removed $removed messages from $localQueuePath"
echo ""
}
pause
#
###