troubleshooting Question

Powershell Functions

Avatar of Kelly Garcia
Kelly GarciaFlag for United Kingdom of Great Britain and Northern Ireland asked on
2 Comments1 Solution65 ViewsLast Modified:
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 $($"}
          else { Write-Output "Diagnostics Setting Disabled for $($" }


    Catch {
            Write-Output "Caught an exception on $($"
            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

thank you in advance.

Adam Brown
Cloud Security Consultant
Join our community to see this answer!
Unlock 1 Answer and 2 Comments.
Start Free Trial
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 2 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros