Monday, December 22, 2008

Bash Scripting with Spaces

From time to time I have I run into a space issues when trying to write bash scripts.  This website was the first solution that made sense.
  1. cat /var/lib/dpkg/info/skype.list
  2. for i in `cat /var/lib/dpkg/info/skype.list`; do echo rm $i; done
Modified command:
  1. IFS=$(echo -en "\n\b"); for i in `more /var/lib/dpkg/info/skype.list`; do echo rm \"$i\"; done
Note: the above command didn't work like I wanted it to and then I figured out why I couldn't delete skype from aptitude, it was only installed using gdeb or dpkg -i.

BASH Shell: For Loop File Names With Spaces
BASH Shell: For Loop File Names With Spaces

by Vivek Gite [Last updated: December 9, 2008]

BASH for loop works nicely under UNIX / Linux / Windows and OS X while working on set of files. However, if you try to process a for loop on file name with spaces in them you are going to have some problem. for loop uses $IFS variable to determine what the field separators are. By default $IFS is set to the space character. There are multiple solutions to this problem.
Set $IFS variable

Try it as follows:

#!/bin/bash
SAVEIFS=$IFS
IFS=$(echo -en "\n\b")
for f in *
do
echo "$f"
done
IFS=$SAVEIFS

OR

#!/bin/bash
SAVEIFS=$IFS
IFS=$(echo -en "\n\b")
# set me
FILES=/data/*
for f in $FILES
do
echo "$f"
done
# restore $IFS
IFS=$SAVEIFS

Friday, December 19, 2008

Exchange 2007 Powershell example

get-distributiongroupmember "administrative staff" | select-object displayname, primaryemailaddress, recipienttype | export-csv -path test.csv

This almost accomplishes what we want but we would like to limit the output to only where recipienttype="Usermailbox".  Whenever we try to use "select-string -pattern" (powershell's grep) we either only get the recipienttype module array or we get no output because recipienttype is not in the output.

Technorati Tags: , ,

Thursday, December 18, 2008

Powershell Example

What Can I Do With Windows PowerShell?

Scenario: Needed to test disaster recovery procedures at the hospital.  All employees were told to go to a website and type in a url.

Problem: Workstations located in the doctor-patient procedure rooms don't have that ability because of patients have been known to 'surf' the web while waiting to be helped.

Solution: Needed to copy a new http:// shortcut to every workstation ASAP

Tools used: Active Directory Users and Computers and Powershell

1. Export a list of all Procedure Workstations from Active Directory
2. I was going to use psexec tool but then remembered powershell was capable of running a foreach loop
3. Started with: cat 'secured workstations.txt' | foreach-object -process {echo copy-item 'shortcut.lnk' \\$_\c$\documents and settings\userprofile\start menu}
4. After failure I started looking at backslash as an escape character and putting parts of the phrase in quotes
5. Tested one computer with: echo 'computername' | foreach-object -process {copy-item 'shortcut.lnk' \\$_"\c$\documents and settings\userprofile\start menu\"}
6. Ran final command: cat 'secured workstations.txt' | foreach-object -process {copy-item 'shortcut.lnk' \\$_"\c$\documents and settings\userprofile\start menu\"}