public/Enable-AzureDSCNodeDiagnostics.ps1

function Enable-AzureDSCNodeDiagnostics {

<#
 
.SYNOPSIS
 
Enables azure diagnostics logging for Multiple dsc nodes
  
.DESCRIPTION
 
Enable-AzureDSCNodeDiagnostics cmdlet enables azure diagnostics logging for all dsc nodes for a given automation account, and sends the data to an oms workspace without local retention or archiving to a storage account.
 
.PARAMETER SubscriptionName
 
Specify the subscriptionName.
 
.PARAMETER workspaceID
Specify the OMS workspaceID.
 
.EXAMPLE
Enable-AzureDSCNodeDiagnostics -workspaceid <workspaceID> -subscriptionName <SubscriptionName> -Verbose 
 
#>


    [CmdletBinding()]

    Param (
        
        [Parameter(Mandatory=$false)]
        [bool]$enabled = $true,

        [Parameter(Mandatory=$true)]
        [ValidateNotNullOrEmpty()]
        [string]$workspaceid,

        [Parameter(Mandatory=$true)]
        [ValidateNotNullOrEmpty()]
        [string]$subscriptionName
    
    )

    Process {
    
        # check to see if local token exists (ran Login-AzureRMAccount)
        if (($null -eq (Get-AzureRmContext).Account)) {
            Write-Warning "Please run < Login-AzureRMAccount > first to create a session token...exiting."
            break
        } 

        # Get subscription name
        Try {
            Select-AzureRmSubscription -SubscriptionName $subscriptionName -ErrorAction Stop -Verbose | Out-Null
        }
        Catch {
            $error[0].Exception
            break
        }
        
        $results = @()

        $automation_accounts = Find-AzureRmResource -ResourceType "Microsoft.Automation/automationAccounts"

        foreach ($a in $automation_accounts) {

            Try {
                $dsc_nodes = Get-AzureRmAutomationDscNode -ResourceGroupName "$($a.resourcegroupname)" -AutomationAccountName "$($a.name)" -ErrorAction Stop -Verbose
            }
            Catch {
                $error[0].Exception
                break
            }
            
            if ($null -ne $dsc_nodes) {
        
                Try {
                    Write-Verbose "Found DSC nodes registered to automation account $($a.name)."
                    $op = Set-AzureRmDiagnosticSetting -ResourceId "$($a.ResourceId)" -WorkspaceId $workspaceid -Enabled $enabled -Categories 'DSCNodeStatus' -ErrorAction Stop -Verbose
                    $results += $op
                }
                Catch {
                    $error[0].Exception
                    break
                }
            }
            else {
                Write-Warning "No dsc nodes found in resource group '$($a.resourcegroupname).'"
            }
        }

        return $results
    } # end process block

} # end function