Wednesday, August 13, 2014

Powershell: Script to purge old files from a folder

So I need to backup the SQL DB of a mission critical app.  The app sits on a virtual machine and backups need to get written to a NAS device.  But if there is a network or NAS issue then the backups won't get written, so no current backups.  What I do in this scenario is write the backups locally first, to c:\DB_backups, then script a task to sync the backup directory to the NAS.  If I have daily backups then the NAS will store all of the daily rolling backups, but that will eat up space on the C drive, which is bad.  So I need to purge the DB_backups folder, there are a few ways to do this and the easiest I found is to use powershell:
 Get-ChildItem 'C:\DB_Backup' -Recurse | Where {$_.creationtime -lt (Get-Date).AddDays(-10)} | Remove-Item  
This is pretty clever on powershell's part, lets step through what it does.
Get-ChildItem 'C:\DB_Backup' -Recurse
What this does is list all of the items in this folder as objects, and I am recursing through all of the folders in this folder, but that is not necessarily needed for this job.
Where {$_.creationtime -lt (Get-Date).AddDays(-10)}
The objects in this folder is then piped to this line.  Where is an alias for Where-Object.  We are using where because we only want to select files with specific attributes from this directory.  These attributes are defined by the $_.creationtime.  The creation time is then compared to today's date (Get-Date) using -lt (less than).  But we only want the files older than 10 days ago, so we subtract 10 from Get-Date, using the AddDays modifier.  So now we are listing all of the files older than 10 days, pretty neat, now to delete them.
Remove-Item
The cmd way of doing things is using del, which is actually an alias for -Remove-Item, neat.  This will remove the 10 day old objects we piped to it in the previous line.

Save it all as a .ps1 file and schedule the job in task scheduler to run daily to keep your C drive to size.

Tuesday, August 12, 2014

Hyper-V Server: Enable firewall rule to allow Hyper-V Replica over HTTP or HTTPS

The server is Hyper-V Server 2012 r2 Core and we need to enable the inbound replica traffic on the firewall.  There is no firewall GUI in server core but there is predefined rules for Hyper-V Replica which we need to enable via powershell.  There are several ways for remote access to server core, this way will use RDP because of my wonderful RDP organization with mRemoteNG.  Log on to your Hyper-V Server, move the sconfig window to the side and use the command prompt to open a powershell session.


Powershell can be ran from any directory.  Once in powershell list the default firewall rules by using the Get-NetFirewallRule command-let:

 Get-NetFirewallRule | format-table name, displaygroup, action, direction, enabled -autosize  

The table is pretty long and you may have to widen your console window to show all of the rows, but you will be looking for something like this:


Hyper-V Replica HTTP and Hyper-V Replica HTTPS are the two rules, I went with HTTP in my Hyper-V settings so I will enable that rule by using the Enable-NetFirewallRule command-let:

 Enable-NetFirewallRule -DisplayGroup "Hyper-V Replica HTTP"  

And now we have flowing replica traffic:


Sunday, August 3, 2014

Windows Protip: LockHunter to unlock or delete locked files

Once of my favorite tools is Unlocker, its used to show you which processes are locking files and allows you to unlock them, but I decided to look for a replacement because Chrome and MalwareBytes labels it as malicious.  After a quick search around I found LockHunter.  This freeware tool has some of the regular features: unlock, delete, copy locked file but also does some neat stuff:

Adds link to unlock in the context menu:


And if it cannot delete a locked file, it will automatically delete after the next system reboot!


So I copied the locked file to a new directory and set the locked original to delete on the next reboot (whenever that may be!)

Friday, August 1, 2014

Build Log: Shapeoko 2 inspired CNC Router - Part 1 Testing Electronics


I started working on a CNC Router project inspired by the Shapeoko 2.  Why inspired?  As an electronics hobbyist I have amassed a decent amount of electronics and hardware of which I am going to be using as much as I can in order to cut costs over buying the complete Shapeoko kit.  Why Shapeoko?  After some research I feel that the Shapeoko Wiki is an incredibly thorough guide for building a low cost desktop CNC router.  The wiki includes guides for electronics, hardware, and software, which coming from a single source means that everything will play nice together.  My goal is to keep the total cost below the Shapeoko 2 Full Kit AND provide a larger cutting area by scaling the X-Y axis linear motion from 500mm (19.6 inches) to 610mm (24 inches).  I am hoping for a cutting area of 18 x 18 inches.

