## Script Variables $baseUrl = "loginenterprisetw.sandyshores.nl" $ConfigToken = "BifSUhkFfEKkLb7tqnYUtYiNBkaJA4OgrdemwoLM71Q" $AccountGroupName = "Account Group Name" $LauncherGroupName = "Launcher Group Name" # WARNING: ignoring SSL/TLS certificate errors is a security risk $code = @" public class SSLHandler {public static System.Net.Security.RemoteCertificateValidationCallback GetSSLHandler() {return new System.Net.Security.RemoteCertificateValidationCallback((sender, certificate, chain, policyErrors) => { return true; });} } "@ Add-Type -TypeDefinition $code # WARNING: ignoring SSL/TLS certificate errors is a security risk [System.Net.ServicePointManager]::ServerCertificateValidationCallback = [SSLHandler]::GetSSLHandler() # this is only required for older version of PowerShell/.NET [Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls -bor [Net.SecurityProtocolType]::Tls11 -bor [Net.SecurityProtocolType]::Tls12 # Define global authorization header for the script $Header = @{ "Accept" = "application/json" "Authorization" = "Bearer $ConfigToken" } #Define global Body statements for finding the Launcher and Account group name $Body = @{ "orderBy" = "name" "direction" = "desc" "count" = "100" } ## The Get-AccountGroupID function grabs all of the existing account groups and compares the configured name ($AccountGroupName) with the list ## If the configured name is found in the list it returns the ID of that account group ($global:AccountGroupID) function get-AccountGroupID { #Parameters of the API command that combines all values and selects the target API $Parameters = @{ Uri = 'https://' + $baseUrl + '/publicApi/v5/account-groups/' Headers = $Header Method = 'GET' body = $Body ContentType = 'application/json' } #Execute the command using the parameters and placing them in the $request object. $request = Invoke-RestMethod @Parameters #setting counter to 0 $i=0 #Go through the array of items generated by the rest command ForEach ($item in $request.items) { $i++ # Check each entry in array to check if the configured name exists in the array, if not it does nothing. If($item.name -like $AccountGroupName){ $global:AccountGroupID = '"' + $item.groupId + '"' Write-Host 'Found Account group match:'$item.groupId Break } # if the end of the array is reached and no match is found, report and continue ElseIf($i -ge $request.items.count){ Write-Host 'No Account Group name match found for'$AccountGroupName ', Continuing script...' # set value to null so test is still created without an account group $global:AccountGroupID = $null } } } ## The Get-LauncherGroupID function grabs all of the existing launcher groups and compares the configured name ($LauncherGroupName) with the list ## If the configured name is found in the list it returns the ID of that launcher group ($global:LauncherGroupID) function get-LauncherGroupID { #Parameters of the API command that combines all values and selects the target API $Parameters = @{ Uri = 'https://' + $baseUrl + '/publicApi/v5/launcher-groups/' Headers = $Header Method = 'GET' body = $Body ContentType = 'application/json' } #Execute the command using the parameters and placing them in the $request object. $request = Invoke-RestMethod @Parameters #setting counter to 0 $i=0 #Go through the list of items generated by the rest command ForEach ($item in $request.items) { $i++ # Check each entry in array to check if the configured name exists in the array, if not it does nothing. If($item.name -like $LauncherGroupName){ [string]$global:LauncherGroupID = '"' + $item.id + '"' Write-Host 'Found Launcher group match:'$item.id Break } # if the end of the array is reached and no match is found, report and continue ElseIf($i -ge $request.items.count){ Write-Host 'No Launcher Group name match found for'$LauncherGroupName ', Continuing script...' # set value to null so test is still created without an account group $global:LauncherGroupID = $null } } } function create-test { ## Create Body of API to create the new test, contains all required fields to create a new test. ## This example makes use of the host function of the RDP connector ## for more info check the API documentation on the External Notifications > Public API Page. $CreateBody = @" { "type": "ContinuousTest", "name": "Your Test Name", "description": "My New Test", "connector": { "type": "Rdp", "hostList": [ {"enabled": true, "endpoint": "Host1"}, {"enabled": true, "endpoint": "Host2"}, ], "suppressCertWarn": "True", }, "accountGroups": [$Global:AccountGroupId], "launcherGroups": [$Global:LauncherGroupId] } "@ #Parameters of the API command that combines all values and selects the target API $Parameters = @{ Uri = 'https://' + $baseUrl + '/publicApi/v5/tests/' Headers = $Header Method = 'POST' body = $CreateBody ContentType = 'application/json' } #Execute the rest command, if a test with the same name already exists it will generate an error. This command does not need an AccountGroupId or LauncherGroupId to work Invoke-RestMethod @Parameters } #execute each function get-AccountGroupID get-LauncherGroupID Create-test