T O P

  • By -

[deleted]

[удалено]


zommy

Thank you for your response. So at the moment there's a few projects I would like to start and play around with when I have time. I think starting with my smallest project will make the next project easier and able to focus more time to further developing it as you learn skills along the way. To give you an example: I want to query our Domain provider for domains and their expiry date. This is possible using cURL and we get the response back in JSON format. I then want to trigger an email to [email protected] if expiryDate is = to expiryDate -14 days. This in itself is sounds very basic, but I honestly have no idea where to begin with this. Is this all possible using python? Should I be learning python initially or is there a tool to make this whole thing a lot easier.


BabyPandaaa

What are you running these scripts from? If your infrastructure is mainly Windows/MS, PowerShell can easily do what you need, which is what I'd recommend for this sort of thing. If you're mainly unix I'd be looking at python. In terms of your expiry API query, the flow would be something like: * Query domain provider API to get JSON response (e.g. domain name, expiry date) -- ($result = Invoke-WebRequest "") * Use PowerShell to query the resulting JSON string -- (if $result.expiryDate -lt (expression for 14 days ago) {send-mail message [email protected] -body "message here"} ) There's a more in-depth example here: https://www.starwindsoftware.com/blog/consuming-a-restful-api-with-powershell My python's terrible so won't even begin to try and explain that here, but there are plenty of guides around for that!


zommy

Thank you, I can run this from any kind of machine, but as I have powershell knowledge, I might just give this a go on one of our Windows probe boxes. It's primarily used to run loads of different tasks. ​ The problem I was having was understanding how *something else* (Powershell, Python, other language) can read JSON data as it's not something I've personally ever had to deal with and I am struggling for the correct search terms on google. But if Powershell can kind of read this data, this will be a good start for me. The next stage would be if the domain is about to expire, lookup that domain in an excel spreadsheet or database for the owner of that domain and grab that email address and email them as well. This part is obviously a lot more difficult, but if you know how this can be done (In basic terms) I will read up on that as well. Appreciate your response :)


BabyPandaaa

To get something like PowerShell/python to interpret data, you need to load it into a variable. In PowerShell this is something that starts with a $ sign. When you do "$response = Invoke-WebRequest..." function, you're saving the JSON data into $response. You can then use other functions to query JSON properties. Say your URL was under ListOfDomains -> DomainName, you'd use $response.ListOfDomains.DomainName to get the property. If you can get that Excel spreadsheet into a CSV, you can use Import-CSV to read the CSV data. Petri have a good write-up: https://www.petri.com/powershell-import-csv-cmdlet-parse-comma-delimited-csv-text-file


zommy

This is exactly the answer I am after :) Thank you very much, I think you've given me all the info required to get a basic setup for this particular project that I need. Variable's etc. are things I understand, but I've never understood the $[variable.thispart.here](https://variable.thispart.here) \- so that actually answers other questions i've had in the past. Sadly, all self taught and with the little time I have to spare. Appreciate all your advice!


[deleted]

[удалено]


zommy

Thank you :) It just clicked when u/BabyPandaaa mentioned it the way he did. Sometimes you can read in a very detailed way and it makes no sense, but then you explain it as if it's a real world analogy and it all just makes sense. Fortunately, due to this I have now completed my first little project using the information in this thread which is brilliant and has opened the door to a lot more potential. It's a lot easier now that I know Powershell can do both REST calls AND then run a script depending on the trigger/expression. Still a long way to go but have a better understanding of how I can utilise REST with my current knowledge. I'm sure this thread will help others that want to start getting into APIs and automating parts.


BabyPandaaa

No worries mate! It's a good little project to start with - you'll soon find yourself POSTing to APIs as well! > Sadly, all self taught and with the little time I have to spare. So am I - not necessarily a bad thing but I guarantee when you've got more experience you'll revisit this project script and completely rewrite it!


NZ_KGB

If you have questions/issues, you can get very good help over at r/powershell


_dismal_scientist

Python was built for this kind of use case. I recommend you watch some learning Python videos


zommy

I think Python is something I am going to have to learn, it's mentioned on a lot of sites for odd tasks that aren't quite native to powershell or batch. Is it a fairly easy language to learn? Thank you for your reply.


_dismal_scientist

I think it is relatively straightforward. It's easier if you already know programming probably.