I'm completly new to SCCM and I am confused by the object model. I have a folder like this:
And I want to get a list of the object inside it:
APP0001V01 (etc)
APP0001V02 (etc)
etc
The code I have so far is:
public void GetCollectionTest2()
{
var con = this.scomService.Connect(Server, UserName, Password);
string query = "select * from SMS_ObjectContainerItem where ContainerNodeID = '16777219'";
IResultObject packages = con.QueryProcessor.ExecuteQuery(query);
foreach (IResultObject ws in packages)
{
foreach (IResultObject item in ws.Properties)
{
Debug.Print(item.ToString());
}
string query2 = "SELECT * FROM SMS_FullCollectionMembership WHERE InstanceKey = '" + ws["InstanceKey"] + "'";
IResultObject packages2 = con.QueryProcessor.ExecuteQuery(query2);
foreach (IResultObject ws2 in packages2)
{
Debug.Print(ws2["Name"].StringValue);
}
}
}
I think that
"select * from SMS_ObjectContainerItem where ContainerNodeID = '16777219'";
Is returning the folder I want but when I try to get the contents I just keep drawing a blank.
What should I be doing?
Update
Thanks to the reply from Xin Guo I now have:
public void GetCollectionTest2()
{
var con = this.scomService.Connect(Server, UserName, Password);
string query = "select * from SMS_ObjectContainerItem where ContainerNodeID = '16777219'";
IResultObject packages = con.QueryProcessor.ExecuteQuery(query);
foreach (IResultObject ws in packages)
{
foreach (IResultObject item in ws.Properties)
{
Debug.Print(item.ToString());
}
}
}
This seems to give me all the item but how do I get their names?
At the moment I can only return these:
instance of SMS_ObjectContainerItem
{
ContainerNodeID = 16777219;
InstanceKey = "0010047C";
MemberGuid = "C8A66344-B7E8-451B-A4EF-9BFB3B3E228C";
MemberID = 16778256;
ObjectType = 5000;
ObjectTypeName = "SMS_Collection_Device";
SourceSite = "";
};
I assume I need to link to use an ID to look the name up somewhere else but I can't find any documentation on this?
I recommend you run select * from SMS_ObjectContainerItem to see the result.
Here is the result my test lab.
WMI Explorer is a very useful tool.
Ahh finally worked it out thanks to a good rummage through WMI Explorer. In the diagram above the folders on the left are Containers & those on the right are collections.
So the code I wanted was:
public void GetCollectionTest2()
{
var con = this.scomService.Connect(Server, UserName, Password);
string query = "select * from SMS_ObjectContainerItem where ContainerNodeID = '16777219'";
IResultObject packages = con.QueryProcessor.ExecuteQuery(query);
foreach (IResultObject ws in packages)
{
Debug.Print(ws["InstanceKey"].StringValue);
string query2 = "SELECT * FROM SMS_Collection WHERE CollectionID='"+ ws["InstanceKey"].StringValue +"'";
//// Run query.
IResultObject colResultObject = con.QueryProcessor.ExecuteQuery(query2);
foreach (IResultObject ws2 in colResultObject)
{
Debug.Print(ws2["Name"].StringValue);
}
}
}
Thanks for your help in getting to this Xin
Related
I'm trying to populate list from mysql with the below code. My model looks like this https://pastebin.com/iQEVV42C
My goal is to populate the lists then take the sub lists and put them into the root (tickets) how can I do this?
public static async Task<IEnumerable<Models.Zelkon.Tickets.Root>> Tickets(int ticketId = 0, int clientId = 0)
{
DataSet ds = new DataSet();
List<Models.Zelkon.Tickets.Root> tickets = new List<Models.Zelkon.Tickets.Root>();
List<Models.Zelkon.Tickets.Incidents> ticketIncidents = new List<Models.Zelkon.Tickets.Incidents>();
List<Models.Zelkon.Tickets.Files> ticketFiles = new List<Models.Zelkon.Tickets.Files>();
List<Models.Zelkon.Tickets.Comments> ticketComments = new List<Models.Zelkon.Tickets.Comments>();
using (MySqlConnection con = new MySqlConnection(Variables.conString))
{
await con.OpenAsync();
string query = "SELECT * FROM tickets;" +
"SELECT * FROM ticket_incidents;" +
"SELECT * FROM ticket_files;" +
"SELECT * FROM ticket_comments;";
using (MySqlCommand cmd = new MySqlCommand(query, con))
{
using (MySqlDataReader reader = (MySqlDataReader)await cmd.ExecuteReaderAsync())
{
// tickets
while (await reader.ReadAsync())
{
tickets.Add(new Models.Zelkon.Tickets.Root()
{
header_text = reader.GetString("name"),
description = reader.GetString("description"),
status = reader.GetString("status"),
source = reader.GetString("source"),
owner = reader.GetString("owner"),
temp = reader.GetString("temp"),
priority = reader.GetString("priority"),
category = reader.GetString("category"),
creation_team = reader.GetString("creation_team"),
last_updated = reader.GetDateTime("last_updated"),
creation_date = reader.GetDateTime("creation_date"),
client_id = reader.GetInt32("client_id"),
ticket_id = reader.GetInt32("ticket_id"),
unique_id = reader.GetInt32("unique_id")
});
}
await reader.NextResultAsync();
// ticket_incidents
while (await reader.ReadAsync())
{
ticketIncidents.Add(new Models.Zelkon.Tickets.Incidents()
{
header_text = reader.GetString("header_text"),
description = reader.GetString("description"),
type = reader.GetString("type"),
creation_team = reader.GetString("creation_team"),
created_by = reader.GetString("created_by"),
notify_team = reader.GetInt32("notify_team"),
notification_seen = reader.GetInt32("notification_seen"),
is_pinned = reader.GetInt32("is_pinned"),
creation_date = reader.GetDateTime("creation_date"),
incident_id = reader.GetInt32("incident_id"),
ticket_id = reader.GetInt32("ticket_id"),
unique_id = reader.GetInt32("unique_id")
});
}
await reader.NextResultAsync();
// ticket_files
while (await reader.ReadAsync())
{
// Not filled yet
}
await reader.NextResultAsync();
// ticket_comments
while (await reader.ReadAsync())
{
// Not filled yet
}
await reader.NextResultAsync();
}
}
}
System.Diagnostics.Debug.WriteLine(tickets.First().owner);
return tickets;
}
My issue is how I can fill the sub classes (ticket_incidents, ticket_files, ticket_comments) and then put it into the root class?
Firstly, you might save a lot of time by using an ORM to query your database; it will turn a set of related tables into an object graph.
Secondly, if you're going to avoid a full ORM and read the data directly via ADO.NET, I would recommend Dapper, which will remove a lot of boilerplate code.
Here's how the main body of your method would look with Dapper:
string query = "SELECT * FROM tickets;" +
"SELECT * FROM ticket_incidents;" +
"SELECT * FROM ticket_files;" +
"SELECT * FROM ticket_comments;";
using var gridReader = await con.QueryMultipleAsync(query);
// NOTE: Assumes database columns are named exactly the same as the object properties
var tickets = (await gridReader.ReadAsync<Models.Zelkon.Tickets.Root>()).ToList();
var ticketIncidents = (await gridReader.ReadAsync<Models.Zelkon.Tickets.Incidents>()).ToList();
var ticketFiles = (await gridReader.ReadAsync<Models.Zelkon.Tickets.Files>()).ToList();
var ticketComments = (await gridReader.ReadAsync<Models.Zelkon.Tickets.Comments>()).ToList();
Thirdly, if you're using async with MySQL, uninstall Oracle's MySQL Connector/NET (i.e., MySql.Data) and use MySqlConnector instead. It's a longstanding bug in Connector/NET that async operations actually operate completely synchronously.
Finally, to link your objects together, iterate over each collection and attach the object to the right Root object. One way to do this would be to put your Root objects in a Dictionary keyed on their ID:
var ticketsById = tickets.ToDictionary(x => x.ticket_id, x => x);
foreach (var incident in ticketIncidents)
ticketsById[incident.ticket_id].incidents.Add(incident);
foreach (var file in ticketFiles)
ticketsById[file.ticket_id].files.Add(file);
foreach (var comment in ticketComments)
ticketsById[comment.ticket_id].comments.Add(comment);
This assumes that the Root object has an incidents property (files, comments) that's initialized to an empty List<Incidents> (List<Files>, List<Comments>). You will need to write that code if it doesn't exist.
Again, any decent ORM (Entity Framework, NHibernate, etc.) will provide all of this code for you "out of the box", so you wouldn't even have to write this method at all.
I am new to React and new to Web API. I am uploading data in a tabulator in react front end from the value that I am passing through the web API. I am passing value through the getReports function like this:
[HttpPost]
[Route("GetReports")]
public IHttpActionResult GetReports(string jwt, List<object> data)
{
if (!Common.VerificationToken.VerifyJWToken(jwt))
{
return null;
}
var to = data[0];
var from = data[1];
DateTime toDate = Convert.ToDateTime(to);
DateTime fromDate = Convert.ToDateTime(from);
var ReportData = db.T_CQL_COIL_DESC.Where(t => t.CCD_CREATED_ON >= toDate &&
t.CCD_CREATED_ON <= fromDate).ToList();
ReportsDTO dto = new ReportsDTO();
List<ReportsDTO> ReportDTO = new List<ReportsDTO>();
try
{
foreach (var report in ReportData)
{
List<vehicledetail> vehicle = new List<vehicledetail>();
var imgurl = "https://firebasestorage.googleapis.com/v0/b/tsl-coil-qlty-
monitoring-dev.appspot.com/";
dto = new ReportsDTO();
dto.Type = report.CCD_OP_TYPE;
dto.ID = report.CCD_COIL_ID;
vehicle = GetVehicleID(dto.ID);
vehicledetail vehicledetails = vehicle[0];
dto.vehicleno = vehicledetails.vehicleno.ToString();
dto.wagonno = vehicledetails.wagonno.ToString();
dto.Active = report.CCD_ACTIVE;
dto.ImgURL = report.CCD_IMAGE_URL != null ? imgurl + report.CCD_IMAGE_URL : "NA";
dto.Desc = report.CCD_VIEW_DESC != null ? report.CCD_VIEW_DESC : "NA";
ReportDTO.Add(dto);
}
return Ok(ReportDTO);
}
catch (Exception ex)
{
return Content(HttpStatusCode.NoContent, "Something went wrong");
}
}
The data in vehicledetail in vehicledetail vehicledetails = vehicle[0]; is getting populated from this function:
public List<vehicledetail> GetVehicleID(string coilID)
{
List<vehicledetail> vehicle = new List<vehicledetail>();
vehicledetail vehicledetails = new vehicledetail();
string oradb = Utilities.STAR_DB;
OracleConnection conn = new OracleConnection(oradb);
string query = "SELECT a.Vbeln, b.WAGON_NO FROM sapr3.lips a, sapr3.ZVTRRDA b WHERE
a.MANDT='600' AND a.CHARG='" + coilID + "' AND a.LFIMG > 0 AND a.MANDT = b.MANDT AND
a.VBELN = b.VBELN";
OracleDataAdapter da = new OracleDataAdapter(query, conn);
conn.Open();
DataTable dt = new DataTable();
da.Fill(dt);
foreach (DataRow row in dt.Rows)
{
vehicledetails.vehicleno = row["VBELN"].ToString();
vehicledetails.wagonno = row["WAGON_NO"].ToString();
}
conn.Close();
vehicle.Add(vehicledetails);
return (vehicle);
}
It is working fine but it is taking 30 seconds to load the below data:
How do I optimize this . Please help. Note: that it is taking 30 seconds to upload this data
Few other things aside, the major problem seems to be that you are querying database for each vehicle.
In this particular scenario it might very well be better to select all vehicle ids and query them all.
var vehicleIds = reportData.SelectMany(t => t.ID);
You can then form a query that will get all vehicle details together. This will reduce the number of database calls and it can have massive impact on time.
Another thing to check is if there's any index created on the vehicle id column in database as that may also help speed things up.
I've got a console app which updates a column in the database called InstagramId based off user input. I want to be able to query the database and if there's a instagramId which is null, to update it with the correct instagramID based off their instagramUsername.
I wasn't sure on the approach if I should create a fake table based off all the null instagramIds and then use those to update my column or if theres another cleaner approach. This console app is only a one time use so just a quick fix up.
class Program
{
static async Task Main(string[] args)
{
// receive a profile name
var tasks = new List<Task<InstagramUser>>();
foreach (var arg in args)
{
if (string.IsNullOrEmpty(arg)) continue;
var profileName = arg;
var url = $"https://instagram.com/{profileName}";
tasks.Add(ScrapeInstagram(url));
}
try
{
var instagramUsers = await Task.WhenAll<InstagramUser>(tasks);
foreach (var iu in instagramUsers)
{
iu.Display();
}
}
catch (Exception ex)
{
Console.WriteLine(ex.ToString());
}
}
public static async Task<InstagramUser> ScrapeInstagram(string url)
{
using (var client = new HttpClient())
{
var response = await client.GetAsync(url);
if (response.IsSuccessStatusCode)
{
// create html document
var htmlBody = await response.Content.ReadAsStringAsync();
var htmlDocument = new HtmlDocument();
htmlDocument.LoadHtml(htmlBody);
// select script tags
var scripts = htmlDocument.DocumentNode.SelectNodes("/html/body/script");
// preprocess result
var uselessString = "window._sharedData = ";
var scriptInnerText = scripts[0].InnerText
.Substring(uselessString.Length)
.Replace(";", "");
// serialize objects and fetch the user data
dynamic jsonStuff = JObject.Parse(scriptInnerText);
dynamic userProfile = jsonStuff["entry_data"]["ProfilePage"][0]["graphql"]["user"];
//Update database query
string connectionString = #"Server=mockup-dev-db";
using (SqlConnection con = new SqlConnection(connectionString))
{
SqlCommand cmd = new SqlCommand("Update ApplicationUser Set InstagramId = '" + userProfile.id + "'" + "where Instagram = '" + userProfile.username + "'", con);
cmd.Connection.Open();
cmd.ExecuteNonQuery();
}
// create an InstagramUser
var instagramUser = new InstagramUser
{
FullName = userProfile.full_name,
FollowerCount = userProfile.edge_followed_by.count,
FollowingCount = userProfile.edge_follow.count,
Id = userProfile.id,
url = url
};
return instagramUser;
}
else
{
throw new Exception($"Something wrong happened {response.StatusCode} - {response.ReasonPhrase} - {response.RequestMessage}");
}
}
}
}
}
It seems like there are two problems here
Retrieving a list of username from the DB which have null instagram IDs
For each of these, downloading and populating the instagram ID
It looks like you've already solved 1, so let's look at number 2:
This data can be fetched using an SQL query:
SELECT Instagram FROM ApplicationUser WHERE InstagramId IS NULL
In C# this could look like:
using (SqlConnection con = new SqlConnection(connectionString))
{
SqlCommand cmd = new SqlCommand("SELECT Instagram FROM ApplicationUser WHERE InstagramId IS NULL", con);
cmd.Connection.Open();
var instagramUsernames = new List<string>();
SqlDataReader reader = command.ExecuteReader();
while (reader.Read())
{
instagramUsernames.Add(reader.GetString(0));
}
}
Now you have all the usernames in the instagramUsernames list and can execute your existing code (with minor modifications) in a foreach loop.
Instead of looping through the args you'd now loop through the list of instagramUsernames (so the above code would come at the start of the Main method):
foreach (var arg in args)
Would become foreach (var arg in instagramUsernames)
I am using active directory for getting all the departsment and filtering distinct departments using linq query, below is my code
private static DomainController GetDomainController(string domainpath)
{
var domainContext = new DirectoryContext(DirectoryContextType.Domain, domainpath);
var domain = Domain.GetDomain(domainContext);
var controller = domain.FindDomainController();
return controller;
}
private static MyMethod()
{
var domainController = GetDomainController(ActiveDirectorySettings.DomainPath);
// Lookup the information in AD
var ldapEntry = new DirectoryEntry(string.Format("LDAP://{0}", domainController)) { AuthenticationType = AuthenticationTypes.Secure | AuthenticationTypes.FastBind };
DirectorySearcher ds;
ds = new DirectorySearcher(ldapEntry)
{
SearchScope = SearchScope.Subtree,
Filter = "(&" + "(objectClass=user)" + "(department=" + departmentname + "*))"
};
ds.PropertiesToLoad.Add("department");
if (ds.FindAll().Count >= 1)
{
//DataSet du = DataReader.CheckAdUserExist();
var results = ds.FindAll();
var uniqueSearchResults = results.Cast<SearchResult>().Select(x => GetProperty(x,"department")).Distinct();
addUsersList.AddRange(uniqueSearchResults.Select(departmentName => new UsersAndDepartments
{
UserDepartment = departmentName
}));
}
}
I want to check the linq query result with the database whether department already exist or not, I am not sure how to do that?
If what you want is to create a simple database connection using SqlConnection you just need to query your DB using the department parameter you received from your AD request.
try{
SqlConnection connection = new SqlConnection("YourConnectionstring");
connection.Open();
//Your connection string can be found through your Server DB
//Now you go through your SearchResultCollection populated by SearchResult objects
foreach(SearchResult res in uniqueSearchResult){
SqlCommand cmd = new SqlCommand("Select * from yourTable where department=" +res.Properties["department"][0].ToString() + "", connection);
SqlDataReader reader = cmd.ExecuteReader();
//Here you verify if there are corresponding rows in your table
//with the request you have executed
if(!reader.HasRows()){
//If you have not found corresponding rows, then you add the department to your
//list
addUsersList.Add(res.Properties["department"][0].ToString());
}
}
connection.close();
}catch(Exception e){
Console.WriteLine("Exception caught : \n\n" + e.ToString();
}
This should work.
There are plenty of tutorials for this, but if you are making alot of requests I do not recommend using this connection method you will just lose too much time / organization, maybe try using a persistence Framework like Entity Framework :
https://www.codeproject.com/Articles/4416/Beginners-guide-to-accessing-SQL-Server-through-C
Hope this answers your question!
Here is my solution, I have solved it myself
private static DomainController GetDomainController(string domainpath)
{
var domainContext = new DirectoryContext(DirectoryContextType.Domain, domainpath);
var domain = Domain.GetDomain(domainContext);
var controller = domain.FindDomainController();
return controller;
}
private static MyMethod()
{
var domainController = GetDomainController(ActiveDirectorySettings.DomainPath);
// Lookup the information in AD
var ldapEntry = new DirectoryEntry(string.Format("LDAP://{0}", domainController)) { AuthenticationType = AuthenticationTypes.Secure | AuthenticationTypes.FastBind };
DirectorySearcher ds;
ds = new DirectorySearcher(ldapEntry)
{
SearchScope = SearchScope.Subtree,
Filter = "(&" + "(objectClass=user)" + "(department=" + departmentname + "*))"
};
ds.PropertiesToLoad.Add("department");
if (ds.FindAll().Count >= 1)
{
// getting list of all departments from the database
var departmentsList = AllDepartments();
// getting list of all departments from active directory
var results = ds.FindAll();
// filtering distinct departments from the result
var uniqueSearchResults = results.Cast<SearchResult>().Select(x => GetProperty(x,"department")).Distinct();
// here firstly i am getting the department list from the database and checking it for null, then using linq query i am comparing the result with ad department results
if (departmentsList != null)
{
addUsersList.AddRange(from sResultSet in uniqueSearchResults
where !departmentsList.Exists(u => u.UserDepartment == sResultSet)
select new UsersAndDepartments
{
UserDepartment = sResultSet
});
}
else
{
addUsersList.AddRange(uniqueSearchResults.Select(departmentName => new UsersAndDepartments
{
UserDepartment = departmentName
}));
}
}
}
I'm making an application that needs to list all the LPT ports in a machine with their IO addresses. (ie it's output : LPT1 [starting ; ending] ....)
Using WMI you can get this info.. the name/number from Win32_ParallelPort and the addresses from Win32_PortResource.
The problem is that i don't know how to associate the portname with it's addresses.
You have to query three times and loop over the results to get the matching records from ParallelPort, PnPAllocatedResource and PortResource. The following code does exactly that:
var parallelPort = new ManagementObjectSearcher("Select * From Win32_ParallelPort");
//Dump(parallelPort.Get());
foreach (var rec in parallelPort.Get())
{
var wql = "Select * From Win32_PnPAllocatedResource";
var pnp = new ManagementObjectSearcher(wql);
var searchTerm = rec.Properties["PNPDeviceId"].Value.ToString();
// compensate for escaping
searchTerm = searchTerm.Replace(#"\", #"\\");
foreach (var pnpRec in pnp.Get())
{
var objRef = pnpRec.Properties["dependent"].Value.ToString();
var antref = pnpRec.Properties["antecedent"].Value.ToString();
if (objRef.Contains(searchTerm))
{
var wqlPort = "Select * From Win32_PortResource";
var port = new ManagementObjectSearcher(wqlPort);
foreach (var portRec in port.Get())
{
if (portRec.ToString() == antref)
{
Console.WriteLine( "{0} [{1};{2}]",
rec.Properties["Name"].Value,
portRec.Properties["StartingAddress"].Value,
portRec.Properties["EndingAddress"].Value );
}
}
}
}
}