logo

NJP

Running Powershell scripts from a ServiceNow MID Server using Flow Designer

Import · Nov 22, 2023 · video

this video is going to look at running Powershell scripts from a service now mid server using flow designer it's a kind of primitive substitute for the uh ad spoke integration Hub ad spoke uh which is the easiest way to do this um but if you don't have the licensing for it uh this will help you to uh to do some of those tasks uh using just base platform functionality so I have an article on this uh which I will will put in the description and this video will basically just follow this article um so the first thing we want to do is create a Powershell script um and you're probably going to want to do this in Powershell um so you can fire up I have a Powershell here and it's very small uh so you get that up a little bit there we go so this one is going to be a very simple script it's just going to do a get local user and uh I'm going to have a variable here that will where we can insert the username and then I uh convert this to Json for the output so if I run this here we can see we get the um output uh like this for this local user so if you're doing this uh for a real world application you're obviously not going to do something simple like this uh you'd probably be using the active directory set of tools so like get 8 user uh I think it's create ad user Etc um and that is a a module that needs to be installed in Powershell in order to use that um so you're going to probably use Powershell to to um get your script uh working um I do use this convert to Json uh with the pipe and then the convert to Json at the end because this is a lot easier to parse when when it's back in service now if we didn't do that what we're going to get in service now is this uh and this uh in addition to not having all the information uh this is a heck of a lot more difficult to parse because now we're just it's just a string right and we have a bunch of stuff so uh using the convert to Json is much easier so we get our script together and then we go into our instance and and we're going to go to mids server and we're going to go to script files and then I've already created it here but uh essentially we're going to paste this into here paste the script into here uh and then put it in one of the folders here I put it into the Powershell uh folder and then we give it a name like get user. PS1 um you'll notice and I have this Linked In the article there's a support article from service now which talks about using the args um variable and this allows you to uh provide input to the script um so that you can you know have have Dynamic uh input uh parameters so you see our get local user here is uses the username variable which gets it from the first uh argument there okay so once that's done uh The Next Step will to create a custom well okay let's uh update that and then you'll see and I have the mid server I have the mid server running on my machine here so once you save that file and you wait a few seconds uh if you go to your mid server installation on your on whatever server you have it running on uh so it's in service now agent on this one and then we go into scripts and remember I put it in the Powershell directory and get user here it is and if I were to open it with that you can see uh here's my same thing that we saw in the service now instance so our next step is to create a custom action in flow designer to parse the eccq result um what I do here so in flow designer you go uh here and then you go action and I've already created this so here's the action parts eccq result and the input is simply a payload as a string and then there's a script step uh and this script step and we'll look at this um when we look at the actual when we actually run it it'll make a little more sense but what the script is doing essentially is it is taking the payload which is an XML and it is uh converting it to Json and then pulling out the results. result. STD out uh and when we look at the what comes back in the uh from the mid server after running the script uh this will make a little more sense but it's basically just extracting uh that the the actual output and um then it takes that output and makes it the output of the script step into Powershell output and and then we have an action output for the entire action called Powershell output and we just uh drag this over here is this Powershell output we drag that into here so basically we are giving uh the input is the XML that comes back from the mids server and the output is whatever is the actual output from that script uh this will make a little more sense after we run it and you can see that okay so that's our c custom action now we create a subflow to put everything together so I have my subflow here and I have just one input which is the username that I want to get information for and then the output is going to be a string and it's going to be all of that user information that we looked at when we ran the Powershell script in uh in Powershell um okay so now our actions the first action is to create a record in the eccq table so this is the table that is used to communicate back and forth with the mid server or mid servers if you have multiples um and so what this record is doing is it's just a queuing up a record for the mid server so our agent is mid. server. mid test so that's the name of my mid server if we go uh back to the platform here and we look at Mid servers right I have a mid server called mid test so that's the agent the topic is going to be command the payload and this is scripted so all of this is in the article uh the actual script in there uh so we can see the it's returning this is an XML and and parameters and then a parameter and parameter name and then the value uh so that's the first parameter and then the second parameter is a just a skip sensor value is true um I believe what this is doing is uh because if it's for Discovery then uh there are sensors uh that get run but since this is not for Discovery this is just a a regular script uh we can do skip sensor is true um okay so the parameter name with a name of name uh and then the value so the value here is if we look up here on line nine so we want to run from the command line we're running Powershell and then we're saying go to the scripts directory go to the Powershell directory and then do the get user. PS1 and then after that give it a parameter of the username which is the uh input from the subflow okay so that's the payload uh now the Q is going to be um output uh because this is relevant uh relative to the instance right so this is going to be output um and then when we get a result back it's going to be input uh but for for now when we're sending it out it's uh its output and then the state is going to be ready right you see the other states here are processing which is what happens when it's starts to process and then when it finishes it goes to processed okay so this sets up the uh Q the eccq record um then and you know what maybe we should run this uh before going into each one of these things because it's a lot easier to understand once we have run it so let's do a test and we'll put in a username I know this exists Michael local and I'll do a run test okay so it did uh complete the record so before we even look at this let's go back to the instance let's look at that eccq table that we were just uh looking at the action that creates the record here and here here is the record that was created uh so again we saw the agent the topic uh it's output it's at a state of ready and we can see the actual command here so it's Powershell and it's going to this folder to the get user PS1 and that's what I had the input Michael local so that's what we're asking it to run so now let's go take a look at the results if we go back to our eccq table um and I'm going to sort uh right so here we have this is the response record and you can tell because there's a response to column command so this goes to the original record uh so yeah this is the original the output and this is the input that came back so let's have a look at that um so this is what we get back and again this is also an XML payload um and you can see that most of it is in this uh standard out which we extract uh and we looked at that uh before the uh custom action is what actually extracts this piece out and because we're returning Json we get all of this uh Json in the standard out um other commands have are a lot less verbose in terms of their output uh if you're like adding a user to a group I don't think there's any output actually unless there's an error uh so the standard out will actually just be blank uh but in this case we get this uh this large Json thing and then here's the rest of the XML that comes back so uh and we'll see this when we look at the the the operation in flow designer um basically that custom um action for parsing the output just extracts what's in standard out uh and leaves everything else uh leaves everything else there okay so there's our response so now let's flip back to our flow designer operation and and let's look at it in the operation view rather than in here it'll things will make a little more sense so the second step is to set the subflow variable um so let me talk about that I have this variable called TR count and I start it at zero and I give it five tries so when we when we look at this uh record here when we look at this this list of Records in the eccq table when I let me see if I can find it again let me look at I just want to see commands [Music] and right here was the uh original uh output right at 85303 and it took 30 seconds a little bit uh 32 seconds it looks like for it to come back at 85335 um so the way this the way this works is right well we saw this that the when you're doing an eccq request basically you create the record and the mid server goes and grabs it and then uh does the processing and then sends back the result um so that takes time it's it's not uh instantaneous so in the meantime we have to keep looking at at this table for a result um so there's two ways you can do it if you're doing it in flow designer uh which is the way we I do it here uh we have to keep checking this table to see if there's a result the other way you could do it is with business rules where you have business rules on this table and you say hey when I get a result where the name is uh or yeah where the name is uh Powershell you know contains get user something like that then do something uh with it uh that's actually maybe a little more efficient because you don't have to keep polling the table to see if the result has come in U but on the other hand you have to create a whole bunch of business rules and it can start to get a little sticky um I prefer the flow designer way of doing it so what we do in flow designer is we um have this do the following Loop and so it goes and keeps looking up the record right it keeps maybe we look that look at that in here it's a little bit easier to see so it's going to wait for 5 seconds in step five and then it's going to go check and say hey is there a record there where the response to is the original record that I created right it's going to go and check for it and um then it's going to set the payload to uh the value of of the of the payload that's in this lookup record right that would have been if we go in here the payload is this value this huge XML value um so it's going to keep looping around and doing that until one of two conditions is met either the payload is not blank right so we actually got a record and it had some XML in it uh then we'll stop doing the lookups or condition two the TR count variable is greater than or equal to five right so I give it five tries uh in order to find it if it doesn't find anything after five tries then it gives up and uh and stops looking right so the purpose of this is well if you never get a response back um or there's some other circumstance that your flow is not hanging here forever uh looking for a response or worse if it's you know keeps hammering the uh the eccq table every 5 Seconds looking for a response um so I have a 5-second uh delay in between each um each attempt um this probably could be increased I mean this is a development instance so and the EC the mid server is running on my laptop so actually I was kind of surprised at how slow it was but in production on a production instance with a decent mids server uh it usually comes back in the first attempt um but it also depends on what you're doing in the Powershell script if the Powershell script is complex and there's it's resource intensive uh then you'll want to up this a little bit or uh you can just up the uh number of attempts right instead of five make it 10 or something like that um Okay so so um just getting back to this set flow variable then is the the try count right so I originally set the flow variable to zero up here and then once we get into the loop I just increment it by one each time right so this is a script here and it's just saying uh take whatever the flow the tri count is and increment it by one uh okay so then once it gets a payload um then it's going to use what we created in the earlier step which is this uh parse eccq result action and so let's go back here so set flow variable oh whoops this didn't finish yet okay and so one of five right so it used all of the attempts it looks like and I think yeah it only got it on the last one five seconds I I thought that would be more than enough five attempts at five seconds each but uh we saw it took a 32 seconds so um and actually it probably just got under the wire there because 5 Time 5 is 25 maybe there was another there was some extra time in there but uh anyway so here's the payload right so this is what comes out of the eccq record the whole payload and then what our parse eccq result um action does is it just extracts this uh Json right the only the actual result um and then what I do is I just assign that actual result the user information to the subflow output um so normally uh you would call this subflow from a flow or another subflow and then you would do something with this output you would either write it somewhere or use it for further processing of something else um that would be a kind of a subject for another video uh a common pattern I've used is actually calling the subflows from a UI action so so for example if you have a um a request item for a create user account and the person fills in all the information on the variables then you can have a UI action in your on the Rhythm table and the Rhythm that UI action can call a subflow which goes and creates an account for example um and then uh returns that information returns the new account information via Json and then you write that Json to the work notes or to some field uh in the Rhythm form um so that's that's the basic idea of how you would use a flow designer to run Powershell actions and then get their results

View original source

https://www.youtube.com/watch?v=6yW-3eXMT7o