Tuesday, January 29, 2013

PowerShell Encrypted Password NET USE

I needed to copy a file from a UNC to a server today. In the past I have always used the NET USE command to connect to the share. The main problem with this is that I typically stored the password unencrypted in the script. Lame.

I figured there has to be a better way to do this. Turns out there is. Power Shell allows you to setup an encrypted password file, then decrypt that file on the fly at runtime. I know this is not the most secure solution, but it sure beats storing the password in plain text.

#Function used to decrypt password
function Decrypt([string]$exportfile)
$securepassword = ConvertTo-SecureString $exportfile
$helper = new-object -typename System.Management.Automation.PSCredential -argumentlist $username, $password
$global:pass = $helper.GetNetworkCredential().Password

#Set some variables
# Secure file stores the password for the DOMAINUSER Account the file was generated using this command
# read-host -assecurestring | convertfrom-securestring | out-file C:scriptsSecureFile.txt
$username = "DOMAINUSERNAME"
$sourcePath = "\someserverx$somefile.ext"
$destpath = "x:some path"
$securefile = "C:scriptsSecureFile.txt"

#Open up the secure file and decrypt it
$exportfile = get-content $securefile
Decrypt $exportfile

#Map the Drive
#using old school NET USE command to map the drive. This will cache the credentials so the Copy-Item command will work.
net use \serverx$ $pass /USER:$username

#copy the File and delete the drive
# Clean up the NET USE command by deleting the connection.
Copy-Item $sourcePath -Destination $destPath
net use \serverx$ /DELETE

Thursday, January 24, 2013

Basement Temp

Was able to play with the Arduino last night. Setup a simple Temperature sensor to record the temperature every minute. I then had the help of a Serial Port/Arduino proxy called Gobetwino. This allowed me to record the temperature to a text file. From there I wrote a FTP script to move the text file to my web server. Then I was able to consume the data using an ASP page.


Terrible looking website but my focus was to get it working. The FTP script runs every 10 minutes. Works pretty smooth. Now I want to add the Photo Resistor to the project to tell when the light turns on and add that to the output.

Friday, January 11, 2013

2012 NFL Penalty Data

Found a guy that put all the plays for the NFL season in a CSV file for consumption. Awesome. Here is my first analysis on the data. More penalties are called on 3rd down than any other down. Perhaps more of a pressure situation. Also interesting more penalties are called in the 2nd and 4th quarters. Need to dive down a little deeper, maybe sort by time intervals.

Wednesday, January 9, 2013

JavaScript Determine which key is pressed

Needed to act based on button pressed. So here it is.

function RandomEP(e)
var evtobj=window.event? event : e //distinguish between IE's explicit event object (window.event) and Firefox's implicit.
var unicode=evtobj.charCode? evtobj.charCode : evtobj.keyCode
var actualkey=String.fromCharCode(unicode)
if (actualkey=="d")
//your code here

Then in the body tag you insert onkeypress=”RandomEP()” or whatever your function is called.

Wednesday, January 2, 2013

TSQL Make Directory

Create a directory using xp_cmdshell. Script includes steps to enable xp_cmdshell and then disabling it when finished. Take that out if you want.

--ENABLE xp_cmdshell to execute the mkdir
EXEC sp_configure 'show advanced options', 1
GO EXEC sp_configure 'xp_cmdshell', 1
--Make the directory

       @directory VARCHAR(100) = 'C:tempjoe ' + @@SERVERNAME 
SET @cmd = ' mkdir ' + @directory
EXEC xp_cmdshell @cmd, no output

--Disable xp_cmdshell
EXEC sp_configure 'show advanced options', 1
EXEC sp_configure 'xp_cmdshell', 0