Skip to content
This repository was archived by the owner on Mar 24, 2023. It is now read-only.

Commit 26e5a03

Browse files
authored
Merge pull request #8 from ehrnst/dev
Dev
2 parents 1c17028 + d4075b8 commit 26e5a03

File tree

7 files changed

+96
-65
lines changed

7 files changed

+96
-65
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ Bringing SCOM in to the 21. century with a Restful Web API.
4343
| ------ | ------ |
4444
| [POST] API/ComputerMaintenance | Put the specific computer object and all child in maintenance mode |
4545
| [POST] API/ObjectMaintenance | Put the specific monitoring object and all child in maintenance mode |
46-
| [POST] API/MaintenanceSchedule | Create a new maintenance schedule. SCOM 2016 ONLY |
46+
| [POST] API/MaintenanceSchedule | Create a new maintenance schedule. Supports multiple object guids in an array. SCOM 2016 ONLY |
4747

4848
### Object
4949

SCOM API/Controllers/SCOMMaintenanceController.cs

Lines changed: 63 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,15 @@
77
using Microsoft.EnterpriseManagement;
88
using Microsoft.EnterpriseManagement.Common;
99
using Microsoft.EnterpriseManagement.Monitoring;
10-
using Microsoft.EnterpriseManagement.Monitoring.MaintenanceSchedule;
1110
using System.Web;
11+
using Microsoft.SystemCenter.OperationsManagerV10.Commands;
1212
using Microsoft.EnterpriseManagement.Configuration;
1313
using Newtonsoft.Json;
1414
using System.Collections.ObjectModel;
1515
using SCOM_API.Models;
1616
using System.Configuration;
1717
using System.Web.Http.Description;
18+
using Microsoft.EnterpriseManagement.Monitoring.MaintenanceSchedule;
1819

