forked from beatcracker/Powershell-Misc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Add-ClusterMsmqRole.ps1
155 lines (122 loc) · 5.53 KB
/
Add-ClusterMsmqRole.ps1
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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
<#
.Synopsis
Creates clustered MSMQ role.
.Description
Creates clustered MSMQ role with correct group type and dependencies.
Can optionally add services to the created group.
.Parameter Name
Role name.
.Parameter Disk
Cluster disk name to use for shared storage.
Must exist and be available.
.Parameter StaticAddress
Array of IP addresses that should be added to the group.
.Parameter Services
Array of Windows services that should be added to the group.
.Parameter Cluster
Cluster name to operate on. Netbios/FQDN.
If the input for this parameter is omitted, then the all operations are run on the local cluster.
.Parameter Network
Cluster network name.
If the input for this parameter is omitted, first available network will be used.
.Parameter Start
Start role group after it's been created.
.Example
Add-ClusterMsmqRole -Name 'MSMQ' -Disk 'Cluster Disk 1' -StaticAddress '10.20.30.40' -Start
Create new MSMQ role with network name 'MSMQ' and IP address '10.20.30.40' using 'Cluster Disk 1' for shared storage.
Start 'MSMQ' group after it's been created.
.Example
Add-ClusterMsmqRole -Name 'MSMQ' -Disk 'Cluster Disk 1' -StaticAddress '10.20.30.40' -Service 'SomeService'
Create new MSMQ role with network name 'MSMQ' and IP address '10.20.30.40' using 'Cluster Disk 1' for shared storage.
Add windows service 'SomeService' to 'MSMQ' group. Do not start 'MSMQ' group.
#>
function Add-ClusterMsmqRole {
[CmdletBinding()]
Param (
[Parameter(Mandatory = $true, Position = 0)]
[ValidateNotNullOrEmpty()]
[string]$Name,
[Parameter(Mandatory = $true, Position = 1)]
[ValidateNotNullOrEmpty()]
[string]$Disk,
[Parameter(Mandatory = $true, Position = 2)]
[ValidateNotNullOrEmpty()]
[ipaddress[]]$StaticAddress,
[Parameter(Position = 3)]
[ValidateNotNullOrEmpty()]
[string[]]$Service,
[Parameter(Position = 4)]
[ValidateScript({
Get-Cluster -Name $_
})]
[string]$Cluster = '.',
[Parameter(Position = 5)]
[string]$Network,
[Parameter(Position = 6)]
[switch]$Start
)
Begin {
# Set default cluster name
$PSDefaultParameterValues = @{
'*-Cluster*:Cluster' = $Cluster
}
}
End {
# Using $Network = if ($Network){...} breaks pipeline somehow
if ($Network) {
[Microsoft.FailoverClusters.PowerShell.ClusterNetwork]$Network = Get-ClusterNetwork -Name $Network
} else {
# Using Select-Object stops pipeline too...
[Microsoft.FailoverClusters.PowerShell.ClusterNetwork]$Network = @(Get-ClusterNetwork)[0]
}
Write-Verbose "[*] Cluster network: $Network"
[Microsoft.FailoverClusters.PowerShell.ClusterResource]$Disk = Get-ClusterResource -Name $Disk |
Where-Object {
$_.ResourceType -eq 'Physical Disk' -and
$_.OwnerGroup -eq 'Available Storage'
}
if (-not $Disk) {
throw "Cluster disk '$Disk' is not available!"
}
Write-Verbose "[*] Creating group: $Name"
$Group = Add-ClusterGroup -Name $Name -GroupType Msmq
$StaticAddressList = foreach ($ip in $StaticAddress) {
$ipName = "IP Address $ip"
Write-Verbose "[*] Adding '$ipName' to '$Group' using '$Network'"
$Group |
Add-ClusterResource -Name $ipName -ResourceType 'IP Address' |
Set-ClusterParameter -Multiple @{
Network = $Network.Name
Address = "$ip"
SubnetMask = $Network.AddressMask
} | Write-Verbose
}
Write-Verbose "[*] Moving cluster disk '$Disk' to group '$Group'"
$Disk | Move-ClusterResource -Group $Group.Name | Write-Verbose
Write-Verbose "[*] Adding '$Name (Network Name)' to group '$Group'"
$NetworkName = $Group | Add-ClusterResource -Name $Name -ResourceType 'Network Name'
Write-Verbose "[*] Setting 'DnsName' on '$NetworkName (Network Name)'"
$NetworkName | Set-ClusterParameter -Name 'DnsName' -Value $Name | Write-Verbose
$NetworkNameDep = (
$StaticAddressList | ForEach-Object {"([$_])"}
) -join ' and '
Write-Verbose "[*] Adding dependencies to '$NetworkName (Network Name)': $NetworkNameDep"
$NetworkName | Set-ClusterResourceDependency -Dependency $NetworkNameDep | Write-Verbose
$MsmqName = "$Name-MSMQ"
Write-Verbose "[*] Adding resource of type 'Msmq' to group '$Group': $MsmqName"
$Msmq = $Group | Add-ClusterResource -Name $MsmqName -ResourceType Msmq
$MsmqDep = "([$Disk]) and ([$NetworkName])"
Write-Verbose "[*] Adding dependencies to '$Msmq (MSMQ)': $MsmqDep"
$Msmq | Set-ClusterResourceDependency -Dependency $MsmqDep | Write-Verbose
foreach ($svc in $Service) {
Write-Verbose "[*] Adding resource of type 'Generic Service' to group '$Group': $svc"
$Group |
Add-ClusterResource -Name $svc -ResourceType 'Generic Service' |
Set-ClusterParameter -Name 'ServiceName' -Value $svc | Write-Verbose
}
if ($Start) {
Write-Verbose "[*] Starting group: $Group"
$Group | Start-ClusterGroup | Write-Verbose
}
}
}