Category Archives: PowerShell - Page 2

Check the date of a certificate from a polled URL with PowerShell

I recently setup LetsEncrypt on this blog. With all the insecure connection url bar indicators quickly becoming default on the modern browsers, this great open source project really comes through and makes securing your site with a SSL/TLS certificate easy. And of course, in the spirit of open source, the certificates are free!

As part of using LetsEncrypt, you need to automate your certificate renewals. So once I got everything setup and a cron job configured to handle the renewals, I wanted to log the date of my current certificate from a web call on my home computer.

Here is the PowerShell I used to get the certificate date information which then can be logged

 

Delete ElasticSearch indexes with powershell

So I followed this AWS blog and this documentation to launch a tiny t2 elasticsearch cluster to visualize VPC flow logs. Those links have instructions that guide you along setting up flow logs to flow into ES in a few different ways. I ended up following the documentation link and then downloading some kibana3 dashboards until I found one I liked.

Over time however, the little t2 ES cluster could not keep up, and I ran out of storage space and CPU credits. So I wanted to automate the deletion of indices / indexes so that the cluster would free up storage space and not churn through CPU. With more RAM available the cluster uses less CPU, so I had to limit how much data the single node ES cluster is storing. There is plenty of documentation online on how to use curl to delete elasticsearch indexes but I’m on windows most of the time so I decided to write a quick a powershell script to do it.

To use this script just update the esdomain variable to point to your ES cluster name. Also this filter will only work if the lambda script is creating cwl- indexes. Tweak it if your indexes are different. Run it and it will keep the last 2 weeks of indexes and delete anything older.

 

 

Cleanup IIS Logs AutoMagically

First head over to the Microsoft Script Gallery and grab deleteold.ps1 by Jaap Brasser.

I tend to copy scripts to c:\scripts on systems so I copied deleteold.ps1 there and created two new batch files there as well.

The first was used to launch the powershell script with switches that will delete all files in the IIS Logs directory older than 90 days and append to a log of all actions taken.

IISLogsCleanUp.bat :

The second batch file registers a task that runs this script weekly. Right click this batch and run as administrator and IIS logs will no longer be a thorn in your side.

IISLogCleanUpTaskReg.bat :

 

 

As usual, customize as needed! Next on my list is for this repetitive task is creating a powershell remoting script to copy these files to remote servers and execute the task register command.

VMware Tools Check – PowerCLI Email Report

The last two posts I made utilize PowerCLI and VMware tools to gather information for reporting. So what happens if VMware tools is not running…. things fall through the cracks!

Luckily, we can report on VMware tools status too.

 

Virtual Machine Guest Disk Free Space – PowerCLI Email Report

I expanded upon a useful sharing from over at the VMware community site.

Below is some code you can use to run against vcenter to get an emailed report of all virtual machines with low free space. The VMs need to have vmware tools running for this report. Don’t worry, I’ll post a script to check on that too!

As a personal preference I like to create a service account with logon as batch rights on a system to run this script. I then provision this same service account with read only privileges to vCenter. Once those things are setup the powershell script can be ran from task scheduler (using least privileged, yay).

Another thing I like to do is to capture transcripts and outputs of any scripts I run. This way if I have an SMTP issue or lose the email, I can go back and check files to see what was in previous reports or any errors that the script encountered.

You will want to edit the vcenter server name, the output file path, and email addresses before using this script. The script will email any VMs with drives that have less than 10% free space, of course, you can customize that % as well.

 

Automating AWS with PowerShell

I often have to turn on instances or register them with load balancers. I had some trouble finding documentation on the AWS PowerShell modules but in the end I was able to get everything done via scripts.

Here are some examples that might help you out.

If you are not using IAM roles then you will need to pass credentials to the AWS powershell module to use. This is how you accomplish that (using least privileged IAM credentials I hope).

Query for EC2 instances by name

Power on an instance

Convert your already queried instance objects into objects that can be used with Elastic Load Balancers…

Use your ELB Instance objects to add or remove them from ELBs

 

Enable Ping reply with PowerShell

Windows ships with ICMP reply disabled in the firewall. Here is a quick 2 liner in powershell to enable ping replys.

 

Display and Save Wireless Mac Address with PowerShell

A while back I was working at a place where the desktop techs needed to send the wireless MAC address to the network security team. So during the build I wrote this powershell script to save the MAC address to a text document on the desktop and to open it up.

This was done in conjunction with MDT while it wrapped up the task sequence logged in as the built-in administrator .