forked from lwhitelock/HuduAutomation
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Hudu-Customer-Products-Magic-Dash.ps1
92 lines (72 loc) · 3.13 KB
/
Hudu-Customer-Products-Magic-Dash.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
$VaultName = "Your Key Vault Name"
#### Hudu Settings ####
$HuduAPIKey = Get-AzKeyVaultSecret -vaultName $VaultName -name "HuduAPIKey" -AsPlainText
# Set the base domain of your Hudu instance without a trailing /
$HuduBaseDomain = Get-AzKeyVaultSecret -vaultName $VaultName -name "HuduBaseDomain" -AsPlainText
$DetailsLayoutName = 'Company Details'
$SplitChar = ':'
import-module HuduAPI
#Login to Hudu
New-HuduAPIKey $HuduAPIKey
New-HuduBaseUrl $HuduBaseDomain
$AllowedActions = @('ENABLED', 'NOTE', 'URL')
# Get the Asset Layout
$DetailsLayout = Get-HuduAssetLayouts -name $DetailsLayoutName
# Check we found the layout
if (($DetailsLayout | measure-object).count -ne 1) {
Write-Error "No / multiple layout(s) found with name $DetailsLayoutName"
} else {
# Get all the detail assets and loop
$DetailsAssets = Get-HuduAssets -assetlayoutid $DetailsLayout.id
foreach ($Asset in $DetailsAssets) {
# Loop through all the fields on the Asset
$Fields = foreach ($field in $DetailsAssets.fields) {
# Split the field name
$SplitField = $Field.label -split $SplitChar
# Check the field has an allowed action.
if ($SplitField[1] -notin $AllowedActions) {
Write-Error "Field $($Field.label) is not an allowed action"
} else {
# Format an object to work with
[PSCustomObject]@{
ServiceName = $SplitField[0]
ServiceAction = $SplitField[1]
Value = $field.value
}
}
}
Foreach ($Service in $fields.servicename | select-object -unique){
$EnabledField = $fields | Where-Object {$_.servicename -eq $Service -and $_.serviceaction -eq 'ENABLED'}
$NoteField = $fields | Where-Object {$_.servicename -eq $Service -and $_.serviceaction -eq 'NOTE'}
$URLField = $fields | Where-Object {$_.servicename -eq $Service -and $_.serviceaction -eq 'URL'}
if ($EnabledField){
$Colour = Switch ($EnabledField.value) {
$True {'success'}
$False {'grey'}
default {'grey'}
}
$Param = @{
Title = $Service
CompanyName = $Asset.company_name
Shade = $Colour
}
if ($NoteField.value){
$Param['Message'] = $NoteField.value
$Param | Add-Member -MemberType NoteProperty -Name 'Message' -Value $NoteField.value
} else {
$Param['Message'] =Switch ($EnabledField.value) {
$True {"Customer has $Service"}
$False {"No $Service"}
default {"No $Service"}
}
}
if ($URLField.value){
$Param['ContentLink'] = $URLField.value
}
Set-HuduMagicDash @Param
} else {
Write-Error "No Enabled Field was found"
}
}
}
}