Aufrufen von Configuration Manager Codeausschnitten
Artikel
Die folgenden Codebeispiele zeigen, wie Sie den aufrufenden Code für die Codebeispiele einrichten, die im gesamten Configuration Manager Software Development Kit (SDK) verwendet werden.
Ersetzen Sie den CODEAUSSCHNITTMETHOD-Codeausschnitt durch den Codeausschnitt, den Sie ausführen möchten. In den meisten Fällen müssen Sie Änderungen vornehmen, z. B. Das Hinzufügen von Parametern, damit der Code funktioniert.
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()
EndIfSet connection = Connect(computer,userName,userPassword)
IfErr.Number<>0Then
Wscript.Echo "Call to connect failed"EndIfCall SNIPPETMETHODNAME (connection)
Sub SNIPPETMETHODNAME(connection)
' Insert snippet code here. EndSubFunction Connect(server, userName, userPassword)
OnErrorResumeNextDim 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")
IfUCase(net.ComputerName) = UCase(server) Then
localConnection = true
userName = ""
userPassword = ""server = "."EndIf' Connect to the server. Set swbemServices= swbemLocator.ConnectServer _
(server, "root\sms",userName,userPassword)
IfErr.Number<>0Then
Wscript.Echo "Couldn't connect: " + Err.Description
Connect = nullExitFunctionEndIf' Determine where the provider is and connect. Set providerLoc = swbemServices.InstancesOf("SMS_ProviderLocation")
ForEach location In providerLoc
If location.ProviderForLocalSite = TrueThenSet swbemServices = swbemLocator.ConnectServer _
(location.Machine, "root\sms\site_" + _
location.SiteCode,userName,userPassword)
IfErr.Number<>0Then
Wscript.Echo "Couldn't connect:" + Err.Description
Connect = NullExitFunctionEndIfSet Connect = swbemServices
ExitFunctionEndIfNextSet Connect = null' Failed to connect. EndFunction
c#
using System;
using System.Collections.Generic;
using System.Text;
using System.ComponentModel;
using Microsoft.ConfigurationManagement.ManagementProvider;
using Microsoft.ConfigurationManagement.ManagementProvider.WqlQueryEngine;
namespaceConfigurationManagerSnippets
{
classProgram
{
staticvoidMain(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);
}
}
classSnippetClass
{
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);
returnnull;
}
catch (UnauthorizedAccessException ex)
{
Console.WriteLine("Failed to authenticate. Error:" + ex.Message);
returnnull;
}
}
publicvoidSNIPPETMETHODNAME(WqlConnectionManager connection)
{
// Insert snippet code here.
}
publicstringReturnPassword()
{
string password = "";
ConsoleKeyInfo info = Console.ReadKey(true);
while (info.Key != ConsoleKey.Enter)
{
if (info.Key != ConsoleKey.Backspace)
{
password += info.KeyChar;
info = Console.ReadKey(true);
}
elseif (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;
}
}
}
In diesem Modul wird die Struktur der Namespaces erläutert, die Klassen enthalten, sowie das Abfragen von Instanzen einer Klasse. Der Text beschreibt, wie man Remotecomputer mithilfe von Ad-hoc-Verbindungen und CIM-Sitzungen abfragt.