Advertisement
Guest User

Untitled

a guest
Oct 20th, 2019
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.93 KB | None | 0 0
  1. <#
  2. .SYNOPSIS
  3. Gets focused Az log information on resources. Helpful to determine who created what.
  4.  
  5. .DESCRIPTION
  6. Providing only the ResourceGroupName will retrive logs for all items in that resource group. You can additionall filter the operations. If you also provide the resource name and type then only logs for that single items will be retrieved
  7.  
  8. .PARAMETER ResourceGroupName
  9. Specifies the name of the resource group.
  10.  
  11. .PARAMETER ResourceName
  12. Specifies the name of the single resource.
  13.  
  14. .PARAMETER ResourceType
  15. Specifies the type of the single resource.
  16.  
  17. .PARAMETER MatchOperations
  18. Specifies a regex pattern of operation names to filter for.
  19. #>
  20. [CmdletBinding(DefaultParameterSetName='Group')]
  21. param(
  22. [Parameter(ParameterSetName='Group', Mandatory)]
  23. [Parameter(ParameterSetName='SingleItem', Mandatory)]
  24. $ResourceGroupName,
  25.  
  26. [Parameter(ParameterSetName='SingleItem', Mandatory)]
  27. $ResourceName,
  28.  
  29. [Parameter(ParameterSetName='SingleItem', Mandatory)]
  30. $ResourceType,
  31.  
  32. $FilterOperations
  33. )
  34.  
  35. $context = Get-AzContext
  36. if(-not $context) {
  37. Write-Warning 'Connect to your Azure account with Connect-AzAccount'
  38. }
  39.  
  40. $logs = $null
  41.  
  42. if($PSCmdlet.ParameterSetName -eq 'SingleItem') {
  43. $resource = Get-AzResource -ResourceGroupName $ResourceGroupName -Name $ResourceName -ResourceType $ResourceType
  44. $logs = Get-AzLog -ResourceId $resource.Id
  45. } elseif ($PSCmdlet.ParameterSetName -eq 'Group') {
  46. $logs = Get-AzLog -ResourceGroupName $ResourceGroupName
  47. }
  48.  
  49. $parameters = @{
  50. Property = @(
  51. 'Caller',
  52. @{Name='Operation'; Expression = { $_.OperationName.LocalizedValue}},
  53. 'ResourceId',
  54. @{Name='ResourceName'; Expression = { $_.Authorization.Scope | Split-Path -Leaf }}
  55. )
  56. }
  57.  
  58. if($FilterOperations) {
  59. $logs | Where-Object { $_.OperationName.LocalizedValue -match $FilterOperations -or $_.OperationName.Value -match $FilterOperations } | Select-Object @parameters
  60. } else {
  61. $logs | Select-Object @parameters
  62. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement