Advertisement
Hilkoqhh

Untitled

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