We help IT Professionals succeed at work.

Powershell Functions

Kelly Garcia
Kelly Garcia asked
on
63 Views
Last Modified: 2020-04-01
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
Comment
Watch Question

Senior Systems Admin
CERTIFIED EXPERT
Top Expert 2010
Commented:
This one is on us!
(Get your first solution completely free - no credit card required)
UNLOCK SOLUTION
Kelly GarciaSenior Systems Administrator

Author

Commented:
they are defined just didn't include the full script.
Unlock the solution to this question.
Join our community and discover your potential

Experts Exchange is the only place where you can interact directly with leading experts in the technology field. Become a member today and access the collective knowledge of thousands of technology experts.

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.