可以在 Configuration Manager 中使用 SMS_CollectionSettings 服务器 WMI 类和 SMS_ServiceWindow 服务器 WMI 类类和属性更改集合的维护时段属性。
更改维护时段的属性
设置与 SMS 提供程序的连接。 有关详细信息,请参阅 SMS 提供程序基础知识。
使用提供的现有集合 ID 获取现有集合设置实例。
使用提供的现有服务窗口 ID 获取现有服务窗口对象。
更改现有属性值 (在这种情况下,维护时段说明) 。
保存集合设置实例和属性。
注意
示例方法中的步骤包括其他步骤,主要是处理服务窗口对象的开销,这些对象作为嵌入对象存储在集合设置实例中。
示例
以下示例方法更改特定维护时段实例的属性。
重要
这假定可以修改集合实例。 在子网站中,集合由父网站拥有 () ,则情况可能并非如此。
有关调用示例代码的信息,请参阅调用Configuration Manager代码片段。
Sub ChangeMaintenanceWindowProperties(connection, _
targetCollectionID, _
targetServiceWindowID, _
newMaintenanceWindowDescription, _
newMaintenanceWindowServiceWindowSchedules, _
newMaintenanceWindowIsEnabled)
' Get the specific collection settings instance.
Set collectionSettingsInstance = connection.Get("SMS_CollectionSettings.CollectionID='" & targetCollectionID &"'" )
' Populate the local array list with the existing service window objects (from the target collection).
tempMaintenanceWindowArray = collectionSettingsInstance.ServiceWindows
' Enumerate through the array list to access each maintenance window object.
For Each maintenanceWindow in tempMaintenanceWindowArray
' If the service window ID matches the one passed in to the function, change the specific values.
If maintenanceWindow.ServiceWindowID = targetServiceWindowID Then
' Populate retrieved SMS_ServiceWindow object with the new maintenance window values.
maintenanceWindow.Description = newMaintenanceWindowDescription
maintenanceWindow.ServiceWindowSchedules = newMaintenanceWindowServiceWindowSchedules
maintenanceWindow.IsEnabled = newMaintenanceWindowIsEnabled
End If
Next
' Replace the existing service window objects from the target collection with the temporary array that includes the modified service window.
collectionSettingsInstance.ServiceWindows = tempMaintenanceWindowArray
' Save the new values in the collection settings instance associated with the collection ID.
collectionSettingsInstance.Put_
' Output success message.
wscript.echo "Maintenance Window " & targetServiceWindowID & " modified."
End Sub
public void ChangeMaintenanceWindowProperties(WqlConnectionManager connection,
string targetCollectionID,
string serviceWindowID,
string newMaintenanceWindowDescription)
{
try
{
// Create a new array list to hold the service window objects.
List<IResultObject> tempMaintenanceWindowArray = new List<IResultObject>();
// Establish connection to collection settings instance associated with the Collection ID.
IResultObject collectionSettings = connection.GetInstance(@"SMS_CollectionSettings.CollectionID='" + targetCollectionID + "'");
// Populate the array list with the existing service window objects (from the target collection).
tempMaintenanceWindowArray = collectionSettings.GetArrayItems("ServiceWindows");
// Enumerate through the array list to access each maintenance window object.
foreach (IResultObject maintenanceWindow in tempMaintenanceWindowArray)
{
// If the service window ID matches the one passed in to the function, change the specific values.
if (maintenanceWindow["ServiceWindowID"].StringValue == serviceWindowID)
{
maintenanceWindow["Description"].StringValue = newMaintenanceWindowDescription;
break;
}
}
// Replace the existing service window objects from the target collection with the temporary array that includes the new service window.
collectionSettings.SetArrayItems("ServiceWindows", tempMaintenanceWindowArray);
// Save the new values in the collection settings instance associated with the Collection ID.
collectionSettings.Put();
}
catch (SmsException ex)
{
Console.WriteLine("Failed. Error: " + ex.InnerException.Message);
throw;
}
}
示例方法具有以下参数:
参数 | 类型 | 说明 |
---|---|---|
connection swebemServices |
-管理: WqlConnectionManager - VBScript: SWbemServices |
与 SMS 提供程序的有效连接。 |
targetCollectionID |
-管理: String - VBScript: String |
集合的 ID。 |
serviceWindowID |
-管理: String - VBScript: String |
要更改其属性的维护时段的 ID。 |
newMaintenanceWindowDescription |
-管理: String - VBScript: String |
新维护时段的说明。 |
编译代码
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基于角色的管理。
另请参阅
关于维护 windows软件分发概述关于部署对象概述如何使用托管代码连接到Configuration Manager提供程序
如何使用 WMI 连接到Configuration Manager提供程序
SMS_CollectionSettings 服务器 WMI 类
SMS_ServiceWindow服务器 WMI 类
关于计划如何创建计划令牌