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+ # ################################
17+ # # IotHub Device Cmdlets ##
18+ # ################################
19+
20+ <#
21+ . SYNOPSIS
22+ Test all iothub device cmdlets
23+ #>
24+ function Test-AzureRmIotHubDeviceLifecycle
25+ {
26+ $Location = Get-Location " Microsoft.Devices" " IotHubs"
27+ $IotHubName = getAssetName
28+ $ResourceGroupName = getAssetName
29+ $Sku = " S1"
30+ $device1 = getAssetName
31+ $device2 = getAssetName
32+ $device3 = getAssetName
33+ $primaryThumbprint = ' 38303FC7371EC78DDE3E18D732C8414EE50969C7'
34+ $secondaryThumbprint = ' F54465586FBAF4AC269851424A592254C8861BE7'
35+
36+ # Create Resource Group
37+ $resourceGroup = New-AzResourceGroup - Name $ResourceGroupName - Location $Location
38+
39+ # Create Iot Hub
40+ $iothub = New-AzIotHub - Name $IotHubName - ResourceGroupName $ResourceGroupName - Location $Location - SkuName $Sku - Units 1
41+
42+ # Get all devices
43+ $devices = Get-AzIotHubDevice - ResourceGroupName $ResourceGroupName - IotHubName $IotHubName
44+ Assert-True { $devices.Count -eq 0 }
45+
46+ # Add iot device with symmetric authentication
47+ $newDevice1 = Add-AzIotHubDevice - ResourceGroupName $ResourceGroupName - IotHubName $IotHubName - DeviceId $device1 - AuthMethod ' shared_private_key'
48+ Assert-True { $newDevice1.Id -eq $device1 }
49+ Assert-True { $newDevice1.Authentication.Type -eq ' Sas' }
50+ Assert-False { $newDevice1.Capabilities.IotEdge }
51+
52+ # Add iot device with selfsigned authentication
53+ $newDevice2 = Add-AzIotHubDevice - ResourceGroupName $ResourceGroupName - IotHubName $IotHubName - DeviceId $device2 - AuthMethod ' x509_thumbprint' - PrimaryThumbprint $primaryThumbprint - SecondaryThumbprint $secondaryThumbprint
54+ Assert-True { $newDevice2.Id -eq $device2 }
55+ Assert-True { $newDevice2.Authentication.Type -eq ' SelfSigned' }
56+ Assert-False { $newDevice2.Capabilities.IotEdge }
57+
58+ # Add iot device with certifictae authority authentication
59+ $newDevice3 = Add-AzIotHubDevice - ResourceGroupName $ResourceGroupName - IotHubName $IotHubName - DeviceId $device3 - AuthMethod ' x509_ca'
60+ Assert-True { $newDevice3.Id -eq $device3 }
61+ Assert-True { $newDevice3.Authentication.Type -eq ' CertificateAuthority' }
62+ Assert-False { $newDevice3.Capabilities.IotEdge }
63+
64+ # Get all devices
65+ $devices = Get-AzIotHubDevice - ResourceGroupName $ResourceGroupName - IotHubName $IotHubName
66+ Assert-True { $devices.Count -eq 3 }
67+
68+ # Update Device
69+ $updatedDevice1 = Set-AzIoTHubDevice - ResourceGroupName $ResourceGroupName - IotHubName $IotHubName - DeviceId $device1 - Status ' Disabled' - StatusReason ' Reason1'
70+ Assert-True { $updatedDevice1.Id -eq $device1 }
71+ Assert-True { $updatedDevice1.Status -eq ' Disabled' }
72+ Assert-True { $updatedDevice1.StatusReason -eq ' Reason1' }
73+
74+ # Update iot device to edge device
75+ $updatedDevice2 = Set-AzIoTHubDevice - ResourceGroupName $ResourceGroupName - IotHubName $IotHubName - DeviceId $device3 - EdgeEnabled $true
76+ Assert-True { $updatedDevice2.Capabilities.IotEdge }
77+
78+ # Get device detail
79+ $iotDevice = Get-AzIotHubDevice - ResourceGroupName $ResourceGroupName - IotHubName $IotHubName - DeviceId $device1
80+ Assert-True { $iotDevice.Id -eq $device1 }
81+ Assert-True { $iotDevice.Authentication.Type -eq ' Sas' }
82+ Assert-False { $iotDevice.Capabilities.IotEdge }
83+ Assert-True { $iotDevice.Status -eq ' Disabled' }
84+ Assert-True { $iotDevice.StatusReason -eq ' Reason1' }
85+
86+ # Delete iot device
87+ $result = Remove-AzIotHubDevice - ResourceGroupName $ResourceGroupName - IotHubName $IotHubName - DeviceId $device1 - Passthru
88+ Assert-True { $result }
89+
90+ # Delete all devices
91+ $result = Remove-AzIotHubDevice - ResourceGroupName $ResourceGroupName - IotHubName $IotHubName - Passthru
92+ Assert-True { $result }
93+ }
0 commit comments