Configuration Manager 코드 조각 호출

다음 코드 샘플에서는 Configuration Manager SDK(소프트웨어 개발 키트)에서 사용되는 코드 예제에 대한 호출 코드를 설정하는 방법을 보여 줍니다.

SNIPPETMETHOD 코드 조각을 실행하려는 코드 조각으로 바꿉니다. 대부분의 경우 코드를 작동하려면 매개 변수 추가와 같은 변경을 수행해야 합니다.

원격 WMI(Windows Management Instrumentation) 연결에 대한 자세한 내용은 원격 컴퓨터에서 WMI에 연결을 참조하세요.

예시

Dim connection  
Dim computer  
Dim userName  
Dim userPassword  
Dim password 'Password object  

Wscript.StdOut.Write "Computer you want to connect to (Enter . for local): "  
computer = WScript.StdIn.ReadLine  

If computer = "." Then  
    userName = ""  
    userPassword = ""  
Else  
    Wscript.StdOut.Write "Please enter the user name: "  
    userName = WScript.StdIn.ReadLine  

    Set password = CreateObject("ScriptPW.Password")   
    WScript.StdOut.Write "Please enter your password:"   
    userPassword = password.GetPassword()   
End If  

Set connection = Connect(computer,userName,userPassword)  

If Err.Number<>0 Then  
    Wscript.Echo "Call to connect failed"  
End If  

Call SNIPPETMETHODNAME (connection)  

Sub SNIPPETMETHODNAME(connection)  
   ' Insert snippet code here.  
End Sub  

Function Connect(server, userName, userPassword)  

    On Error Resume Next  

    Dim net  
    Dim localConnection  
    Dim swbemLocator  
    Dim swbemServices  
    Dim providerLoc  
    Dim location  

    Set swbemLocator = CreateObject("WbemScripting.SWbemLocator")  

    swbemLocator.Security_.AuthenticationLevel = 6 'Packet Privacy  

    ' If  the server is local, don not supply credentials.  
    Set net = CreateObject("WScript.NetWork")   
    If UCase(net.ComputerName) = UCase(server) Then  
        localConnection = true  
        userName = ""  
        userPassword = ""  
        server = "."  
    End If  

    ' Connect to the server.  
    Set swbemServices= swbemLocator.ConnectServer _  
            (server, "root\sms",userName,userPassword)  
    If Err.Number<>0 Then  
        Wscript.Echo "Couldn't connect: " + Err.Description  
        Connect = null  
        Exit Function  
    End If  

    ' Determine where the provider is and connect.  
    Set providerLoc = swbemServices.InstancesOf("SMS_ProviderLocation")  

        For Each location In providerLoc  
            If location.ProviderForLocalSite = True Then  
                Set swbemServices = swbemLocator.ConnectServer _  
                 (location.Machine, "root\sms\site_" + _  
                    location.SiteCode,userName,userPassword)  
                If Err.Number<>0 Then  
                    Wscript.Echo "Couldn't connect:" + Err.Description  
                    Connect = Null  
                    Exit Function  
                End If  
                Set Connect = swbemServices  
                Exit Function  
            End If  
        Next  
    Set Connect = null ' Failed to connect.  
End Function  

using System;  
using System.Collections.Generic;  
using System.Text;  
using System.ComponentModel;  
using Microsoft.ConfigurationManagement.ManagementProvider;  
using Microsoft.ConfigurationManagement.ManagementProvider.WqlQueryEngine;  

namespace ConfigurationManagerSnippets  
{  
    class Program  
    {  
        static void Main(string[] args)  
        {  
            // Setup snippet class.  

            string computer = "";  
            string userName = "";  
            string password = "";  

            SnippetClass snippets = new SnippetClass();  

            Console.WriteLine("Computer you want to connect to (Enter . for local): ");  
            computer = Console.ReadLine();  
            Console.WriteLine();  

            if (computer == ".")  
            {  
                computer = System.Net.Dns.GetHostName();  
                userName = "";  
                password = "";  
            }  
            else  
            {  
                Console.WriteLine("Please enter the user name: ");  
                userName = Console.ReadLine();  

                Console.WriteLine("Please enter your password:");  
                password = snippets.ReturnPassword();  
            }  

            // Make connection to provider.  
            WqlConnectionManager WMIConnection = snippets.Connect(computer, userName, password);  

            // Call snippet function and pass the provider connection object.  
            snippets.SNIPPETMETHODNAME(WMIConnection);  
        }  
    }  

    class SnippetClass  
    {  
        public WqlConnectionManager Connect(string serverName, string userName, string userPassword)  
        {  
            try  
            {                 
                SmsNamedValuesDictionary namedValues = new SmsNamedValuesDictionary();  
                WqlConnectionManager connection = new WqlConnectionManager(namedValues);  
                if (System.Net.Dns.GetHostName().ToUpper() == serverName.ToUpper())  
                {  
                    connection.Connect(serverName);  
                }  
                else  
                {  
                    connection.Connect(serverName, userName, userPassword);  
                }  
                return connection;  
            }  
            catch (SmsException ex)  
            {  
                Console.WriteLine("Failed to Connect. Error: " + ex.Message);  
                return null;  
            }  
            catch (UnauthorizedAccessException ex)  
            {  
                Console.WriteLine("Failed to authenticate. Error:" + ex.Message);  
                return null;  
            }  
        }  

        public void SNIPPETMETHODNAME(WqlConnectionManager connection)  
        {  
            // Insert snippet code here.  
        }  

        public string ReturnPassword()  
        {  
            string password = "";  
            ConsoleKeyInfo info = Console.ReadKey(true);  
            while (info.Key != ConsoleKey.Enter)  
            {  
                if (info.Key != ConsoleKey.Backspace)  
                {  
                    password += info.KeyChar;  
                    info = Console.ReadKey(true);  
                }  
                else if (info.Key == ConsoleKey.Backspace)  
                {  
                    if (!string.IsNullOrEmpty(password))  
                    {  
                        password = password.Substring  
                        (0, password.Length - 1);  
                    }  
                    info = Console.ReadKey(true);  
                }  
            }  
            for (int i = 0; i < password.Length; i++)  
                Console.Write("*");  
            return password;  
        }  
    }  
}  

코드 컴파일

네임 스페이스

시스템

Microsoft. ConfigurationManagement.ManagementProvider

Microsoft. ConfigurationManagement.ManagementProvider.WqlQueryEngine

어셈블리

adminui.wqlqueryengine

microsoft.configurationmanagement.managementprovider

참고

어셈블리는 <Program Files>\Microsoft Endpoint Manager\AdminConsole\bin 폴더에 있습니다.

런타임 요구 사항

자세한 내용은 Configuration Manager 서버 런타임 요구 사항을 참조하세요.

강력한 프로그래밍

발생할 수 있는 Configuration Manager 예외는 SmsConnectionExceptionSmsQueryException입니다. SmsException과 함께 사용할 수 있습니다.