5/2/10

Testing String Format in F# Interactive

printf "%02x" 10;;
System.String.Format("{0:x2}", 10);;
open System;;
String.Format("{0:x2}", 10);;
val it : string = "0a"

4/19/10

PowerShell List Dates Script

listDates '01-01' 7                       #01-01-2010
listDates '01-01' 7 'M-d'                 #1-1
listDates '01-01' 14 'ddd MM-dd'          #Fri 01-01
listDates '01-01-2011' 30                 #01-01-2011
listDates '01-01' 60 'MMM dd yyyy - ddd'  #Jan 01 2010 - Fri
function listDates($startDate, $daysCount, $format='MM-dd-yyyy')
{
  $startDate = [datetime]$startDate
  0..($daysCount-1) |
    %{
        $startDate.addDays($_).toString($format)  #console - show date
     }
}

4/9/10

HTTP Error Codes 401 and 500 on IIS 7 and ASP.NET

To fix these errors on Windows 7 Pro, add the user group/account to the web folder and set permissions.

HTTP Error 500.19 - Internal Server Error
The requested page cannot be accessed because the related configuration data for the page is invalid.
User Group: IIS_IUSRS

HTTP Error 401.3 - Unauthorized
You do not have permission to view this directory or page because of the access control list (ACL) configuration or encryption settings for this resource on the Web server.
User Account: IUSR

Permissions:
read
read and execute
list folder contents

4/8/10

PowerShell File Concatenation Script

Concatenates text files into a single file for printing, text-zip-attachments, etc.
File index is included at the top of the output file.
function concatFiles($outFile)
{
  begin
  {
    $date = get-date
    [string[]] $filelist = @()  #empty string array
    $filebreak = '-'*32
  }
  process
  {
    #for process-block vars, append data using +=
    $_.fullname  #or "$_", console - show file name
    $filelist += $_.fullname
    $filedata += "$filebreak`nfile: $_`n"
    $filedata += gc $_.fullname | out-string  #use out-string to restore linebreaks
  }
  end
  {
    $filecount = $filelist.length
    "{0} files" -f $filecount  #console - show file count
    $fileheader = "{0}`n" -f $date.toString('MM-dd-yy HH:mm:ss')
    $fileheader += "concat output file: {0}`n{1} files" -f $outFile, $filecount
    $fileheader            >> $outFile
    $filelist              >> $outFile
    $filedata + $filebreak >> $outFile
  }
}


Concatenate all text files
gci c:\docs\* -inc *.txt -rec | concatFiles c:\temp\out.txt

#powershell console output
C:\docs\file1.txt
C:\docs\file7.txt
2 files

#file output
03-01-11 14:09:22
concat output file: c:\temp\out.txt
2 files
C:\docs\file1.txt
C:\docs\file7.txt
--------------------------------
file: C:\docs\file1.txt
file 1
...
--------------------------------
file: C:\docs\file7.txt
file 7
...
--------------------------------


Concatenate files modified on Jan 1 2011 or later
$files = "*.htm","*.css","*.js"
gci c:\docs\* -inc $files -rec -force -ea silentlycontinue |
  ?{$_.lastwritetime -gt [datetime]'01-01-2011'} | concatFiles c:\temp\out.txt

3/31/10

Insert String Every N Characters

This PowerShell v2 script inserts a string every N characters. The interval is specified using the regex repetition operator, {min,max}.
$a = 'abcdefghijklmnopqrstuvwxyz'
([regex]::matches($a, '.{1,3}') | %{$_.value}) -join ' '
([regex]::matches($a, '.{1,8}') | %{$_.value}) -join '..'
([regex]::matches($a, '.{1}') | %{$_.value}) -join '-'

#output
abc def ghi jkl mno pqr stu vwx yz
abcdefgh..ijklmnop..qrstuvwx..yz
a-b-c-d-e-f-g-h-i-j-k-l-m-n-o-p-q-r-s-t-u-v-w-x-y-z

3/22/10

PowerShell Download File Script

This script downloads files asynchronously. It uses the .NET WebClient DownloadFileAsync method.
Note that the download file path must exist.
downloadFile 'http://www.foo.com/foo.iso' 'c:\temp\foo.iso'

function downloadFile($uri, $filename)
{
  $webClient = new-object net.webclient
  $webClient.downloadFileAsync($uri, $filename)
}