How To Use Ipswitch MOVEit Transfer 2018’S REST API To Transfer Files With PowerShell
Think you can do better? Show us!
One of the new features of Ipswitch’s MOVEit Automation 2018 product is the introduction of REST APIs. In this snip, we’ll cover how we to use PowerShell to first authenticate to a MOVEit Transfer server and then we’ll cover how to create and monitor a file transfer job using a PowerShell script.
Hello in this snip we're going to talk about using ipswitch’s moveIt automation 2018 product and its new rest APIs that it's got. And we're going to use those rest. APIs you can do a lot of different things. But in the snip. We're going to use those to show you how you can initiate programmatically in a powerShell script to transfer files from one source to a destination so before we do that. There's a few prereqs that we're going to need to talk about obviously you need moveIt transfer 2018 and you need permission to do that, and moveit transfer. Obviously, I'm using the admin account. There are a few things that I've already done. I've already added files manually in the source folder into transfer and you just make sure that that's also accessible. To explain this the scenario is we have a file called wallpaper dot PNG and it's already uploaded into the script like I showed you so here in moveit transfer in our source folder here. We have a file called wallpaper dot PNG. So I've already done that ahead of time. So let me make sure here's the destination folder. We're going to be using and I've already had that there for my testing so I go ahead and just delete that yeah that UI here alright so now. Now that we have that ready to go and let's see how we can transfer that to the destination folder. The references here. This is a really good reference link if you want to get more information about copying files with the API. This gets pretty code heavy but I will show you here at the bottom really short cut on how to get this done quickly. Alright so first off the first thing we need to do, we need to craft the end point URI so in a rest. API everything has an end point and you need to have the URI to point, your your script to your code so the first few lines here. I'm just defining some variables, the localhost. It is going to be locally on the moveIt transfer server itself and I'm specifying the movemIt transfer username and password and then the grant type that just means normally you can authenticate to rest API with a password. Or with a token I’m just going to be using a username and password here and then on line 28. That's where I defined the end point, URL so it is going to be localhost, slash API, slash v1 slash token that actually gets the endpoint. URI to grab the token. We have to grab a token first was then once we have the token. We can use that to authenticate and I will show you how to do that. And then on Line 29 there. We had the off HTTP body, which is essentially the HTTP query. The get where we're going to be sending a get verb to the rest. API and that just tells it the grant type the username password. It provides all that information. OK then online 31, here then here's where we actually are going to send the HTTP post request to moveIt. So first off in 32. That is the powershell specific thing. If you're using something like python or something else. You may not have to do this. But that certificate validation callback method that we're calling there that too. So we don't get prompted for a self signed certificates. So we have a self signed certificate on the moveIt transfer server. Next step is where we actually run invoke rest method. We pass in the URI. We're going to say we do want to send a post method and then we send the body along with that and that actually grabs the token so once we have the token then we can use that for all subsequent calls to it, so that it knows who we are so we don't have to keep passing the username and password all the time. Alright so I will go ahead and run this. And then hopefully, Yep we didn't get an error message, which is good and now we have a big long gobbledigook encrypted token that we can use to authenticate ourselves to the moviIt transfer server all right. Next up? We need to prep the headers here in what I'm doing here is I'm just essentially creating headers value that I'm going to pass later. I'm going to use the authorization key here and pass out the value of the bearer token that's how invoke rest method actually can. Then pass that information to the moviIt transfer server once we have the token in all that is done, then we need to find a file ID moveIt transfer depends on IDs quite a bit so the first stuff we need to find the file ID that wallpaper dot PNG file that I showed you before, so to do that. We need to query that files API, which do that by defining the import URL notice that it's instead of his token it's files now. And then on Line 43, here that's where we actually can query that out, and pull that individual file out and now you can see that we have ID name, path, all kinds of information about it. So now that we have the file object created now we need to find the destination folder ID and to do that with same kind of concept here we use the import URL this time. It's folders instead of files and are doing the same approach. I'm just looking for the folder name of destination folder. And when I do that notice that now that I have a folder object created to have the ID parent ID all that good information there. Alright next step. Now we can actually do the copy so the copy how I'm doing this is creating a hashtable. In 52 called folder and it has the destination folder ID key in it, and using the moveIt transfer folder ID inside of that and then on 55 is where I can bring that to JSON. I'm doing that because online 57. There, you see the body parameter. We have to have JSON and you can just create JSON on the fly if you want, but I found that in powershell, I could just create a hashtable pass it to convert to JSON on 55 here and then it just creates that JSON for me, so that the copy method on 56 here and the URI that we're going to be using it requires that JSON. So I will go ahead and. Run this and everything is pretty much the same but instead of URI we’re passing the same URI there the body like we just talked about and the content type is necessary. We have to specify application. Slash JSON because if we don't. It's going to be undefined mime type and it's just not going to know what kind of format. The calls supposed to be in so now we run this notice is pretty quick. It's it's only a 7 meg file and it's all locally. But what we did was we actually just copied the file it was just that. Easy everything is pretty quick once you get all the code built building out all the code is the hard part, but I've done. All that for you and if your doing this with powershell. I'm going to show you a really easy way to do that. Sum this all up. But just to make sure that this thing is in the destination folder. Now it is so that actually does the transfer let's delete this out? And I'll show you how working we can sum all this up together so you don't have to worry about all that code if you don't want to so I've created a powershell function here called copy move. It transfer file and inside of that it's essentially just combining all that code that we just built together. I have a bunch of parameters a source file the destination folder that to you don't even have to mess with IDs or anything like that. You just specify source file destination folder username password and then. Specify this server name you can set a default value like I'm doing here and then the rest of this is essentially doing exactly the same thing, creating getting that authentication token prepping the headers finding the file ID make sure the destination folder? Is there and getting the folder ID and then doing the file copy for the most part is exactly the same thing, but notice that I've combined it all into one function. I don't have to execute these one line by line. So, in the real world, you're probably going to do this a lot of times so it's. Good idea to create a tool to do this so let me bring this in so now. I have copy move. It transfer file and here I'm going to pass some parameters, PowerShell Splatting. The source file path as just as we saw. That was home techsnip source folder wallpaper and going to transfer it to destination folder and then there's the username and password. I'm going to use so I can just copy. And bring all that in and notice that we did. The exact same thing. I didn't have to go down through each of those line by line. The copy move. It transfer file function does all that stuff for us and let's say to prove that is there, I'll go and run it again and now you can see that a file name with the wallpaper dot PNG already exists in the folder. So it did transfer so that's a great thing about powershell. You can learn it's important to learn how the all that code works. But as soon as you learn all that you can rap that all up in a single function. And between all of the code that we had over here, so notice that we had all the way from 22. All the way down to 57, 37 lines of code. We were able to get that we were able to do all that same functionality with just what is that 126 through 132 with just 6 lines of code so it's really great thing about powershell so that has been how to transfer files with the rest API using Ipswich’s moveIt transfer 2018.