Link to home
Start Free TrialLog in
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 $($"}
          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

Open in new window

thank you in advance.

Avatar of Adam Brown
Adam Brown
Flag of United States of America image

Link to home
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Kelly Garcia


they are defined just didn't include the full script.