Skip to content

Commit

Permalink
Merge pull request #111 from KelvinTegelaar/dev
Browse files Browse the repository at this point in the history
Pushing Dev to release
  • Loading branch information
KelvinTegelaar committed Feb 4, 2022
2 parents c934c00 + 307c61c commit 215b638
Show file tree
Hide file tree
Showing 53 changed files with 1,655 additions and 314 deletions.
24 changes: 24 additions & 0 deletions AddAlert/function.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
{
"bindings": [
{
"authLevel": "anonymous",
"type": "httpTrigger",
"direction": "in",
"name": "Request",
"methods": [
"get",
"post"
]
},
{
"type": "http",
"direction": "out",
"name": "Response"
},
{
"name": "starter",
"direction": "in",
"type": "durableClient"
}
]
}
47 changes: 47 additions & 0 deletions AddAlert/run.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
using namespace System.Net

# Input bindings are passed in via param block.
param($Request, $TriggerMetadata)

$APIName = $TriggerMetadata.FunctionName
Log-Request -user $request.headers.'x-ms-client-principal' -API $APINAME -message "Accessed this API" -Sev "Debug"

Write-Host "PowerShell HTTP trigger function processed a request."

$Tenants = ($Request.body | Select-Object Select_*).psobject.properties.value
$Results = foreach ($Tenant in $tenants) {
try {
$CompleteObject = [PSCustomObject]@{
tenant = $tenant
AdminPassword = $Request.body.AdminPassword
DefenderMalware = $Request.body.DefenderMalware
DefenderStatus = $Request.body.DefenderStatus
DisableRestart = $Request.body.DisableRestart
InstallAsSystem = $Request.body.InstallAsSystem
MFAAdmins = $Request.body.MFAAdmins
MFAAlertUsers = $Request.body.MFAAlertUsers
NewApprovedApp = $Request.body.NewApprovedApp
NewGA = $Request.body.NewGA
NewRole = $Request.body.NewRole
QuotaUsed = $Request.body.QuotaUsed
UnusedLicenses = $Request.body.UnusedLicenses
Type = "Alert"

} | ConvertTo-Json
$JSONFile = New-Item -Path ".\Cache_Scheduler\$tenant.alert.json" -Value $CompleteObject -Force -ErrorAction Stop
"Succesfully added Alert for $($Tenant) to queue."
Log-Request -user $request.headers.'x-ms-client-principal' -API $APINAME -tenant $tenant -message "Succesfully added Alert for $($Tenant) to queue." -Sev "Info"
}
catch {
Log-Request -user $request.headers.'x-ms-client-principal' -API $APINAME -tenant $tenant -message "Failed to add Alert for for $($Tenant) to queue" -Sev "Error"
"Failed to add Alert for for $($Tenant) to queue"
}
}

$body = [pscustomobject]@{"Results" = @($results) }

# Associate values to output bindings by calling 'Push-OutputBinding'.
Push-OutputBinding -Name Response -Value ([HttpResponseContext]@{
StatusCode = [HttpStatusCode]::OK
Body = $body
})
3 changes: 0 additions & 3 deletions AddChocoApp/run.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,6 @@ $Results = foreach ($Tenant in $tenants) {
}
}

$InstanceId = Start-NewOrchestration -FunctionName 'Applications_Orchestrator'
Write-Host "Started orchestration with ID = '$InstanceId'"

$body = [pscustomobject]@{"Results" = $results }

# Associate values to output bindings by calling 'Push-OutputBinding'.
Expand Down
24 changes: 24 additions & 0 deletions AddChocoApp_OrchestrationStarter/function.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
{
"bindings": [
{
"authLevel": "anonymous",
"name": "Request",
"type": "httpTrigger",
"direction": "in",
"methods": [
"post",
"get"
]
},
{
"type": "http",
"direction": "out",
"name": "Response"
},
{
"name": "starter",
"type": "durableClient",
"direction": "in"
}
]
}
22 changes: 22 additions & 0 deletions AddChocoApp_OrchestrationStarter/run.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
using namespace System.Net

param($Request, $TriggerMetadata)
$CurrentlyRunning = Get-Item "ChocoApps.Cache\CurrentlyRunning.txt" -ErrorAction SilentlyContinue | Where-Object -Property LastWriteTime -GT (Get-Date).AddHours(-24)
if ($CurrentlyRunning) {
$Results = [pscustomobject]@{"Results" = "Already running. Please wait for the current instance to finish" }
Log-request -API "ChocoApps" -message "Attempted to start upload but an instance was already running." -sev Info
}
else {
$InstanceId = Start-NewOrchestration -FunctionName 'Applications_Orchestrator'
Write-Host "Started orchestration with ID = '$InstanceId'"
$Orchestrator = New-OrchestrationCheckStatusResponse -Request $Request -InstanceId $InstanceId
Log-request -API "ChocoApps" -message "Started uploading applications to tenants" -sev Info
$Results = [pscustomobject]@{"Results" = "Started application queue" }
}
Write-Host ($Orchestrator | ConvertTo-Json)


