Avatar of Kelly Garcia
Kelly GarciaFlag for United Kingdom of Great Britain and Northern Ireland asked on

Powershell Functions

Hi Guys,

I have written this function in powershell, can you think of better ways of writing this?:

function LogsToLogAnalytics ($resource,$categories,$Log){

Process {     

    Try {
          Set-AzureRmDiagnosticSetting -Name $DiName -ResourceID $($resource.ResourceID)  -WorkspaceId $($LogAnalytics.ResourceId)  -enabled $en -Categories $categories -ErrorAction Stop  >> $Log
          if ($en){write-output "Diagnostics Setting Successfully Enabled for $($Resource.name)"}
          else { Write-Output "Diagnostics Setting Disabled for $($Resource.name)" }


    Catch {
            Write-Output "Caught an exception on $($Resource.name)"
            Write-Output "Exception Type: $($_.Exception.GetType().FullName)"
            Write-Output "Exception Message: $($_.Exception.Message)"
            Write-Output " "



 $KV = Get-AzureRmResource | Where-Object {$_.resourcetype -eq "Microsoft.KeyVault/vaults"  }
 $KV | ForEach-Object {

    LogsToLogAnalytics -Resource $_  -Categories AuditEvent -Log $KeyVaultConfigLog

                                       } #endforeach

Open in new window

thank you in advance.


Avatar of undefined
Last Comment
Kelly Garcia

8/22/2022 - Mon
Adam Brown

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
See how we're fighting big data
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
Kelly Garcia

they are defined just didn't include the full script.
I started with Experts Exchange in 2004 and it's been a mainstay of my professional computing life since. It helped me launch a career as a programmer / Oracle data analyst
William Peck