CWA Agent Sync Script

Is there a script that can be used to run a CWA agent sync?

The task to update the CWA EDF is failing because the LT agent has not synchronized with ImmyBot when the task is executed (during onboarding). Forcing a CWA agent sync for the tenant at the start of the script should solve the issue.

For some context, here are the parameters I am using in the script:

$Computer = Get-ImmyComputer
$RmmInfo = Get-RmmInfo -ProviderType CWAutomate
$RmmComputer = Get-RmmComputer -Computer $Computer -ProviderType CWAutomate
$Uri = "cwa/api/v1/computers/$($RmmComputer.RmmDeviceId)/extrafields"

$($RmmComputer.RmmDeviceId) returns blank because the CWA agent ID cannot be found in ImmyBot. The script works fine if the CWA agent for the particular computer is already showing in ImmyBot.

Found a solution. Since the script was only trying to get the CWA agent ID from ImmyBot and wasn’t trying to do anything else with the integration, I just needed another method to get the ID.

Updates:

  • Using the following to pull the ID from the registry: $RMMDeviceID = Get-WindowsRegistryValue -Path HKLM:\SOFTWARE\LabTech\Service -Name “ID”
  • Changed the $Uri variables to use $RMMDeviceID instead of $($RmmComputer.RmmDeviceId)

Posting the whole script here in case anyone finds it useful for updating Dropdown and Checkbox CWA EDFs.

Update CWA EDF Script:

if(!$Computer) {
    $Computer = Get-ImmyComputer
}

$RMMDeviceID = Get-WindowsRegistryValue -Path HKLM:\SOFTWARE\LabTech\Service -Name "ID"
$RmmInfo = Get-RmmInfo -ProviderType CWAutomate
$RmmComputer = Get-RmmComputer -Computer $Computer -ProviderType CWAutomate
$Uri = "cwa/api/v1/computers/$RMMDeviceID/extrafields"

switch ($method) {
    "Test" {
        $RmmEdfTest = Invoke-CWARestMethod $Uri -Provider $RmmInfo.Provider -QueryParams $QueryParams | ?{$_.Title -like $FieldName}
        switch ($RmmEdfTest.PSObject.Properties.Name){
            "TextFieldSettings" {
                if (($RmmEdfTest.TextFieldSettings | select -expand Value) -eq $Value) {
                    Write-host "EDF $FieldName value matches $value. No change needed"
                    return $true
                }
                else {
                    Write-host "EDF $FieldName value does not match $value. Proceeding to update EDF"
                    return $false
                }
            }
            "DropdownSettings" {
                if (($RmmEdfTest.DropdownSettings | select -expand SelectedValue) -eq $value) {
                    Write-host "EDF $FieldName value matches $value. No change needed"
                    return $true
                }
                else {
                    Write-host "EDF $FieldName value does not match $value. Proceeding to update EDF"
                    return $false
                }
            }
            "CheckboxSettings" {
                $RmmEdfTest.CheckboxSettings | select -expand IsChecked
            }
        }
    }

    "Set" {
        $RmmInfo = Get-RmmInfo -ProviderType CWAutomate
        $RmmComputer = Get-RmmComputer -Computer $Computer -ProviderType CWAutomate

        # Retrieve the EDF for the given field name
        $Uri = "cwa/api/v1/computers/$RMMDeviceID/extrafields"
        $RmmEdfSet = Invoke-CWARestMethod $Uri -Provider $RmmInfo.Provider | Where-Object { $_.Title -like $FieldName }

        if (!$RmmEdfSet) {
            Write-Warning "Aborting: Unable to find EDF $FieldName"
            return
        }

        $ExtraFieldDefinitionId = $RmmEdfSet.ExtraFieldDefinitionId
        
        switch ($RmmEdfSet.PSObject.Properties.Name) {
            "TextFieldSettings" {
                $PatchPath = "/TextFieldSettings/Value"
                $PatchValue = $Value
            }
            "DropdownSettings" {
                $DropDownOptions = $RmmEdfSet.DropdownSettings.AvailableOptions
                $Option = $null
                foreach ($key in $DropDownOptions.PSObject.Properties.Name) {
                    if ($DropDownOptions.$key -eq $Value) {
                        $Option = $DropDownOptions.$key
                        break
                    }
                }

                if (!$Option) {
                    Write-Warning "Aborting: Unable to find drop-down option $Value for $FieldName"
                    return
                }

                $PatchPath = "/DropdownSettings/SelectedValue"
                $PatchValue = $Option
            }
            default {
            # Log detailed information about the unsupported EDF type
            Write-Warning "Unsupported field type $($RmmEdfSet.PSObject.Properties.Name) for $FieldName. Skipping."
            $RmmEdfSet | Select-Object * | Format-List | Out-String | Write-Warning
            }
        }

        # Construct the JSON body for the PATCH request
        $Body = ConvertTo-Json @(
            @{
                op = "replace"
                path = $PatchPath
                value = $PatchValue
            }
        )

        # Send the PATCH request to update the field value
        $UriUpdate = "cwa/api/v1/computers/$RMMDeviceID/extrafields/$ExtraFieldDefinitionId"
        Invoke-CWARestMethod $UriUpdate -Provider $RmmInfo.Provider -Method PATCH -Body $Body
    }
}

