Dela via


Så här skapar du en statisk samling

I Configuration Manager använder programmet SMS_Collection Serverns WMI-klass för att definiera attributen för en samling, till exempel medlemskapsregler och uppdateringsschemat. Egenskapen MemberClassName innehåller det systemgenererade klassnamnet som innehåller medlemmarna i samlingen.

Medlemmar i en samling anges med hjälp av direkta regler, frågeregler eller både och. Direktregler definierar en explicit resurs och frågeregler definierar en dynamisk samling som regelbundet utvärderas baserat på webbplatsens aktuella tillstånd.

Obs!

När du skapar en regel för direkt medlemskap ska du komma ihåg att regeln alltid måste ha samma namn som den dator som regeln anger.

Programmet använder klassen SMS_CollectionRuleDirect Serverns WMI-klass för att definiera direkta regler. Den här metoden används för resurser som är statiska till sin natur. Om du till exempel har ett begränsat antal licenser för ett visst program bör programmet använda direkta regler för att annonsera till specifika datorer eller användare.

Samlingarna är nära knutna till paket, program och annonser. Mer information finns i Översikt över programvarudistribution.

Följande exempel kräver följande värden:

  • Ett WMI-anslutningsobjekt (Windows Management Instrumentation).

  • Ett nytt namn på statisk samling.

  • En ny kommentar för statisk samling.

  • Flaggan "ägs av den här webbplatsen".

  • Ett resursklassnamn.

  • Ett resurs-ID.

  • En samlingsidentifierare för att begränsa medlemskapets omfattning.

Obs!

Om samlingen Alla system (SMS00001) har tagits bort från platsservern fungerar inte VBScript-exemplet.

Exempel på subrutinanropet i Visual Basic:

Call CreateStaticCollection(swbemconnection, "New Static Collection Name", "New static collection comment.", true, "SMS_R_System", 2, "SMS00001")  

Exempel på metodanropet i C#:

CreateStaticCollection (WMIConnection, "New Static Collection Name", "New static collection comment.", true, "SMS_R_System", 2, "SMS00001")  

Så här skapar du en statisk samling

  1. Konfigurera en anslutning till SMS-providern. Mer information finns i grunderna för SMS-provider.

  2. Skapa det nya samlingsobjektet med hjälp av klassen SMS_Collection Server WMI .

  3. Skapa direktregeln med hjälp av klassen SMS_CollectionRuleDirect Serverns WMI-klass .

  4. Lägg till regeln i samlingen.

  5. Uppdatera samlingen.

Exempel

Följande exempelmetod skapar en samling.

Information om hur du anropar exempelkoden finns i Anropa Configuration Manager kodfragment.


' Set up a connection to the local provider.  
Set swbemLocator = CreateObject("WbemScripting.SWbemLocator")  
Set swbemconnection= swbemLocator.ConnectServer(".", "root\sms")  
Set providerLoc = swbemconnection.InstancesOf("SMS_ProviderLocation")  

For Each Location In providerLoc  
    If location.ProviderForLocalSite = True Then  
        Set swbemconnection = swbemLocator.ConnectServer(Location.Machine, "root\sms\site_" + Location.SiteCode)  
        Exit For  
    End If  
Next  

Call CreateStaticCollection(swbemconnection, "New Static Collection Name", "New static collection comment.", true, "SMS_R_System", 2, "SMS00001")  

Sub CreateStaticCollection(connection, newCollectionName, newCollectionComment, ownedByThisSite, resourceClassName, resourceID, limitToCollectionID)  

    ' Create the collection.  
    Set newCollection = connection.Get("SMS_Collection").SpawnInstance_  
    newCollection.Comment = newCollectionComment  
    newCollection.Name = newCollectionName  
    newCollection.OwnedByThisSite = ownedByThisSite  
    newCollection.LimitToCollectionID = limitToCollectionID  

    ' Save the new collection and save the collection path for later.  
    Set collectionPath = newCollection.Put_      

    ' Create the direct rule.  
    Set newDirectRule = connection.Get("SMS_CollectionRuleDirect").SpawnInstance_  
    newDirectRule.ResourceClassName = resourceClassName  
    newDirectRule.ResourceID = resourceID  

    ' Add the new query rule to a variable.  
    Set newCollectionRule = newDirectRule  

    ' Get the collection.  
    Set newCollection = connection.Get(collectionPath.RelPath)  

    ' Add the rules to the collection.  
    newCollection.AddMembershipRule newCollectionRule  

    ' Call RequestRefresh to initiate the collection evaluator.   
    newCollection.RequestRefresh False  

