1
+ # ----------------------------------------------------------------------------------
2
+ #
3
+ # Copyright Microsoft Corporation
4
+ # Licensed under the Apache License, Version 2.0 (the "License");
5
+ # you may not use this file except in compliance with the License.
6
+ # You may obtain a copy of the License at
7
+ # http://www.apache.org/licenses/LICENSE-2.0
8
+ # Unless required by applicable law or agreed to in writing, software
9
+ # distributed under the License is distributed on an "AS IS" BASIS,
10
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11
+ # See the License for the specific language governing permissions and
12
+ # limitations under the License.
13
+ # ----------------------------------------------------------------------------------
14
+
15
+ <#
16
+ . SYNOPSIS
17
+ Tests to query load balancer health per rule..
18
+ #>
19
+ function Test-LoadBalancerHealthPerRule
20
+ {
21
+ # Setup
22
+ $rgname = Get-ResourceGroupName
23
+ $vnetName = Get-ResourceName
24
+ $subnetName = Get-ResourceName
25
+ $publicIpName = Get-ResourceName
26
+ $lbName = Get-ResourceName
27
+ $lbRuleName = Get-ResourceName
28
+ $probeName = Get-ResourceName
29
+ $frontendName = Get-ResourceName
30
+ $backendAddressPoolName = Get-ResourceName
31
+ $rglocation = Get-ProviderLocation ResourceManagement
32
+ $resourceTypeParent = " Microsoft.Network/loadBalancers"
33
+ $location = Get-ProviderLocation $resourceTypeParent
34
+ $vmUsername = " azureuser"
35
+ $vmPassword = ConvertTo-SecureString " $vmUsername @123" - AsPlainText - Force
36
+ $ipConfigName = Get-ResourceName
37
+ $vmsize = " Standard_A2"
38
+ $ImagePublisher = " MicrosoftWindowsServer"
39
+ $imageOffer = " WindowsServer"
40
+ $imageSKU = " 2019-Datacenter-GS"
41
+
42
+ try
43
+ {
44
+ # Create the resource group
45
+ $resourceGroup = New-AzResourceGroup - Name $rgname - Location $rglocation - Tags @ { testtag = " testval" }
46
+
47
+ # Create the Virtual Network
48
+ $subnet = New-AzVirtualNetworkSubnetConfig - Name $subnetName - AddressPrefix 10.0 .1.0 / 24
49
+ $vnet = New-AzVirtualNetwork - Name $vnetName - ResourceGroupName $rgname - Location $location - AddressPrefix 10.0 .0.0 / 16 - Subnet $subnet
50
+
51
+ # Create the publicip
52
+ $publicip = New-AzPublicIpAddress - ResourceGroupName $rgname - name $publicIpName - location $location - AllocationMethod Static - sku Standard
53
+
54
+ # Create LoadBalancer
55
+ $frontend = New-AzLoadBalancerFrontendIpConfig - Name $frontendName - PublicIpAddress $publicip
56
+
57
+ $bepool = New-AzLoadBalancerBackendAddressPoolConfig - Name $backendAddressPoolName
58
+
59
+ $probe = @ {
60
+ Name = $probeName
61
+ Protocol = ' tcp'
62
+ Port = ' 80'
63
+ IntervalInSeconds = ' 360'
64
+ ProbeCount = ' 5'
65
+ }
66
+ $healthprobe = New-AzLoadBalancerProbeConfig @probe
67
+
68
+ $lbrule = @ {
69
+ Name = $lbRuleName
70
+ Protocol = ' tcp'
71
+ FrontendPort = ' 80'
72
+ BackendPort = ' 80'
73
+ IdleTimeoutInMinutes = ' 15'
74
+ FrontendIpConfiguration = $frontend
75
+ BackendAddressPool = $bePool
76
+ Probe = $healthprobe
77
+ }
78
+ $rule = New-AzLoadBalancerRuleConfig @lbrule - EnableTcpReset - DisableOutboundSNAT
79
+
80
+ $loadbalancer = @ {
81
+ ResourceGroupName = $rgName
82
+ Name = $lbName
83
+ Location = $rglocation
84
+ Sku = ' Standard'
85
+ FrontendIpConfiguration = $frontend
86
+ BackendAddressPool = $bePool
87
+ LoadBalancingRule = $rule
88
+ Probe = $healthprobe
89
+ }
90
+ $lb = New-AzLoadBalancer @loadbalancer
91
+
92
+ $cred = New-Object System.Management.Automation.PSCredential ($vmUsername , $vmPassword );
93
+
94
+ $RdpPublicIP = New-AzPublicIpAddress - Name " RdpPublicIP" - ResourceGroupName $resourceGroup.ResourceGroupName - Location $resourceGroup.Location - AllocationMethod Static - Sku Standard - IpAddressVersion IPv4
95
+
96
+ # both LB pool and Instance Level public ip for RPD
97
+ $Ip4Config = New-AzNetworkInterfaceIpConfig - Name $ipConfigName - Subnet $vnet.subnets [0 ] - PrivateIpAddressVersion IPv4 - LoadBalancerBackendAddressPool $bepool - PublicIpAddress $RdpPublicIP
98
+
99
+ $NIC = New-AzNetworkInterface - Name " NIC" - ResourceGroupName $resourceGroup.ResourceGroupName - Location $resourceGroup.Location - NetworkSecurityGroupId $nsg.Id - IpConfiguration $Ip4Config
100
+
101
+ $vmName = " myVM"
102
+ $VMconfig = New-AzVMConfig - VMName $vmName - VMSize $vmsize
103
+ | Set-AzVMOperatingSystem - Windows - ComputerName $vmName - Credential $cred - ProvisionVMAgent 3> $null
104
+ | Set-AzVMSourceImage - PublisherName $ImagePublisher - Offer $imageOffer - Skus $imageSKU - Version " latest" 3> $null
105
+ | Set-AzVMOSDisk - Name " $vmName .vhd" - CreateOption fromImage 3> $null | Add-AzVMNetworkInterface - Id $NIC.Id 3> $null
106
+
107
+ $VM = New-AzVM - ResourceGroupName $resourceGroup.ResourceGroupName - Location $resourceGroup.Location - VM $VMconfig
108
+
109
+ # install IIS (port 80)
110
+ $ext = @ {
111
+ Publisher = ' Microsoft.Compute'
112
+ ExtensionType = ' CustomScriptExtension'
113
+ ExtensionName = ' IIS'
114
+ ResourceGroupName = $rgName
115
+ VMName = " myVM"
116
+ Location = $region
117
+ TypeHandlerVersion = ' 1.8'
118
+ SettingString = ' {"commandToExecute":"powershell Add-WindowsFeature Web-Server; powershell Add-Content -Path \"C:\\inetpub\\wwwroot\\Default.htm\" -Value $($env:computername)"}'
119
+ }
120
+ Set-AzVMExtension @ext - AsJob
121
+
122
+ # List
123
+ $list = Get-AzLoadBalancerRuleHealth - ResourceGroupName $rgname - LoadBalancerName $lbName - Name $lbruleName
124
+
125
+ Assert-True { @ ($list.LoadBalancerBackendAddresses ).Count -eq 1 }
126
+ Assert-AreEqual $list.LoadBalancerBackendAddresses [0 ].IpAddress " 10.0.1.4"
127
+ Assert-True { $list.Down.Count -eq 1 }
128
+ }
129
+ finally
130
+ {
131
+ # Cleanup
132
+ Clean - ResourceGroup $rgname
133
+ }
134
+ }
0 commit comments