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 $($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.

Regards,
Kay
ASKER CERTIFIED SOLUTION
Avatar of Adam Brown
Adam Brown
Flag of United States of America image

Link to home
membership
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

ASKER

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