Push-OutputBinding -Name Response -Value ([HttpResponseContext]@{
StatusCode = [HttpStatusCode]::OK
Body = $results
})
15 changes: 15 additions & 0 deletions AddChocoApp_OrchestrationStarterTimer/function.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"bindings": [
{
"name": "Timer",
"type": "timerTrigger",
"direction": "in",
"schedule": "0 0 12 * * *"
},
{
"name": "starter",
"type": "durableClient",
"direction": "in"
}
]
}
15 changes: 15 additions & 0 deletions AddChocoApp_OrchestrationStarterTimer/run.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
param($Timer)

$CurrentlyRunning = Get-Item "ChocoApps.Cache\CurrentlyRunning.txt" -ErrorAction SilentlyContinue | Where-Object -Property LastWriteTime -GT (Get-Date).AddHours(-24)
if ($CurrentlyRunning) {
$Results = [pscustomobject]@{"Results" = "Already running. Please wait for the current instance to finish" }
Log-request -API "ChocoApps" -message "Attempted to start upload but an instance was already running." -sev Info
}
else {
$InstanceId = Start-NewOrchestration -FunctionName 'Applications_Orchestrator'
Write-Host "Started orchestration with ID = '$InstanceId'"
$Orchestrator = New-OrchestrationCheckStatusResponse -Request $Request -InstanceId $InstanceId
Log-request -API "ChocoApps" -message "Started uploading applications to tenants" -sev Info
$Results = [pscustomobject]@{"Results" = "Started running analysis" }
}
Write-Host ($Orchestrator | ConvertTo-Json)
34 changes: 27 additions & 7 deletions AddPolicy/run.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ param($Request, $TriggerMetadata)
$APIName = $TriggerMetadata.FunctionName
Log-Request -user $request.headers.'x-ms-client-principal' -API $APINAME -message "Accessed this API" -Sev "Debug"