End Sub  
public void CreateStaticCollection(WqlConnectionManager connection, string newCollectionName, string newCollectionComment, bool ownedByThisSite, string resourceClassName, int resourceID, string limitToCollectionID)  
{  
    try  
    {  
        // Create a new SMS_Collection object.  
        IResultObject newCollection = connection.CreateInstance("SMS_Collection");  

        // Populate new collection properties.  
        newCollection["Name"].StringValue = newCollectionName;  
        newCollection["Comment"].StringValue = newCollectionComment;  
        newCollection["OwnedByThisSite"].BooleanValue = ownedByThisSite;  
        newCollection["LimitToCollectionID"].StringValue = limitToCollectionID;   

        // Save the new collection object and properties.    
        // In this case, it seems necessary to 'get' the object again to access the properties.    
        newCollection.Put();  
        newCollection.Get();  

        // Create a new static rule object.  
        IResultObject newStaticRule = connection.CreateInstance("SMS_CollectionRuleDirect");  
        newStaticRule["ResourceClassName"].StringValue = resourceClassName;  
        newStaticRule["ResourceID"].IntegerValue = resourceID;  

        // Add the rule. Although not used in this sample, staticID contains the query identifier.                     
        Dictionary<string, object> addMembershipRuleParameters = new Dictionary<string, object>();  
        addMembershipRuleParameters.Add("collectionRule", newStaticRule);  
        IResultObject staticID = newCollection.ExecuteMethod("AddMembershipRule", addMembershipRuleParameters);  

        // Start collection evaluator.  
        Dictionary<string, object> requestRefreshParameters = new Dictionary<string, object>();  
        requestRefreshParameters.Add("IncludeSubCollections", false);  
        newCollection.ExecuteMethod("RequestRefresh", requestRefreshParameters);  

        // Output message.  
        Console.WriteLine("Created collection" + newCollectionName);  
    }  

    catch (SmsException ex)  
    {  
        Console.WriteLine("Failed to create collection. Error: " + ex.Message);  
        throw;  
    }  
}  

Exempelmetoden har följande parametrar:

Parameter Typ Beskrivning
connection -Hanterade: WqlConnectionManager
– VBScript: SWbemServices
En giltig anslutning till SMS-providern.
newCollectionName -Hanterade: String
-Vbscript: String
Det unika namn som representerar samlingen i Configuration Manager-konsolen.
newCollectionComment -Hanterade: String
-Vbscript: String
Allmän kommentar eller observera att dokument samlingen.
ownedByThisSite -Hanterade: Boolean
-Vbscript: Boolean
trueom samlingen har sitt ursprung på den lokala Configuration Manager platsen.
resourceClassName -Hanterade: String
-Vbscript: String
Resursnamnet för det statiska regelobjektet.
resourceID -Hanterade: Integer
-Vbscript: Integer
Resurs-ID:t.
limitToCollectionID -Hanterade: String
-Vbscript: String
Samlingsidentifierare för att begränsa medlemskapets omfattning.

Kompilera koden

C#-exemplet kräver:

Namnområden

System

System.Collections.Generic

System.ComponentModel

Microsoft. ConfigurationManagement.ManagementProvider

Microsoft. ConfigurationManagement.ManagementProvider.WqlQueryEngine

Församlingen

adminui.wqlqueryengine

microsoft.configurationmanagement.managementprovider

Robust programmering

Mer information om felhantering finns i Om Configuration Manager fel.

.NET Framework Säkerhet

Mer information om hur du skyddar Configuration Manager program finns i Configuration Manager rollbaserad administration.

Se även

SMS_Collection serverns WMI-klass
SMS_CollectionRuleDirect serverns WMI-klass
Översikt över programvarudistributionOm distributionsobjekt– översiktSå här ansluter du till en SMS-provider i Configuration Manager med hjälp av hanterad kod
Ansluta till en SMS-provider i Configuration Manager med hjälp av WMI