Public/Set-specTVPolicyByGUID.ps1

function Set-specTVPolicyByGUID {
    <#
    .SYNOPSIS
        Applies a new TeamViewer policy to a managed device using the policy GUID.
 
    .DESCRIPTION
        This function takes a policy GUID, TeamViewer Management ID, and a secure API key as parameters.
        It applies the specified policy GUID to the managed device identified by the TeamViewer Management ID.
 
    .PARAMETER PolicyGUID
        Specifies the GUID of the TeamViewer policy to apply to the managed device.
 
    .PARAMETER TeamViewerManagementID
        Specifies the ID of the managed device in TeamViewer.
 
    .PARAMETER TVAPIKey
        Specifies the secure API key for accessing TeamViewer features.
 
    .EXAMPLE
        Set-specTVPolicyByGUID -PolicyGUID "12345678" -TeamViewerManagementID "Device123" -TVAPIKey $SecureAPIKey
        Applies the TeamViewer policy with the GUID "12345678" to the managed device with ID "Device123" using the specified API key.
 
    .EXAMPLE
        "87654321", "98765432" | Set-specTVPolicyByGUID -TeamViewerManagementID "Device456" -TVAPIKey $SecureAPIKey
        Applies the TeamViewer policies with GUIDs "87654321" and "98765432" to the managed device with ID "Device456" through the pipeline.
    .EXAMPLE
        ('My Policy Name' | Get-specTVPolicyGUIDFromName -TVSecureAPIKey $TVapiSecure).policyguid | Set-specTVPolicyByGUID -TeamViewerManagementID 'Device123' -TVAPIKey $TVapiSecure
        Gets the GUID for the policy 'My Policy Name' using Get-specTVPolicyGUIDFromName and sends the GUID to Set-specTVPolicyByGUID which then applies the policy to the device with the ID 'Device123'
 
    .NOTES
        Author: Owen Heaume
        Version: 1.0
    #>


    [cmdletbinding()]

    param (
        [Parameter(Mandatory = $true,
            ValueFromPipeline = $true)]
        [string[]]$PolicyGUID,

        [Parameter(Mandatory = $true)]
        [string]$TeamViewerManagementID,

        [Parameter(Mandatory = $true)]
        [securestring]$TVAPIKey
    )


    BEGIN {
        $OK = $true
    }

    PROCESS {
        foreach ($guid in $PolicyGUID) {
            try {
                Write-Verbose "Applying new policy guid: $guid to ManagementID: $managementID."
                Set-TeamViewerManagedDevice -ApiToken $TVAPIKey -Device $TeamViewerManagementID -Policy $guid -ErrorAction Stop -WhatIf
                write-verbose "Successfully applied Policy change for device."
            } catch {
                Write-Warning "Failed to apply new policy guid: $guid to ManagementID: $managementID. Error was: $($_.exception.message)"
                $OK = $false
            }
        }
    }

    END {
        if ($OK) {
            return 0
        } else {
            return 1
        }
    }
}