-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathCustomAttributeUpdateGroupsAllOrgs.ps1
More file actions
193 lines (162 loc) · 7.07 KB
/
CustomAttributeUpdateGroupsAllOrgs.ps1
File metadata and controls
193 lines (162 loc) · 7.07 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
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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
# Name: CustomAttributeUpdateGroupsAllOrgs.ps1
# Description: script is designed to update the Custom Attribute renamed "Groups" with the
# groups each endpoint is a member of. By default, this script will go through all of the organizations in
# your Enterprise and check for the groups each endpoint is a member of. If you only want one organization
# checked, create API credentials for that one organization and use those in script below.
#
# To use this script, please edit the script and update the following:
# 1) uncomment the Install module try/catch section if needed in your environment
# 2) Update the API_Key with your API key.
# 3) Update the Set-Action1Credentials line with your secret
# 4) Update the log directory with your desired location for logging file
# *** NOTE this file is restarted on each run, if you want to append file to see multiple runs, comment out the Remove-Item line below.
# 5) Update Set-Action1Region to your region if you are not in NorthAmerica (Europe)
#
# In Action1 console, go to Advanced->Endpoint Custom Attributes
# 1) Update one of the Custom Attribute names to match "Groups" Spelling, Capitalization and Spacing count
#
# Documentation: https://github.com/Action1Corp/PSAction1/
# Use Action1 Roadmap system (https://roadmap.action1.com/) to submit feedback or enhancement requests.
# WARNING: Carefully study the provided scripts and components before using them. Test in your non-production lab first.
# Action1 Public Repository Material
# Subject to TERMS_OF_USE.md
# Provided AS IS
# Use at your own risk
# Review and test before production deployment
# © Action1 Corporation
# NOTE: Uncomment following try/catch lines if Action1 Powershell Module is not loaded
#try {
# Install-Module -Name PSAction1
#}
#catch {
# Write-Host "ERROR: Unable to install Action1 PSAction1 Powershell integration module"
# Exit
#}
$ErrorActionPreference = "Stop"
#
# NOTE: You must enter your enterprise wide apikey, secret and region in the sections below
#
$api_Key = "api-key-YourKeyHere@action1.com"
$api_secret = "123YourSecretHere321"
$api_Region = "NorthAmerica" # or Europe / Australia
# Define the log file location
$logFile = "C:\tmp\UpdateGroupCustomAttribute.log"
# Delete log file to only log this session
if ( Test-Path -path $logFile ) {
Remove-Item -Path $logFile
}
# Function to write messages to the log file with timestamps
function Write-Log {
param (
[string]$message,
[string]$type = "INFO"
)
$timestamp = Get-Date -Format "yyyy-MM-dd HH:mm:ss"
$logEntry = "$timestamp [$type] $message"
# Append the log entry to the file
Add-Content -Path $logFile -Value $logEntry
}
# Start of script - log initiation
Write-Log "****** : Starting update Custom Attribute Groups ."
try {
Set-Action1Credentials -APIKey $api_Key -Secret $api_secret
}
catch {
Write-Host "ERROR: Unable to get credentials for for $api_Key "
Write-Log "ERROR: Unable to get credentials for for $api_Key " "ERROR"
Exit
}
try {
Set-Action1Region -Region $api_Region
}
catch {
Write-Host "ERROR: Unable to set Action1 Region $api_Region "
Write-Log "ERROR: Unable to set Action1 Region $api_Region " "ERROR"
Exit
}
try {
$orgData = Get-Action1 Organizations
}
catch {
Write-Host "ERROR: Unable to retrieve organizations for API Key $api_Key "
Write-Host " Check key, secret value and region definition "
Write-Log "ERROR: Unable to retrieve organizations for API Key $api_Key " "ERROR"
Write-Log " Check key, secret value and region definition " "ERROR"
Exit
}
foreach ($org in $orgData) {
Write-Host "Processing Organization : $($org.name)"
Write-Log "Processing Organization : $($org.name)"
try {
Set-Action1DefaultOrg -Org_ID $org.id
}
catch {
Write-Host "ERROR: Unable to set Org_ID $($org.name)"
Write-Log "ERROR: Unable to set Org_ID $($org.name)" "ERROR"
}
$groupEndpointMapping = @{}
try {
$allEps = Get-Action1 -Query Endpoints
}
catch {
Write-Host "ERROR: Unable to query endpoints for organization $($org.name)"
Write-Log "ERROR: Unable to query endpoints for organization $($org.name)" "ERROR"
}
if (-not $allEps -or $allEps.total_items -eq 0) {
Write-Host "No Endpoints in Organization $($org.name) "
Write-Log "ORGANIZATION : $($org.name) Has No Endpoints" "WARNING"
} else {
Write-Host "Processing Endpoints in $($org.name) "
Write-Log "ORGANIZATION : $($org.name) "
# Fetch all groups
try {
$allGroups = Get-Action1 -Query EndpointGroups
}
catch {
Write-Host "ERROR: Unable to query endpoint groups from organization $($org.name)"
Write-Log "ORGANIZATION : $($org.name) Has No Groups" "WARNING"
}
# Process groups and their members
foreach ($group in $allGroups) {
try {
$groupMembersResponse = Get-Action1 -Query EndpointGroupMembers -Id $group.id
}
catch {
Write-Host "ERROR: Unable to query endpoint group members from Group $($group.name)"
Write-Log "ERROR: Unable to query endpoint group members from Group $($group.name)" "ERROR"
}
$groupMembers = $groupMembersResponse | ForEach-Object { $_.name }
$groupEndpointMapping[$($group.name)] = $groupMembers
}
Foreach ($endpointName in $allEps) {
$groupsforEndpoint = @()
Write-Host " " $($endpointName.name)
# Write-Log " $($endpointName.name) "
foreach ($groupName in $groupEndpointMapping.Keys) {
if ($($endpointName.name) -in $groupEndpointMapping[$groupName]) {
$groupsForEndpoint += $groupName + ","
}
}
$glen = $groupsForEndpoint.Count
if ($glen -ne 0) {
$gname = $groupsForEndpoint[$glen-1]
$gname = $gname.Substring(0,$gname.Length-1) # Remove comma from last group in array
$groupsForEndpoint[$glen-1]=$gname
}
Write-Log "$($endpointName.name) in groups $groupsForEndpoint "
#
# NOTE: One of the Custom Attribute names must be changed to "Groups"
# Use the Groups attribute to filter reports, etc
#
try {
Update-Action1 Modify CustomAttribute -Id "$($endpointName.id)" -AttributeName "Groups" -AttributeValue "$groupsForEndpoint"
}
catch {
Write-Host "ERROR: Unable to update Group custom attribute on endpoint $($endpointName.name)"
Write-Log "ERROR: Unable to update Group custom attribute on endpoint $($endpointName.name)" "ERROR"
}
}
}
}
Write-Host "Script completed - Exit "
Write-Log "****** : Script completed : Exit "