Advertisement
Guest User

Untitled

a guest
Dec 5th, 2017
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. # Author Hilko Krueger
  2. # QUIBIQ Hamburg GmbH
  3. # License MIT
  4.  
  5. $json = Get-Content 'C:\xxxx\OMS\alerts.json' | Out-String | ConvertFrom-Json
  6.  
  7. $searchNames = $($json.searches | Get-Member -MemberType *Property)
  8.  
  9. $tenentID = "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"
  10. $appUser = "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"
  11. $appUserPassword = "xxxxxxxxxxxxx"
  12. $subscriptionID = "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"
  13.  
  14.  
  15. armclient spn $tenentID $appUser $appUserPassword
  16. foreach ($search in $searchNames) {
  17.  
  18.     $searchValue = ($json.searches | Select-Object -ExpandProperty $search.Name)
  19.     # create a search or update a search
  20.  
  21.     $savedSearchParametersJson = $searchValue.data | ConvertTo-Json -Depth 50 | % { [System.Text.RegularExpressions.Regex]::Unescape($_) }
  22.     $savedSearchParametersJson = [System.Text.RegularExpressions.Regex]::Replace($savedSearchParametersJson, '"', "'")
  23.     $savedSearchResponse = armclient put /subscriptions/$($subscriptionID)/resourceGroups/mms-weu/providers/Microsoft.OperationalInsights/workspaces/NordexTest/savedSearches/$($search.Name)?api-version=2015-03-20 "$savedSearchParametersJson" | Out-String | ConvertFrom-Json
  24.     # no etag = ERROR
  25.     if(!$savedSearchResponse.PSObject.Properties.Match('etag').Count) {
  26.         Write-Error "Error with newSearchsearch: $($search.Name) Payload $($savedSearchParametersJson)"
  27.         exit
  28.     }
  29.     Write-Output "Added Search $($search.Name)"
  30.     $schedules = $($searchValue.schedules | Get-Member -MemberType *Property)
  31.     foreach ($schedule in $schedules) {
  32.  
  33.         $scheduleValue = ($searchValue.schedules  | Select-Object -ExpandProperty $schedule.Name)
  34.         # create a search or update a search
  35.  
  36.         $scheduleJson = $scheduleValue.data | ConvertTo-Json -Depth 50 | % { [System.Text.RegularExpressions.Regex]::Unescape($_) }
  37.         $scheduleJson = [System.Text.RegularExpressions.Regex]::Replace($scheduleJson, '"', "'")
  38.         $savedScheduleResponse = armclient put /subscriptions/$($subscriptionID)/resourceGroups/mms-weu/providers/Microsoft.OperationalInsights/workspaces/NordexTest/savedSearches/$($search.Name)/schedules/$($schedule.Name)?api-version=2015-03-20 "$scheduleJson" | Out-String | ConvertFrom-Json
  39.         # no etag = ERROR
  40.         if(!$savedScheduleResponse.PSObject.Properties.Match('etag').Count) {
  41.             Write-Error "Error with newSchedule: $($schedule.Name) in search: $($search.Name) Payload $($scheduleJson) Response: $($savedScheduleResponse)"
  42.             exit
  43.            
  44.         }
  45.         Write-Output "Added Schedule $($schedule.Name)"
  46.         $actions = $($scheduleValue.actions | Get-Member -MemberType *Property)
  47.         foreach ($action in $actions) {
  48.  
  49.             $actionValue = ($scheduleValue.actions  | Select-Object -ExpandProperty $action.Name)
  50.             # create a search or update a search
  51.  
  52.             $actionJson = $actionValue | ConvertTo-Json -Depth 50 | % { [System.Text.RegularExpressions.Regex]::Unescape($_) }
  53.             $actionJson = [System.Text.RegularExpressions.Regex]::Replace($actionJson, '"', "'")
  54.             $savedActionResponse = armclient put /subscriptions/$($subscriptionID)/resourceGroups/mms-weu/providers/Microsoft.OperationalInsights/workspaces/NordexTest/savedSearches/$($search.Name)/schedules/$($schedule.Name)/Actions/$($action.Name)?api-version=2015-03-20 "$actionJson" | Out-String | ConvertFrom-Json
  55.             # no etag = ERROR
  56.             if(!$savedActionResponse.PSObject.Properties.Match('etag').Count) {
  57.                 Write-Error "Error with newSchedule: $($schedule.Name) in search: $($action.Name) Payload $($actionJson) Response: $($savedActionResponse)"
  58.                 exit
  59.                
  60.             }
  61.             Write-Output "Added Action $($action.Name)"
  62.         }
  63.     }
  64.    
  65.    
  66. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement