Get SCCM Software Center Applications using CIMSession
UPDATE :
@TimonYang,
First of all thank you for your quick reply!
Regarding to the error, if you didn't import the required reference yet, you should import it from :
C:\Windows\Microsoft.NET\assembly\GAC_MSIL\Microsoft.Management.Infrastructure\v4.0_1.0.0.0__31bf3856ad364e35\Microsoft.Management.Infrastructure.dll
Then i forgot to add the code for the connection. Here it is :
on my class just in the top I have the following public variables
public CimCredential connection { get; set; }
public WSManSessionOptions SessionOptions { get; set; }
public CimSession SystemSession { get; set; }
and the then I have the following function to make the connection :
public void CreateConnectionOptions(string hostname, string usrname, string usrpass, string domain)
{
SecureString secpass = ConvertToSecureString(usrpass);
try
{
connection = new CimCredential(PasswordAuthenticationMechanism.Default,
domain,
usrname,
secpass);
// create SessionOptions using connection
SessionOptions = new WSManSessionOptions();
SessionOptions.AddDestinationCredentials(connection);
// create Session using hostname, SessionOptions
SystemSession = CimSession.Create(hostname, SessionOptions);
}
catch (UnauthorizedAccessException error)
{
MessageBox.Show("Can't connect to remote machine with error : " + error.Message, "AdminToolBox",
MessageBoxButtons.OK,
MessageBoxIcon.Error);
return;
}
}
I also have the function below to convert the password to secure string :
private SecureString ConvertToSecureString(string password)
{
if (password == null)
throw new ArgumentNullException("password");
var securePassword = new SecureString();
foreach (char c in password)
securePassword.AppendChar(c);
securePassword.MakeReadOnly();
return securePassword;
}
Note: the following libraries are required :
using Microsoft.Management.Infrastructure;
using Microsoft.Management.Infrastructure.Options;
using System.Security;
Making the connection : CreateConnectionOptions("HOSTNAME", "USERNAME","PASSWORD","DOMAIN");
The function to retrieve data works fine to retrieve data from other classes, like Win32_InstalledStoreProgram, win32_quickfixengineering, CCM_UpdateStatus, SMS_InstalledSoftware
Hi,
I'm being moving all my functions from WMI (System.Management) to CIMSession(Microsoft.Management.Infrastructure) using C#. I have a tool to retrieve applications from ARP, processes and so on, it also retrieves all applications from sccm client software center, for the applications coming from the class "CCM_Application" I'm getting stuck to use CIMSession! When it tries to retrieve the applications I always get the following error : "The WS-Management service cannot process the request. The request contains an invalid datetime. The string 2020-00-07T00:00:00Z does not represent any of these types: xs:dateTime, xs:date, or xs:time."
Here my code :
// Function to retrieve all data from a CLASS
public IEnumerable<CimInstance> GetCimInfo(string strnamespace, string strquery)
{
IEnumerable<CimInstance> ReturnValue = null;
try
{
ReturnValue = SystemSession.QueryInstances(@strnamespace, "WQL", strquery);
if (ReturnValue != null)
{
foreach (CimInstance cmiObj in ReturnValue)
{
return ReturnValue;
}
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
// didn't find anything
return null;
}
Then I have a function to retrieve all Applications Data as I want :
public class cmapps
{
public string cmappFullName { get; set; }
public string cmappSoftwareVersion { get; set; }
public string cmappRevision { get; set; }
public string cmappDescription { get; set; }
public string cmappDeploymentReport { get; set; }
public string cmappConfigureState { get; set; }
public string cmappLastEvalTime { get; set; }
public string cmappLastInstallTime { get; set; }
public string cmappErrorCode { get; set; }
public string cmappEvaluationState { get; set; }
public string cmappinstallstate { get; set; }
public string cmappResolvedState { get; set; }
public string cmappID { get; set; }
public string cmappMachinetarget { get; set; }
public string cmappPercentComplete { get; set; }
}
public List<cmapps> listcmapps = new List<cmapps>();
//CIM Way
public void GetCMApps()
{
listcmapps.Clear();
string getcmappfullname = "";
string getcmappSoftwareVersion = "";
string getcmappRevision = "";
string getcmappDescription = "";
string getcmappDeploymentReport = "";
string getcmappConfigureState = "";
string getcmappLastEvalTime = "";
string getcmappLastInstallTime = "";
string getcmappErrorCode = "";
string getcmappEvaluationState = "";
string getcmappInstallState = "";
string getcmappResolvedState = "";
string getcmappID = "";
string getcmappMachinetarget = "";
string getcmappPercentComplete = "";
string evalstate = "";
IEnumerable<CimInstance> objectSearcher = null;
try
{
objectSearcher = GetCimInfo(@"ROOT\CCM\ClientSDK", "SELECT * From CCM_Application");
if (objectSearcher != null)
{
foreach (var cmiObj in objectSearcher)
{
getcmappfullname = cmiObj.CimInstanceProperties["FullName"].Value.ToString();
getcmappSoftwareVersion = cmiObj.CimInstanceProperties["SoftwareVersion"].Value.ToString();
getcmappRevision = cmiObj.CimInstanceProperties["Revision"].Value.ToString();
getcmappDescription = cmiObj.CimInstanceProperties["Description"].Value.ToString();
getcmappDeploymentReport = cmiObj.CimInstanceProperties["DeploymentReport"].Value.ToString();
getcmappConfigureState = cmiObj.CimInstanceProperties["ConfigureState"].Value.ToString();
getcmappLastEvalTime = cmiObj.CimInstanceProperties["LastEvalTime"].Value.ToString();
getcmappLastInstallTime = cmiObj.CimInstanceProperties["LastInstallTime"].Value.ToString();
getcmappErrorCode = cmiObj.CimInstanceProperties["ErrorCode"].Value.ToString();
getcmappEvaluationState = cmiObj.CimInstanceProperties["EvaluationState"].Value.ToString();
getcmappInstallState = cmiObj.CimInstanceProperties["InstallState"].Value.ToString();
getcmappResolvedState = cmiObj.CimInstanceProperties["ResolvedState"].Value.ToString();
getcmappID = cmiObj.CimInstanceProperties["ID"].Value.ToString();
getcmappMachinetarget = cmiObj.CimInstanceProperties["IsMachineTarget"].Value.ToString();
getcmappPercentComplete = cmiObj.CimInstanceProperties["PercentComplete"].Value.ToString();
listcmapps.Add(new cmapps()
{
cmappFullName = getcmappfullname,
cmappSoftwareVersion = getcmappSoftwareVersion,
cmappRevision = getcmappRevision,
cmappDescription = getcmappDescription,
cmappDeploymentReport = getcmappDeploymentReport,
cmappConfigureState = getcmappConfigureState,
cmappLastEvalTime = getcmappLastEvalTime,
cmappLastInstallTime = getcmappLastInstallTime,
cmappErrorCode = getcmappErrorCode,
cmappEvaluationState = getcmappEvaluationState,
cmappinstallstate = getcmappInstallState,
cmappResolvedState = getcmappResolvedState,
cmappID = getcmappID,
cmappMachinetarget = getcmappMachinetarget,
cmappPercentComplete = getcmappPercentComplete
});
}
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message + "\n" + "Can't connect to the remote machine!", "AdminToolBox",
MessageBoxButtons.OK,
MessageBoxIcon.Error);
}
}
// Calling the function
listcmapps.Clear();
listView1.Clear();
listView1.View = View.Details;
listView1.GridLines = true;
listView1.FullRowSelect = true;
//Add column header
listView1.Columns.Add("FullName", 70);
listView1.Columns.Add("SoftwareVersion", 70);
listView1.Columns.Add("Revision", 70);
listView1.Columns.Add("Status", 70);
listView1.Columns.Add("ConfigureState", 70);
listView1.Columns.Add("LastEvalTime", 70);
listView1.Columns.Add("LastInstallTime", 70);
listView1.Columns.Add("ErrorCode", 70);
listView1.Columns.Add("InstallState", 70);
listView1.Columns.Add("ResolvedState", 70);
//Add items in the listview
string[] arr = new string[10];
ListViewItem itm;
try
{
GetCMApps();
foreach (var cmapp in listcmapps)
{
string GetFullName = cmapp.cmappFullName;
string GetSoftwareVersion = cmapp.cmappSoftwareVersion;
string GetRevision = cmapp.cmappRevision;
string GetEvalState = cmapp.cmappEvaluationState;
string GetDeploymentReport = cmapp.cmappDeploymentReport;
string GetConfigureState = cmapp.cmappConfigureState;
string GetLastEvalTime = cmapp.cmappLastEvalTime;
string GetLastInstallTime = cmapp.cmappLastInstallTime;
string GetErrorCode = cmapp.cmappErrorCode;
string GetInstallState = cmapp.cmappinstallstate;
string GetResolvedState = cmapp.cmappResolvedState;
//add items to ListView
arr[0] = GetFullName;
arr[1] = GetSoftwareVersion;
arr[2] = GetRevision;
arr[3] = GetEvalState;
arr[4] = GetConfigureState;
arr[5] = GetLastEvalTime;
arr[6] = GetLastInstallTime;
arr[7] = GetErrorCode;
arr[8] = GetInstallState;
arr[9] = GetResolvedState;
itm = new ListViewItem(arr);
listView1.Items.Add(itm);
}
}
catch
{ }
any help will be much appreciated :)
best regards!