Wednesday, January 11, 2012

Powershell (v2) - Using NoteProperty to Control Processing in Loop

I am doing some conceptual testing, and, to help me iron out my own test script, I have been looking for ways to control my iterations. One thing has bugged me is the use of complex counters to monitor status of data. I thought to myself, why not use the object itself to tell me it's processed state instead of relying on some convoluted status structure to infer this property. So, using Add-Member I assigned a new NoteProperty value of $true to each object after it had been processed. This way, when I iterate through my collection, I can simply read the property from the file and automatically know what files have been touched, and, what files are in need of processing. In this case, I create a test folder, generate the files, and, assign groups (based on intervals of 1000) in a hashtable value where the filename is the key. I am trying to sort of out some code for larger processes tasks and really want to nail down some simple, but, effective techniques to monitor processing status. My brain just does not handle convoluted script mechanics well. This is much more intuitive.
function wdt {
 Get-Date -Format "yyyy-MM-dd hh:mm:ss"
}

# Clearing screen
Clear-Host

# Set up variables
if(Get-Variable hashtable) {
 Clear-Variable hashtable
}
$hashtable = @{}
$counter = 1
$path = 'C:\testing\powershell\testfiles\largefiletest'
$hashtable = @{}
$counter = 1
$group = 1

# Creating new files
Write-Output "$(wdt) Creating new folder."

# Creating new folder
New-Item -Path $path -ItemType Directory -Force | Out-Null

# Creating new files
Write-Output "$(wdt) Creating new files."

# Generate test files
for ($i=1; $i -le 10000; $i++) {
 if( -not (Test-Path "$path\$($i.txt)")) {
  Write-Output "$(wdt) Creating $($i.txt)."
  New-Item -Path $Path -Name "$i.txt" -ItemType File -Value $i -Force
 }
}

# Get file info into variable
$files = Get-ChildItem $path | Where-Object {( -not $_.PSIsContainer ) -and ( $_.Extension -eq '.txt' )} | Sort-Object -Property Name

# Creating new files
Write-Output "$(wdt) Iterate collection."

# Group files into sets of 1000
foreach($file in $files) {
 Write-Output "$(wdt): Processing $($file.Name)."

 if($counter -le 1000) {
  if( -not $file.Processed ) {
   # Add file to hashtable - filename (key) $group (value)
$hashtable.Add($file.FullName, $group)

   # Add NoteProperty to file object
   $file | Add-Member -MemberType NoteProperty -Name Processed -Value $true

   # Increment Counter
   $counter++
  }
 } else {
  # Increment group
  $group++

  # Reset counter
  $counter = 1
 }
}

# Output status to screen
Write-Output "$(wdt): Sorting hashtable."

# Output collection for proof of concept
$hashtable.GetEnumerator() | Sort-Object Value, Name | Format-Table -AutoSize

0 comments:

Post a Comment