powershell to retrieve asset information
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-15-2020 01:01 PM
I would like to feed a PC serial number to a powershell script and have it retrieve asset details such as assigned user, location and status.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-17-2020 05:39 AM
Hi Lane,
The easiest way to do that is via a REST GET call from your local machine to ServiceNow. Here's the link to the REST documentation.
You could also set up an ODBC database connection between your local DBMS and ServiceNow instance and run a query that way.
Having worked on both I strongly recommend the REST approach. If you're not familiar with using REST in ServiceNow, here's the link to a video that Chuck Tomasi did as part of his learning javascript series that provides a great intro.
Hope that helps.
:{)
Helpful and Correct tags are appreciated and help others to find information faster
:{)
Helpful and Correct tags are appreciated and help others to find information faster
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-17-2020 11:16 AM
My main issue is not knowing the ServiceNow tables and how to pull information from them. Our onsite staff has not been much help. I can create a REST connection but do not know what to GET.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-17-2020 12:44 PM
Okay, that's a lot to try and explain here. I'm assuming that you are more interested in personal type assets, laptops and desktops. There is a table alm_asset to which you can identify the serial number or asset tag. The field names are serial_number and asset_tag. The state field should give you the status. There is also and assigned_to field in alm_asset which is a pointer to sys_user. From there, you can access the user's name. There is a location column in sys_user that is a reference to cmn_locaton.
I'm assuming that you have some level of Javascript programming skills. You will need is to create a scripted REST api which will receive the GET request.
Having, let's assume, sent the serial number you can get the values something like this:
var theAsset = new GlideRecord("alm_asset");
theAsset.addQuery("serial_number", request.serial_number;
theAsset.query();
if (theAsset.next()) {
var theUser = new GlideRecord("sys_user");
theUser.get(theAsset.assigned_to);
theLocation = new Gliderecord("cmn_location");
theLocation.get(theUser.location);
return {
"serial_number" : theAsset.serial_number,
"assigned_to" : theUser.name,
"location" : theLocation.name,
"status" : theAsset.state
};
}
else {
// set up how you want to reply for a not found
}
In the interest of brevity, I haven't included a check that assigned_to is populated. If a device has been returned to a depot and is awaiting redeployment, it could have no value.
I haven't tested this code so I may have a fat finger somewhere in it. You can test this as part of a script where instead of doing the return block, just assign the values to variables that you can track in the debugger. For serial number that would be:
var serialNumber = theAsset.serial_number;
ServiceNow will take care of formatting the JSON response, etc. There are examples for all of this in the documentation.
If you need to do the development somewhat on your own, I'd suggest using your Personal Developer Instance (PDI). Keep an update set running as you do the work so that it will be easy to import it into your dev instance to start the implementation process. Let me know if PDI is something new to you. That is easy to explain.
Hope that helps.
:{)
Helpful and Correct tags are appreciated and help others to find information faster
:{)
Helpful and Correct tags are appreciated and help others to find information faster