It's nothing special..... but it will do.
Hello Again folks! I'm really excited for this post as it is my first time sharing a script via a blog. To preface, I'm clueless about the preferred method of sharing a script via a blog post nor do I really even how to format it for that matter. Additionally, being that this wonderful blog host doesn't allow much with formatting, I decided to use screenshots of the actual code and included a link for the script on pastebin at the bottom. I know, that's what I get for being cheap and choosing the free version. As always, I'm open to any suggestions or feedback. That in mind, lets get into this!
As I prefaced in my last post, this is the first script that I published to our module (Yes, the one that I broke). Actually, funny story, I've broke it close to half of a dozen times now doing the same type of thing but I'm happy to say that I've finally committed the process to memory. Anyways, I've named this beast Invoke-MachineDump.ps1 because I couldn't really think of anything better. That in mind, I'm confident that this will likely not be the next big breakthrough in Powershell scripting but it is pretty straight forward and can even help save time for those who troubleshoot via logs/remotely.
To provided an overview of the script, first it checks for existance of and/or creates a directory to be used as a destination directory. Next, the script copies all of the installation log files from the machines C:\Logs directory. After this is completed, the function generates two additional log files, one for process running on the machine and one for running services. In the future, I plan on adding more functionality but for now, it's a great start.
Note: The directory I'll be referencing for logs will be C:\Logs, which is where I like to store software install logs on end-users' machines and on the server side, a directory named \\servername\LogRepo. So.... yeah, let's get into this for real this time!
I'm going to be brief with the first section as it's pretty self-explanatory. I've declared the function which I've configured with a mandatory parameter that needs to be passed as well as the params for usage within the pipeline.
The next section is where all of the object/variable declaration occurs along with the creation of a specifically named destination directory for the returned files. I've also initialize a return object with a few properties but this is for future usage.... so I guess.... ignore it ;). As mentioned last week, this will make it easier to prep for a module and allows the function to be called directly after importing the module.
Finally, the last two sections are comprised of all the "working commands". The first section begins by running a GCI command which is filtered by files that end in .log and storing them into a $installLogs object. Next we use a foreach loop to iterate through the files returned, pull and store the Name attribute from each file object and then copy it to the destination directory. Last, but not least, I've added a few commands to get process and running services from the remote machine and pipe it to a log file headed for the log repo.
And there you have it! I wasn't kidding when I said that this was a pretty straight forward script. It worked out well though because I'm not only learning various skills for my job but also how to incorporate it into a blog post. That in mind, I was very pleased with the outcome of my first sprint. (Even though I managed to brick the module I was deploying to). That's OK though, shit happens. I'm confident that there will be numerous other learning experiences just like this.
The reality is that my learning experience is going to take plenty of time and effort. In addition to learning General Powershell and automation concepts, I'm also going to be spinning up my first LAMP stack, and, what I consider my biggest mountain, SQL concepts. Don't get me wrong, I know what stuff like databases, tables, queries, etc. are however the extent of my experience with them was a VB application with a locally stored database, consisting of one table with five columns. I encountered my first prod DB this week and I think it's safe to say that I could be labelled as a SQL-Noob. Anyways, that's for another week.
With all of that in mind, I hope you enjoyed my post and thank you for reading. As promised, the pastebin link for my script is below. If you have any questions or feedback, feel free to reach out.
Pastebin Link: http://pastebin.com/diDME8Rb
Hi Derrick,
ReplyDeleteWould you consider to host your scripts @ GitHub?
Hello kf. Sorry for the delay, I was just starting on my next post and saw your message. I was already planning on moving over to Github during a future post where I'll be discussing Git. However, as luck would have it, I actually need squeeze it in with my next post so that I can move on to my next topic.
DeleteI'll have the repo info along with scripts that I've written thus far on my next post. Please let me know if you have any other questions.