Tuesday, August 16th, 2011
I was working on a script that parsed log files. The file names were log.mmddyy (log.080811). This was a little bit of a learning experience for me (new to shell scripting). I was working on a Solaris 10 box, but I thought I’d do all my dev/testing on Ubuntu. Big mistake. Linux was running bash/gnu tools. Solaris was not. After I figured out how to do it on Ubuntu, I had to figure out how to do it on Solaris (thanks Scott!), then I did it in Powershell (had done something similar already for another script) and since I’m teaching myself Python, I wanted to do it in Python.
Are they the “right” way to do them? They work! Probably not a “right” way to do them, as there is always more than one way to do something
Shell
#!/bin/bash
os=`uname`
if [ $os == 'Linux' ];
then
yesterday="$(date -d 'yesterday' +%m%d%y)"
echo $yesterday
elif [ $os == 'SunOS' ];
then
yesterday=`TZ=GMT+24 date +%m%d%y`
echo $yesterday
fi
Gnu Date
#!/usr/bin/bash
## Get Yesterday
yesterday="$(date -d 'yesterday' +%m%d%y)"
Powershell
$yesterday = (get-date -format mmddyy).adddays(-1)
$yesterday.tostring('MMddyy')
Python
import datetime
today = datetime.datetime.today()
one_day = datetime.timedelta(days=1)
yesterday = today - one_day
yesterday.strftime('%m%d%y')
Tags: bash, date, linux, powershell, python, shell, unix, yesterday
Posted in Programming | No Comments »
Wednesday, July 13th, 2011
Quick, Dirty, Simple. Set the default printer with Powershell. Needed this as a part of a logon script to set default printer for certain users on a terminal server.
## Get the Printer with WMI
$printer = Get-WmiObject -Query "Select * from Win32_Printer Where Name = 'PDFCreator'"
## Set printer as default printer
$printer.SetDefaultPrinter()
Tags: powershell, printer, WMI
Posted in Programming, System Administration | No Comments »
Tuesday, July 12th, 2011
Extended version of Hal’s original “dirtoday” script via twitter https://twitter.com/#!/halr9000/status/88211937720156160
## NAME: Dirtoday.ps1
## AUTHOR: Barry Morrison
## LASTEDIT: 07/05/2011 16:18:33
<#
.Synopsis
Get's files from today's date. Will also return narrowed results based on keyword
.Description
Get's files from today's date. Will also return narrowed results based on keyword
.Parameter Path
Will run in current directory. Path argument will allow you to define a path not in current working directory
.Parameter Keyword
Keyword argument will allow you to define a keyword to search on to narrow results.
.Example
PS C:\scripts > .\dirtoday.ps1
Directory: C:\scripts
Mode LastWriteTime Length Name
---- ------------- ------ ----
-a--- 7/5/2011 3:25 PM 114 cat
-a--- 7/5/2011 4:11 PM 2252 dirtoday.ps1
-a--- 7/5/2011 3:46 PM 848 dirtoday2.ps1
-a--- 7/5/2011 2:55 PM 110 info.txt
-a--- 7/5/2011 1:52 PM 37 test.foo
.Example
PS C:\scripts > .\dirtoday.ps1 -path "c:\Users\John Doe\Pictures"
Directory: C:\Users\John Doe\Pictures
Mode LastWriteTime Length Name
---- ------------- ------ ----
-a--- 7/5/2011 11:05 AM 49888 weinerdog.jpg
.Example
PS C:\scripts > .\dirtoday.ps1 -keyword foo
cat:2:test.foo:1:blah, blah, foo, ice cream,
test.foo:1:blah, blah, foo, ice cream,
#>
param(
[string]$Path = "",
[string]$keyword = "";
)
## Checking to see if the Keyword argument is passed -- If it is, search for $keyword
if ($keyword) {
$files = dir -Path $path | Where-Object { $_.lastwritetime -ge (get-date).date } | Select-String $keyword
## If there are not results returned, tell me!
if (!$files) {
Write-Output "Nothing Here"
}else{
## If Results exist, give them to me!
$files
}
} else {
## If not keyword argument is passed, do a simple search
$files = dir -Path $path | Where-Object { $_.lastwritetime -ge (get-date).date }
## Like above, checking to see if no results are found for the query
if (!$files) {
## Tell me if nothing exists
Write-Output "Nothing Here"
}else{
## Write results if something exists
$files
}
}
Tags: dirtoday extended, powershell
Posted in Programming | No Comments »