アプリケーションでは、SMS_CollectionSettings サーバー WMI クラスと SMS_ServiceWindow Server WMI クラスのクラスとプロパティを使用して、Configuration Managerメンテナンス期間を作成できます。
メンテナンス期間を作成するには
SMS プロバイダーへの接続を設定します。 詳細については、「 SMS プロバイダーの基礎」を参照してください。
指定されたコレクション ID を使用して、既存のコレクション設定インスタンスを取得します。
SMS_ServiceWindow サーバー WMI クラスを使用して、新しいサービス ウィンドウ オブジェクトのプロパティを作成して設定します。
前に取得したコレクション設定インスタンスに新しい
SMS_ServiceWindow
オブジェクトを追加します。コレクション設定のインスタンスとプロパティを保存します。
注:
次の例では、主に、コレクション設定インスタンスに埋め込みオブジェクトとして格納されるメンテナンス ウィンドウ オブジェクトを処理するオーバーヘッドを処理するための追加の手順が含まれています。
例
次のメソッドの例では、コレクション インスタンスが変更可能であると仮定して、コレクションのメンテナンス期間を作成します。 これは、コレクションが親サイトによって所有されている子サイトでは当てはめにならない場合があります。
サンプル コードの呼び出しについては、「Configuration Manager コード スニペットの呼び出し」を参照してください。
Sub CreateMaintenanceWindow(connection, _
targetCollectionID, _
newMaintenanceWindowName, _
newMaintenanceWindowDescription, _
newMaintenanceWindowServiceWindowSchedules, _
newMaintenanceWindowIsEnabled, _
newMaintenanceWindowServiceWindowType)
' Build a query to get the specified collection.
collectionSettingsQuery = "SMS_CollectionSettings.CollectionID='" & targetCollectionID & "'"
' Get the collection settings instance for the targetCollectionID.
Set allCollectionSettings = connection.ExecQuery("Select * From SMS_CollectionSettings Where CollectionID = '" & targetCollectionID & "'")
' If a collection settings instance does not exist for the target collection, create one.
If allCollectionSettings.Count = 0 Then
Wscript.Echo "Creating collection settings instance."
Set collectionSettingsInstance = connection.Get("SMS_CollectionSettings").SpawnInstance_
collectionSettingsInstance.CollectionID = targetCollectionID
collectionSettingsInstance.Put_
End If
' Get the specific collection settings instance.
Set collectionSettingsInstance = connection.Get("SMS_CollectionSettings.CollectionID='" & targetCollectionID &"'" )
' Create and populate a temporary SMS_ServiceWindow object with the new maintenance window values.
Set tempServiceWindowObject = connection.Get("SMS_ServiceWindow").SpawnInstance_
' Populate temporary SMS_ServiceWindow object with the new maintenance window values.
tempServiceWindowObject.Name = newMaintenanceWindowName
tempServiceWindowObject.Description = newMaintenanceWindowDescription
tempServiceWindowObject.ServiceWindowSchedules = newMaintenanceWindowServiceWindowSchedules
tempServiceWindowObject.IsEnabled = newMaintenanceWindowIsEnabled
tempServiceWindowObject.ServiceWindowType = newMaintenanceWindowServiceWindowType
' Populate the local array list with the existing service window objects (from the target collection).
tempServiceWindowArray = collectionSettingsInstance.ServiceWindows
' Add the newly created service window object to the temporary array.
ReDim Preserve tempServiceWindowArray (Ubound(tempServiceWindowArray) + 1)
Set tempServiceWindowArray(Ubound(tempServiceWindowArray)) = tempServiceWindowObject
' Replace the existing service window objects from the target collection with the temporary array that includes the new service window.
collectionSettingsInstance.ServiceWindows = tempServiceWindowArray
' Save the collection settings instance with the new service window object.
collectionSettingsInstance.Put_
' Output success message.
wscript.echo "New Maintenance Window created."
End Sub
public void CreateMaintenanceWindow(WqlConnectionManager connection,
string targetCollectionID,
string newMaintenanceWindowName,
string newMaintenanceWindowDescription,
string newMaintenanceWindowServiceWindowSchedules,
bool newMaintenanceWindowIsEnabled,
int newMaintenanceWindowServiceWindowType)
{
try
{
// Create an object to hold the collection settings instance (used to check whether a collection settings instance exists).
IResultObject collectionSettingsInstance = null;
// Get the collection settings instance for the targetCollectionID.
IResultObject allCollectionSettings = connection.QueryProcessor.ExecuteQuery("Select * from SMS_CollectionSettings where CollectionID='" + targetCollectionID + "'");
// Enumerate the allCollectionSettings collection (there should be just one item) and save the instance.
foreach (IResultObject collectionSetting in allCollectionSettings)
{
collectionSettingsInstance = collectionSetting;
}
// If a collection settings instance does not exist for the target collection, create one.
if (collectionSettingsInstance == null)
{
collectionSettingsInstance = connection.CreateInstance("SMS_CollectionSettings");
collectionSettingsInstance["CollectionID"].StringValue = targetCollectionID;
collectionSettingsInstance.Put();
collectionSettingsInstance.Get();
}
// Create a new array list to hold the service window object.
List<IResultObject> tempServiceWindowArray = new List<IResultObject>();
// Create and populate a temporary SMS_ServiceWindow object with the new maintenance window values.
IResultObject tempServiceWindowObject = connection.CreateEmbeddedObjectInstance("SMS_ServiceWindow");
// Populate temporary SMS_ServiceWindow object with the new maintenance window values.
tempServiceWindowObject["Name"].StringValue = newMaintenanceWindowName;
tempServiceWindowObject["Description"].StringValue = newMaintenanceWindowDescription;
tempServiceWindowObject["ServiceWindowSchedules"].StringValue = newMaintenanceWindowServiceWindowSchedules;
tempServiceWindowObject["IsEnabled"].BooleanValue = newMaintenanceWindowIsEnabled;
tempServiceWindowObject["ServiceWindowType"].IntegerValue = newMaintenanceWindowServiceWindowType;
// Populate the local array list with the existing service window objects (from the target collection).
tempServiceWindowArray = collectionSettingsInstance.GetArrayItems("ServiceWindows");
// Add the newly created service window object to the local array list.
tempServiceWindowArray.Add(tempServiceWindowObject);
// Replace the existing service window objects from the target collection with the temporary array that includes the new service window.
collectionSettingsInstance.SetArrayItems("ServiceWindows", tempServiceWindowArray);
// Save the new values in the collection settings instance associated with the target collection.
collectionSettingsInstance.Put();
}
catch (SmsException ex)
{
Console.WriteLine("Failed. Error: " + ex.InnerException.Message);
throw;
}
}
このメソッドの例には、次のパラメーターがあります。
パラメーター | 型 | 説明 |
---|---|---|
connection swebemServices |
-管理: WqlConnectionManager - VBScript: SWbemServices |
SMS プロバイダーへの有効な接続。 |
targetCollectionID |
-管理: String -Vbscript: String |
コレクションの ID。 |
newMaintenanceWindowName |
-管理: String -Vbscript: String |
新しいメンテナンス期間の名前。 |
newMaintenanceWindowDescription |
-管理: String -Vbscript: String |
新しいメンテナンス期間の説明。 |
newMaintenanceWindowServiceWindowSchedules |
-管理: String -Vbscript: String |
新しいメンテナンス期間のサービス スケジュール。 |
newMaintenanceWindowIsEnabled |
-管理: Boolean -Vbscript: Boolean |
true 新しいメンテナンス期間が有効になっている場合。 |
newMaintenanceWindowServiceWindowType |
-管理: Integer -Vbscript: Integer |
新しいメンテナンス期間に「」と入力します。 |
コードのコンパイル
C# の例では、次のものが必要です。
名前空間
System
System.Collections.Generic
System.ComponentModel
Microsoft。ConfigurationManagement.ManagementProvider
Microsoft。ConfigurationManagement.ManagementProvider.WqlQueryEngine
Assembly
adminui.wqlqueryengine
microsoft.configurationmanagement.managementprovider
堅牢なプログラミング
エラー処理の詳細については、「Configuration Manager エラーについて」を参照してください。
.NET Framework のセキュリティ
Configuration Manager アプリケーションのセキュリティ保護の詳細については、「ロールベースの管理Configuration Manager」を参照してください。
関連項目
メンテナンス期間について ソフトウェア配布の概要展開についてオブジェクトの概要マネージド コードを使用してConfiguration Manager プロバイダーに接続する方法
WMI を使用してConfiguration Manager プロバイダーに接続する方法
SMS_CollectionSettings サーバー WMI クラス
SMS_ServiceWindow サーバー WMI クラス
スケジュールについて スケジュールトークンを作成する方法