forked from RamblingCookieMonster/RabbitMQTools
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRemove-RabbitMQConnection.ps1
More file actions
102 lines (78 loc) · 3.39 KB
/
Remove-RabbitMQConnection.ps1
File metadata and controls
102 lines (78 loc) · 3.39 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
<#
.Synopsis
Closes connection to RabbitMQ server.
.DESCRIPTION
The Remove-RabbitMQConnection allows for closing connection to the RabbitMQ server. This cmdlet is marked with High impact.
To close connections to the remote server you need to provide -HostName parameter.
You may pipe an object with names and, optionally, with computer names to close multiple connection. For more information how to do that see Examples.
The cmdlet is using REST Api provided by RabbitMQ Management Plugin. For more information go to: https://www.rabbitmq.com/management.html
.EXAMPLE
Remove-RabbitMQConnection conn1
This command closes connection to local RabbitMQ server named "conn1".
.EXAMPLE
Remove-RabbitMQConnection c1, c1
This command closes connections to local RabbitMQ server named "c1" and "c2".
.EXAMPLE
Remove-RabbitMQConnection c1 -HostName myrabbitmq.servers.com
This command closes connection c1 to myrabbitmq.servers.com server.
.EXAMPLE
@("c1", "c2") | Remove-RabbitMQConnection
This command pipes list of connection to be closed. In the above example two connections named "c1" and "c2" will be closed.
.EXAMPLE
$a = $(
New-Object -TypeName psobject -Prop @{"HostName" = "localhost"; "Name" = "c1"}
New-Object -TypeName psobject -Prop @{"HostName" = "localhost"; "Name" = "c2"}
New-Object -TypeName psobject -Prop @{"HostName" = "127.0.0.1"; "Name" = "c3"}
)
$a | Remove-RabbitMQConnection
Above example shows how to pipe both connection name and Computer Name to specify server.
The above example will close two connection named "c1" and "c2" to local server, and one connection named "c3" to the server 127.0.0.1.
.INPUTS
You can pipe connection names and optionally HostNames to this cmdlet.
.LINK
https://www.rabbitmq.com/management.html - information about RabbitMQ management plugin.
#>
function Remove-RabbitMQConnection
{
[CmdletBinding(SupportsShouldProcess=$true, ConfirmImpact="High")]
Param
(
# Name of RabbitMQ connection.
[parameter(Mandatory=$true, ValueFromPipeline=$true, ValueFromPipelineByPropertyName=$true, Position=0)]
[Alias("ConnectionName")]
[string[]]$Name = "",
# Name of the computer hosting RabbitMQ server. Defalut value is localhost.
[parameter(ValueFromPipelineByPropertyName=$true)]
[Alias("HostName", "hn", "cn")]
[string]$BaseUri = $defaultComputerName,
# Credentials to use when logging to RabbitMQ server.
[Parameter(Mandatory=$false)]
[PSCredential]$Credentials = $defaultCredentials
)
Begin
{
$cnt = 0
}
Process
{
if (-not $pscmdlet.ShouldProcess("server: $BaseUri", "Close connection(s): $(NamesToString $Name '(all)')")) {
foreach ($qn in $Name)
{
Write "Closing connection $qn to server=$BaseUri"
$cnt++
}
return
}
foreach($n in $Name)
{
$url = Join-Parts $BaseUri "/api/connections/$([System.Web.HttpUtility]::UrlEncode($n))"
$result = Invoke-RestMethod $url -Credential $Credentials -DisableKeepAlive -ErrorAction Continue -Method Delete
Write-Verbose "Closed connection $n to server $BaseUri"
$cnt++
}
}
End
{
if ($cnt > 1) { Write-Verbose "Closed $cnt connection(s)." }
}
}