forked from AutomoxCommunity/Templates_And_Examples
-
Notifications
You must be signed in to change notification settings - Fork 0
/
RecursiveGroupsCreationViaCSV.ps1
85 lines (74 loc) · 2.77 KB
/
RecursiveGroupsCreationViaCSV.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
####Save a CSV to your Desktop. The CSV should only contain 2 columns.####
####The Headers in your CSV need to only contain the headers (Parent, Child)####
####Populate the CSV with Parent and Child Folder Names####
####Update $csvname with the name of your CSV created prior to running this script####
####Update the DefaultFolderIDNumber number with the Primary Default Parent Folders ID Number#####
####Only Change these Variables####
$apiKey = 'XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX'
$orgid = "XXXXX"
$csvname = "XXXXXXXXXXXXX.csv"
$DefaultFolderIDNumber = "XXXXX"
#######################################################################################
#Read the CSV and Import
$csvpath = "$env:UserProfile\Desktop\$csvname"
$CSV = Import-CSV -path "$csvpath"
#Select The Unique Parent Names
$PrimaryGroup = ($CSV.Parent | Select -unique)
#For Each Parent Create the Parent
$PrimaryGroup | % {
$Primary = $_
$headers = @{
"Authorization" = "Bearer $apiKey"
"Content-Type" = "application/json"
}
$body = @"
{
"name": "$Primary",
"refresh_interval": 1440,
"parent_server_group_id": "$DefaultFolderIDNumber",
"ui_color": "#3C18EF",
"notes": "$Primary",
"enable_os_auto_update": false,
"enable_wsus": false
}
"@
$url = "https://console.automox.com/api/servergroups?o=$orgid"
Invoke-WebRequest -Method Post -Uri $url -Headers $headers -Body $body
#Wait a sec
Start-sleep
#Call the new folders ID and store it in $parentid
$headers = @{ "Authorization" = "Bearer $apiKey" }
$url = "https://console.automox.com/api/servergroups?o=$orgid"
$response = (Invoke-WebRequest -Method Get -Uri $url -Headers $headers).Content | ConvertFrom-JSon
$parentid = ($response | ? {$_ -match "$Primary"} | Select -ExpandProperty id)
#Select all Unique SubFolders that belong to the current Parent Folder
$SubFolder = (($CSV | ? {$_.Parent -match "$Primary"} | Select -ExpandProperty Child ) | Select -unique)
#Skip if the count is 0
If ($SubFolder.count -ge 1){
#Create all the child groups of the current Parent
$SubFolder | % {
$ChildFolder = $_
$headers = @{
"Authorization" = "Bearer $apiKey"
"Content-Type" = "application/json"
}
$body = @"
{
"name": "$ChildFolder",
"refresh_interval": 1440,
"parent_server_group_id": "$parentid",
"ui_color": "#3C18EF",
"notes": "$Primary_Child_$ChildFolder",
"enable_os_auto_update": false,
"enable_wsus": false
}
"@
$url = "https://console.automox.com/api/servergroups?o=$orgid"
Invoke-WebRequest -Method Post -Uri $url -Headers $headers -Body $body
}
}else{
#If the count was "0" above, then write that the Parent had no Children
Write-Host "$Primary has no SubFolders"
}
}
Write-Host "You have now created the needed groups and they should be availble within the admin console"