Electronics
I wanted to get the electronics hammered out first for a couple reasons:
  1. Large portion of the cost is the controller boards, stepper motors and power supply.  Getting large costs out of the way is nice for projects.
  2. If electronics do not work, and the project is a failure, I am not stuck with custom cut hardware to build a CNC, which could be resold at a loss, it would be easier to flip stepper motors and reuse the Arduino for a different project.
Following the Shapeoko recommendations I purchased a Arduino Uno ($25) and a Synthetos gShield v5 ($49.99).  The Arduino is a micro-controller that takes G-Code (more on that later) and converts it to the individual movements that the motors need to make.  The gShield is a stepper motors driver which processes the movements and applies the proper voltages to the stepper motors so they can move.

Stepper Motors: I needed something that will mill wood, acrylic, and any type of plastic sheet.  After some research I decided on Nema 23, because even the smallest Nema 23 motors are 2-3 times stronger than Nema 17 motors.  This will be enough strength to mill hardwoods and in the future possibly aluminum.  The Shapeoko 2 uses something called Dual Drive Y which means the Y axis has two stepper motors, one for each side of the gantry.  The X axis uses a single stepper motor, and the Z axis uses a motor for moving the milling rotary tool up and down.  I already have a smaller stepper motor I am going to use for the Z axis.

Power Supply: Shapeoko recommends a 90-120 Watt 24V power supply.  I have a much smaller 24v power supply I am going to be using for testing the electronics, it should push the small Z motor fine.


Software
Disclaimer: I am using a PC, follow the links for instructions on the platform you are using.  Once I had my electronics purchased it was time to test the steppers motors out.  First you will need to install the Arduino development suite which includes the USB driver so you can flash/program your Arduino.  Download and install the Arduino IDE 1.0.5 and make sure USB driver is selected during the install.  Plug your Arduino into your computer using a USB Type A to B cable and it should show up under Device Manager:


Note that my Arduino is connected to COM3, you will need to know your port.  Next download and extract XLoader.  This tool allows you to flash the Arduino with the GRBL interpreter.  GRBL is what takes the g-code and turns it into "steps" that control the stepper motors.  Retrieve the GRBL hex file from the github.  Scroll down to Downloads and right click save-as to save the .hex file.  Open up Xloader, select the com port your arduino is hooked up to, select 115200 baud rate, browse to the GRBL .hex file you downloaded and click upload to flash the Arduino:


The TX and RX LED's on the Arduino will blink.  Once completed Xloader will say how many bytes have been written to the Arduino.  We will now finish preparing the electronics for testing:

Unplug the Arduino's USB and connect the gShield (you can flash the Arduino with the gShield installed), then connect a stepper motor to one of the axis'.  I read to always connect the stepper motors with the gShield and Arduino unplugged in order to potentially prevent any hot wires from touching anything.  Next connect the USB back into the Arduino and connect the 24v power supply to the gShield.  Red (positive) hooks to the Vmot side and black (negative) connects to the GND side.  An LED will turn on:

The power supply alligator clips are actually farther apart that what the picture implies.
I did not have instructions for wiring this motor, I just paired the wires together and connected them to the axis keeping the wires in order, at this point I am not worried about polarity.

We can now send g-code commands to it using Universal G-Code Sender (UGS).  Download the latest version and extract (I have a CNC folder on the desktop to keep all of these tools in).  Browse to the UGS folder and edit the start-windows.bat, changing the 'x' in the jar file name to the version you downloaded, in my case 64 bit:
 java -jar -Xmx256m UniversalGcodeSender-all64.jar  
Save and close.  Open Command Prompt, browse to the UGS folder, and run start-windows.bat:


The jar will launch and open up the UGS gui.  If your Arduino is plugged in it should show up under the correct COM port, leave the baud rate at 9600 and click Open.  UGS is now connected to your Arduino:


Whichever axis you plug your stepper motor(s) into, Z in my case, you can send movement command(s) by using the controls under the Manual Control tab.  Do not send commands to an axis that does not have a motor connected to it, you can overload your gShield.


Your stepper motor should be rotating and your electronics testing is completed.  Next up I will be shopping out the Nema 23 motors, power supply and parts to build the Y and X axis.