$user = $request.headers.'x-ms-client-principal'
$Tenants = ($Request.body | Select-Object Select_*).psobject.properties.value
if ("AllTenants" -in $Tenants) { $Tenants = (Get-Tenants).DefaultDomainName }
$displayname = $request.body.Displayname
$description = $request.body.Description
$AssignTo = if ($request.body.Assignto -ne "on") { $request.body.Assignto }
Expand All @@ -17,22 +17,42 @@ $results = foreach ($Tenant in $tenants) {
try {
switch ($Request.body.TemplateType) {
"Admin" {
$TemplateTypeURL = "groupPolicyConfigurations"
$CreateBody = '{"description":"' + $description + '","displayName":"' + $displayname + '","roleScopeTagIds":["0"]}'
$CreateRequest = New-GraphPOSTRequest -uri "https://graph.microsoft.com/beta/deviceManagement/groupPolicyConfigurations" -tenantid $tenant -type POST -body $CreateBody
$UpdateRequest = New-GraphPOSTRequest -uri "https://graph.microsoft.com/beta/deviceManagement/groupPolicyConfigurations('$($CreateRequest.id)')/updateDefinitionValues" -tenantid $tenant -type POST -body $RawJSON
$CheckExististing = New-GraphGETRequest -uri "https://graph.microsoft.com/beta/deviceManagement/$TemplateTypeURL" -tenantid $tenant
if ($displayname -in $CheckExististing.displayName) {
Throw "Policy with Display Name $($Displayname) Already exists"
}
$CreateRequest = New-GraphPOSTRequest -uri "https://graph.microsoft.com/beta/deviceManagement/$TemplateTypeURL" -tenantid $tenant -type POST -body $CreateBody
$UpdateRequest = New-GraphPOSTRequest -uri "https://graph.microsoft.com/beta/deviceManagement/$TemplateTypeURL('$($CreateRequest.id)')/updateDefinitionValues" -tenantid $tenant -type POST -body $RawJSON
}
"Device" {
$CreateRequest = New-GraphPOSTRequest -uri "https://graph.microsoft.com/beta/deviceManagement/deviceConfigurations" -tenantid $tenant -type POST -body $RawJSON
$TemplateTypeURL = "deviceConfigurations"
$PolicyName = ($RawJSON | ConvertFrom-Json).displayName
$CheckExististing = New-GraphGETRequest -uri "https://graph.microsoft.com/beta/deviceManagement/$TemplateTypeURL" -tenantid $tenant
Write-Host $PolicyName
if ($PolicyName -in $CheckExististing.displayName) {
Throw "Policy with Display Name $($Displayname) Already exists"
}

$CreateRequest = New-GraphPOSTRequest -uri "https://graph.microsoft.com/beta/deviceManagement/$TemplateTypeURL" -tenantid $tenant -type POST -body $RawJSON
}
"Catalog" {
$CreateRequest = New-GraphPOSTRequest -uri "https://graph.microsoft.com/beta/deviceManagement/configurationPolicies" -tenantid $tenant -type POST -body $RawJSON
$TemplateTypeURL = "configurationPolicies"
$CheckExististing = New-GraphGETRequest -uri "https://graph.microsoft.com/beta/deviceManagement/$TemplateTypeURL" -tenantid $tenant
$PolicyName = ($RawJSON | ConvertFrom-Json).Name
$CheckExististing = New-GraphGETRequest -uri "https://graph.microsoft.com/beta/deviceManagement/$TemplateTypeURL" -tenantid $tenant
if ($PolicyName -in $CheckExististing.name) {
Throw "Policy with Display Name $($Displayname) Already exists"
}
$CreateRequest = New-GraphPOSTRequest -uri "https://graph.microsoft.com/beta/deviceManagement/$TemplateTypeURL" -tenantid $tenant -type POST -body $RawJSON
}

}
Log-Request -user $request.headers.'x-ms-client-principal' -API $APINAME -tenant $($Tenant) -message "Added policy $($Displayname)" -Sev "Error"
if ($AssignTo) {
$AssignBody = if ($AssignTo -ne "AllDevicesAndUsers") { '{"assignments":[{"id":"","target":{"@odata.type":"#microsoft.graph.' + $($AssignTo) + 'AssignmentTarget"}}]}' } else { '{"assignments":[{"id":"","target":{"@odata.type":"#microsoft.graph.allDevicesAssignmentTarget"}},{"id":"","target":{"@odata.type":"#microsoft.graph.allLicensedUsersAssignmentTarget"}}]}' }
$assign = New-GraphPOSTRequest -uri "https://graph.microsoft.com/beta/deviceManagement/groupPolicyConfigurations('$($CreateRequest.id)')/assign" -tenantid $tenant -type POST -body $AssignBody
$assign = New-GraphPOSTRequest -uri "https://graph.microsoft.com/beta/deviceManagement/$TemplateTypeURL('$($CreateRequest.id)')/assign" -tenantid $tenant -type POST -body $AssignBody
Log-Request -user $request.headers.'x-ms-client-principal' -API $APINAME -tenant $($Tenant) -message "Assigned policy $($Displayname) to $AssignTo" -Sev "Info"
}
"Succesfully added policy for $($Tenant)"
Expand All @@ -45,7 +65,7 @@ $results = foreach ($Tenant in $tenants) {

}

$body = [pscustomobject]@{"Results" = $results }
$body = [pscustomobject]@{"Results" = @($results) }

# Associate values to output bindings by calling 'Push-OutputBinding'.
Push-OutputBinding -Name Response -Value ([HttpResponseContext]@{
Expand Down
2 changes: 1 addition & 1 deletion Applications_GetQueue/run.ps1
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
param($name)

$object = (get-childitem ".\ChocoApps.Cache\*").name
$object = (Get-ChildItem ".\ChocoApps.Cache\*" | Where-Object { $_.name -ne "CurrentlyRunning.txt" }).name
$object
4 changes: 2 additions & 2 deletions Applications_Orchestrator/run.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@ param($Context)
$Batch = (Invoke-DurableActivity -FunctionName 'Applications_GetQueue' -Input 'LetsGo')
Write-Host $Batch
$ParallelTasks = foreach ($Item in $Batch) {
Invoke-DurableActivity -FunctionName "Applications_Upload"-Input $item -NoWait
Invoke-DurableActivity -FunctionName "Applications_Upload" -Input $item -NoWait
}

$Outputs = Wait-ActivityFunction -Task $ParallelTasks
Write-Host $Outputs

Log-request -API "ChocoApp" "Choco Application Queue: Deployment finished." -sev Info
Log-request -API "ChocoApp" -Message "Choco Application Queue: Deployment finished." -sev Info
Loading

0 comments on commit 215b638

Please sign in to comment.