Notes:

  • Requires $FieldName and $Value parameters to be defined in the task
  • Capable of updating EDFs with drop-down fields

Issues:

  • Has not been tested with EDF Checkbox fields
  • Outputs excessive unnecessary information during Enforce step

Previous script was not working for new computer onboardings. Detection phase would always fail since the CWA agent is not installed when it does the detection.

Updated script to check for the LT registry path. If it doesn’t exist, it will return $False and run the Set script. If it does exist, it will check to see if the current EDF value matches the desired value.

if(!$Computer) {
    $Computer = Get-ImmyComputer
}

$RMMDeviceID = Get-WindowsRegistryValue -Path HKLM:\SOFTWARE\LabTech\Service -Name "ID"
$RmmInfo = Get-RmmInfo -ProviderType CWAutomate
$RmmComputer = Get-RmmComputer -Computer $Computer -ProviderType CWAutomate
$Uri = "cwa/api/v1/computers/$RMMDeviceID/extrafields"
$TestPath = Invoke-ImmyCommand {Test-Path -path "HKLM:\SOFTWARE\LabTech\Service"}

switch ($method) {
    "Test" {
        If($TestPath){
            $RmmEdfTest = Invoke-CWARestMethod $Uri -Provider $RmmInfo.Provider -QueryParams $QueryParams | ?{$_.Title -like $FieldName}
            switch ($RmmEdfTest.PSObject.Properties.Name){
                "TextFieldSettings" {
                    if (($RmmEdfTest.TextFieldSettings | select -expand Value) -eq $Value) {
                        Write-host "EDF $FieldName value matches $value. No change needed"
                        return $true
                    }
                    else {
                        Write-host "EDF $FieldName value does not match $value. Proceeding to update EDF"
                        return $false
                    }
                }
                "DropdownSettings" {
                    if (($RmmEdfTest.DropdownSettings | select -expand SelectedValue) -eq $value) {
                        Write-host "EDF $FieldName value matches $value. No change needed"
                        return $true
                    }
                    else {
                        Write-host "EDF $FieldName value does not match $value. Proceeding to update EDF"
                        return $false
                    }
                }
                "CheckboxSettings" {
                    $RmmEdfTest.CheckboxSettings | select -expand IsChecked
                }
            }
        }
        else{
            return $false
        }
    }

    "Set" {
        $RmmInfo = Get-RmmInfo -ProviderType CWAutomate
        $RmmComputer = Get-RmmComputer -Computer $Computer -ProviderType CWAutomate

        # Retrieve the EDF for the given field name
        $Uri = "cwa/api/v1/computers/$RMMDeviceID/extrafields"
        $RmmEdfSet = Invoke-CWARestMethod $Uri -Provider $RmmInfo.Provider | Where-Object { $_.Title -like $FieldName }

        if (!$RmmEdfSet) {
            Write-Warning "Aborting: Unable to find EDF $FieldName"
            return
        }

        $ExtraFieldDefinitionId = $RmmEdfSet.ExtraFieldDefinitionId
        
        switch ($RmmEdfSet.PSObject.Properties.Name) {
            "TextFieldSettings" {
                $PatchPath = "/TextFieldSettings/Value"
                $PatchValue = $Value
            }
            "DropdownSettings" {
                $DropDownOptions = $RmmEdfSet.DropdownSettings.AvailableOptions
                $Option = $null
                foreach ($key in $DropDownOptions.PSObject.Properties.Name) {
                    if ($DropDownOptions.$key -eq $Value) {
                        $Option = $DropDownOptions.$key
                        break
                    }
                }

                if (!$Option) {
                    Write-Warning "Aborting: Unable to find drop-down option $Value for $FieldName"
                    return
                }

                $PatchPath = "/DropdownSettings/SelectedValue"
                $PatchValue = $Option
            }
            default {
            # Log detailed information about the unsupported EDF type
            Write-Warning "Unsupported field type $($RmmEdfSet.PSObject.Properties.Name) for $FieldName. Skipping."
            $RmmEdfSet | Select-Object * | Format-List | Out-String | Write-Warning
            }
        }

        # Construct the JSON body for the PATCH request
        $Body = ConvertTo-Json @(
            @{
                op = "replace"
                path = $PatchPath
                value = $PatchValue
            }
        )

        # Send the PATCH request to update the field value
        $UriUpdate = "cwa/api/v1/computers/$RMMDeviceID/extrafields/$ExtraFieldDefinitionId"
        Invoke-CWARestMethod $UriUpdate -Provider $RmmInfo.Provider -Method PATCH -Body $Body
    }
}