1920
namespace SCOM_API.Controllers
2021
{
@@ -55,7 +56,7 @@ public IHttpActionResult EnableComputerMaintenance(SCOMComputerMaintenanceModel
5556
IList<ManagementPackClass> monClasses = mg.EntityTypes.GetClasses(classCriteria);
5657
MonitoringObjectCriteria criteria = new MonitoringObjectCriteria(string.Format("Name = '{0}'", Data.DisplayName.ToString()), monClasses[0]);
5758
List<MonitoringObject> monObjects = new List<MonitoringObject>();
58-
59+
5960
List<SCOMComputerMaintenanceModel> MaintenanceComputers = new List<SCOMComputerMaintenanceModel>();
6061

6162
///travers trough all classes to get monitoring objects
@@ -83,7 +84,7 @@ public IHttpActionResult EnableComputerMaintenance(SCOMComputerMaintenanceModel
8384
maintenanceComputer.EndTime = schedEndTime;
8485
maintenanceComputer.Minutes = Data.Minutes;
8586
maintenanceComputer.comment = comment;
86-
87+
8788
//add computers to list
8889
MaintenanceComputers.Add(maintenanceComputer);
8990

@@ -131,6 +132,14 @@ public IHttpActionResult EnableComputerMaintenance(SCOMComputerMaintenanceModel
131132
[Route("API/ObjectMaintenance")]
132133
public IHttpActionResult EnableObjectMaintenance(SCOMObjectMaintenanceModel Data)
133134
{
135+
//Validate input
136+
if (!ModelState.IsValid)
137+
{
138+
HttpResponseMessage message = new HttpResponseMessage(HttpStatusCode.BadRequest);
139+
message.Content = new StringContent("Missing a required parameter?");
140+
throw new HttpResponseException(message);
141+
}
142+
134143
//create a Guid from the json input
135144
var ObjectId = new Guid(Data.id);
136145
//get the monitoring object by Guid
@@ -140,41 +149,41 @@ public IHttpActionResult EnableObjectMaintenance(SCOMObjectMaintenanceModel Data
140149

141150
List<SCOMObjectMaintenanceModel> MaintenanceObjects = new List<SCOMObjectMaintenanceModel>();
142151

143-
if (!monObject.InMaintenanceMode)
152+
if (!monObject.InMaintenanceMode)
153+
{
144154
{
145-
{
146-
//set maintenance properties
147-
DateTime startTime = DateTime.UtcNow;
148-
DateTime schedEndTime = DateTime.UtcNow.AddMinutes(Data.Minutes);
149-
MaintenanceModeReason reason = MaintenanceModeReason.PlannedOther;
150-
string comment = Data.comment;
155+
//set maintenance properties
156+
DateTime startTime = DateTime.UtcNow;
157+
DateTime schedEndTime = DateTime.UtcNow.AddMinutes(Data.Minutes);
158+
MaintenanceModeReason reason = MaintenanceModeReason.PlannedOther;
159+
string comment = Data.comment;
151160

152-
monObject.ScheduleMaintenanceMode(startTime, schedEndTime, reason, comment);
161+
monObject.ScheduleMaintenanceMode(startTime, schedEndTime, reason, comment);
153162

154-
//Add properties to list
155-
SCOMObjectMaintenanceModel maintenanceObject = new SCOMObjectMaintenanceModel();
156-
maintenanceObject.displayName = monObject.DisplayName;
157-
maintenanceObject.id = monObject.Id.ToString();
158-
maintenanceObject.EndTime = schedEndTime;
159-
maintenanceObject.Minutes = Data.Minutes;
160-
maintenanceObject.comment = comment;
163+
//Add properties to list
164+
SCOMObjectMaintenanceModel maintenanceObject = new SCOMObjectMaintenanceModel();
165+
maintenanceObject.displayName = monObject.DisplayName;
166+
maintenanceObject.id = monObject.Id.ToString();
167+
maintenanceObject.EndTime = schedEndTime;
168+
maintenanceObject.Minutes = Data.Minutes;
169+
maintenanceObject.comment = comment;
161170

162-
//add computers to list
163-
MaintenanceObjects.Add(maintenanceObject);
171+
//add computers to list
172+
MaintenanceObjects.Add(maintenanceObject);
164173

165-
}
166174
}
175+
}
167176

168-
//If object already in maintenance. Throw conflict message
169-
else
170-
{
171-
MaintenanceWindow MaintenanceWindow = monObject.GetMaintenanceWindow();
177+
//If object already in maintenance. Throw conflict message
178+
else
179+
{
180+
MaintenanceWindow MaintenanceWindow = monObject.GetMaintenanceWindow();
172181

173-
HttpResponseMessage message = new HttpResponseMessage(HttpStatusCode.Conflict);
174-
message.Content = new StringContent("Object already in maintenance until" + MaintenanceWindow.ScheduledEndTime);
175-
throw new HttpResponseException(message);
182+
HttpResponseMessage message = new HttpResponseMessage(HttpStatusCode.Conflict);
183+
message.Content = new StringContent("Object already in maintenance until" + MaintenanceWindow.ScheduledEndTime);
184+
throw new HttpResponseException(message);
176185

177-
}
186+
}
178187

179188
//Return list of computers as Json
180189
return Json(MaintenanceObjects);
@@ -188,15 +197,16 @@ public IHttpActionResult EnableObjectMaintenance(SCOMObjectMaintenanceModel Data
188197
/// <example>
189198
/// {
190199
/// "scheduleName": "string",
191-
/// "id": "monitoringObjectId",
200+
/// "id": "[monitoringObjectId]",
192201
/// "StartTime": "2017-05-22T07:01:00.374Z",
193202
/// "EndTime": "2017-05-22T08:01:00.374Z",
194203
/// "comment": "doing maintenance"
195204
/// }
196205
/// </example>
197206
/// <remarks>
198207
/// ##SCOM 2016 Only##
199-
/// Only non recurring schedules with one object is supported. Maintenance reason is hard coded to 'PlannedOther'
208+
/// Only non recurring schedules are supported. Maintenance reason will be hard coded to 'PlannedOther'
209+
/// Use the other endpoints to obtain your MonitoringObjectId
200210
/// </remarks>
201211
/// <response code="201">Successfully added maintenance mode for the object</response>
202212
/// <response code="400">Bad request. Check json input</response>
@@ -206,15 +216,24 @@ public IHttpActionResult EnableObjectMaintenance(SCOMObjectMaintenanceModel Data
206216
[Route("API/MaintenanceSchedule")]
207217
public IHttpActionResult ScheduleObjectMaintenance(SCOMObjectSchedMaintenanceModel Data)
208218
{
209-
210-
//create a Guid from the json input
211-
var ObjectId = new Guid(Data.id);
212-
//get the monitoring object by Guid
213-
var monObject = mg.EntityObjects.GetObject<MonitoringObject>(ObjectId, ObjectQueryOptions.Default);
219+
//Validate post
220+
if (!ModelState.IsValid)
221+
{
222+
HttpResponseMessage message = new HttpResponseMessage(HttpStatusCode.BadRequest);
223+
message.Content = new StringContent("Missing a required parameter?");
224+
throw new HttpResponseException(message);
225+
}
226+
227+
//LOOP through the id array and add each GUID to the object list
214228
System.Collections.Generic.List<System.Guid> ObjectList = new System.Collections.Generic.List<System.Guid>();
229+
string[] array = Data.id;
230+
foreach (string s in array)
231+
{
232+
var item = new Guid(s);
215233

216-
//add the monitoring object guids to list
217-
ObjectList.Add(monObject.Id);
234+
ObjectList.Add(item);
235+
}
236+
//
218237

219238
//create a recurrencePattern this is 'sourced' from OmCommands.10.dll ( new-scommaintenanceSchedule CMDLET )
220239
//read more: https://docs.microsoft.com/en-us/powershell/systemcenter/systemcenter2016/operationsmanager/vlatest/new-scommaintenanceschedule
@@ -235,27 +254,26 @@ public IHttpActionResult ScheduleObjectMaintenance(SCOMObjectSchedMaintenanceMod
235254
int duration = (int)span.TotalMinutes;
236255

237256
//Create the Maintenance schedule
238-
MaintenanceSchedule Sched = new MaintenanceSchedule(mg, displayname, recursive, isEnabled, ObjectList, duration, activeStartTime, activeEndDate, MaintenanceModeReason.PlannedOther, comments, isRecurrence, recurrencePattern );
239-
257+
MaintenanceSchedule Sched = new MaintenanceSchedule(mg, displayname, recursive, isEnabled, ObjectList, duration, activeStartTime, activeEndDate, MaintenanceModeReason.PlannedOther, comments, isRecurrence, recurrencePattern);
240258

241259
//Create the maintenance schedule
242260
System.Guid guid = MaintenanceSchedule.CreateMaintenanceSchedule(Sched, mg);
243261

244-
//Add properties to class and return the list as Json
262+
//Add properties to class
245263
var shed = MaintenanceSchedule.GetMaintenanceScheduleById(guid, mg);
246264
List<SCOMObjectSchedMaintenanceModel> MaintenanceScheduleList = new List<SCOMObjectSchedMaintenanceModel>();
247-
SCOMObjectSchedMaintenanceModel mSched = new SCOMObjectSchedMaintenanceModel ();
248-
mSched.id = shed.ScheduleId.ToString();
265+
SCOMObjectSchedMaintenanceModel mSched = new SCOMObjectSchedMaintenanceModel();
266+
mSched.id = array;
249267
mSched.StartTime = shed.ActiveStartTime;
250268
mSched.EndTime = shed.ScheduledEndTime;
251269
mSched.scheduleName = shed.ScheduleName;
252270
mSched.comment = shed.Comments;
253271

254272
MaintenanceScheduleList.Add(mSched);
255-
273+
//return the post/class as Json
256274
return Json(MaintenanceScheduleList);
257275

258-
276+
259277
}
260278

261279
}

SCOM API/Models/MaintenanceScheduleModel.cs

Lines changed: 0 additions & 12 deletions
This file was deleted.

SCOM API/Models/SCOMObjectMaintenanceModel.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using System;
22
using System.Collections.Generic;
3+
using System.ComponentModel.DataAnnotations;
34
using System.Linq;
45
using System.Web;
56

@@ -10,12 +11,14 @@ public class SCOMObjectMaintenanceModel
1011
/// <summary>
1112
/// Monitoring object id
1213
/// </summary>
14+
[Required]
1315
public string id { get; set; }
1416

1517
public string displayName { get; set; }
1618
/// <summary>
1719
/// Minutes to maintenance
1820
/// </summary>
21+
[Required]
1922
public int Minutes { get; set; }
2023

2124
public DateTime EndTime { get; set; }

SCOM API/Models/SCOMObjectScheduleMaintenanceModel.cs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using System;
22
using System.Collections.Generic;
3+
using System.ComponentModel.DataAnnotations;
34
using System.Linq;
45
using System.Web;
56

@@ -10,19 +11,24 @@ public class SCOMObjectSchedMaintenanceModel
1011
/// <summary>
1112
/// Name of the maintenance schedule
1213
/// </summary>
14+
[Required]
1315
public string scheduleName { get; set; }
16+
[Required]
1417
/// <summary>
15-
/// Monitoring object ID
18+
/// Monitoring object ID(s)
1619
/// </summary>
17-
public string id { get; set; }
20+
public string[] id { get; set; }
21+
[Required]
1822
/// <summary>
1923
/// Start time and date
2024
/// </summary>
2125
public DateTime StartTime { get; set; }
26+
[Required]
2227
/// <summary>
2328
/// End time and date
2429
/// </summary>
2530
public DateTime EndTime { get; set; }
31+
[Required]
2632
/// <summary>
2733
/// Comment
2834
/// </summary>
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<!--
3+
This file is used by the publish/package process of your Web project. You can customize the behavior of this process
4+
by editing this MSBuild file. In order to learn more about this please visit http://go.microsoft.com/fwlink/?LinkID=208121.
5+
-->
6+
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
7+
<PropertyGroup>
8+
<WebPublishMethod>FileSystem</WebPublishMethod>
9+
<LastUsedBuildConfiguration>Release</LastUsedBuildConfiguration>
10+
<LastUsedPlatform>Any CPU</LastUsedPlatform>
11+
<SiteUrlToLaunchAfterPublish />
12+
<LaunchSiteAfterPublish>True</LaunchSiteAfterPublish>
13+
<ExcludeApp_Data>False</ExcludeApp_Data>
14+
<publishUrl>C:\temp\scomapi\test</publishUrl>
15+
<DeleteExistingFiles>False</DeleteExistingFiles>
16+
</PropertyGroup>
17+
</Project>

SCOM API/SCOM API.csproj

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -120,15 +120,15 @@
120120
</Reference>
121121
<Reference Include="Microsoft.EnterpriseManagement.Core, Version=7.0.5000.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
122122
<SpecificVersion>False</SpecificVersion>
123-
<HintPath>\\i2v-scom16t-001\c$\Program Files\Microsoft System Center 2016\Operations Manager\Server\SDK Binaries\Microsoft.EnterpriseManagement.Core.dll</HintPath>
123+
<HintPath>\\distr\users\aa98\PUBLIC\Microsoft.EnterpriseManagement.Core.dll</HintPath>
124124
</Reference>
125125
<Reference Include="Microsoft.EnterpriseManagement.OperationsManager, Version=7.0.5000.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
126126
<SpecificVersion>False</SpecificVersion>
127-
<HintPath>\\i2v-scom16t-001\c$\Program Files\Microsoft System Center 2016\Operations Manager\Server\SDK Binaries\Microsoft.EnterpriseManagement.OperationsManager.dll</HintPath>
127+
<HintPath>\\distr\users\aa98\PUBLIC\Microsoft.EnterpriseManagement.OperationsManager.dll</HintPath>
128128
</Reference>
129129
<Reference Include="Microsoft.EnterpriseManagement.Runtime, Version=7.0.5000.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
130130
<SpecificVersion>False</SpecificVersion>
131-
<HintPath>\\i2v-scom16t-001\c$\Program Files\Microsoft System Center 2016\Operations Manager\Server\SDK Binaries\Microsoft.EnterpriseManagement.Runtime.dll</HintPath>
131+
<HintPath>\\distr\users\aa98\PUBLIC\Microsoft.EnterpriseManagement.Runtime.dll</HintPath>
132132
</Reference>
133133
<Reference Include="Microsoft.Extensions.Configuration.Abstractions, Version=1.0.0.0, Culture=neutral, PublicKeyToken=adb9793829ddae60, processorArchitecture=MSIL">
134134
<HintPath>..\packages\Microsoft.Extensions.Configuration.Abstractions.1.0.0\lib\netstandard1.0\Microsoft.Extensions.Configuration.Abstractions.dll</HintPath>
@@ -273,7 +273,6 @@
273273
<Compile Include="Global.asax.cs">
274274
<DependentUpon>Global.asax</DependentUpon>
275275
</Compile>
276-
<Compile Include="Models\MaintenanceScheduleModel.cs" />
277276
<Compile Include="Models\SCOMComputerModelDetailed.cs" />
278277
<Compile Include="Models\SCOMObjectScheduleMaintenanceModel.cs" />
279278
<Compile Include="Models\SCOMObjectMaintenanceModel.cs" />
@@ -284,6 +283,7 @@
284283
</ItemGroup>
285284
<ItemGroup>
286285
<Content Include="packages.config" />
286+
<None Include="Properties\PublishProfiles\web.pubxml" />
287287
<None Include="Web.Debug.config">
288288
<DependentUpon>Web.config</DependentUpon>
289289
</None>
@@ -293,7 +293,6 @@
293293
</ItemGroup>
294294
<ItemGroup>
295295
<Folder Include="App_Data\" />
296-
<Folder Include="Properties\PublishProfiles\" />
297296
</ItemGroup>
298297
<ItemGroup>
299298
<WCFMetadata Include="Service References\" />

0 commit comments

Comments
 (0)