<#

Deploying profiles is one of the dark arts of PowerShell. In view of their power to
affect user experience, convenience and efficiency, this field of PowerShell is
underappreciated.

The sample profile below does several things. First, it imports the Active Directory
module, a module that is necessary to use cmdlets like 'Get-ADUser,' very useful in
working with the directory. Once it is in the user's profile, it is no longer necessary
to add it to any PowerShell script that the user runs; inclusion is automatic.

Secondly, most of the profile script below is concerned with tracking work on scripts.
Using the .NET "FileSystemWatcher," the profile registers events for changes, renames and
creates. Such events are captured as the user works on ".ps1" files in the given path.

In this version, PowerShell scripts are found at directories with 'MyFIMScripts', 'Collection',
and 'Scripts' in the directory name.

Notably, for example, when a change to a script is detected, a corresponding directory
is written to such that a version of the script is maintained in the 'shadow' directory
as changes are made. Each 'save' event in the main directory script results in a new
version of the script in the 'shadow' directory.

Included in the title of each new version in the shadow directory is the name of the
user who is making the change, as determined by the built-n variable "$env:username."

Individual users may be assigned a profile of their own and profiles for all users
can also be constructed. Full discussions of profile scopes are readily available
on the Internet.

NB:  It also occurred to me that a Windows service could be written that would perform
essentially these same tasks.  Such a service could even conceivably be configured
as a mini-TFS by watching over .cs and other dev files as they are worked on and saved/created.

#>


import-module activedirectory

$watcher = New-Object System.IO.FileSystemWatcher
$watcher.Path = 'H:\'
$watcher.IncludeSubdirectories = $true
$watcher.EnableRaisingEvents = $true

$changed = Register-ObjectEvent $watcher "Changed" -Action {

try
{
$dte = Get-Date
$dte = $dte -replace '/','_'
$dte = $dte -replace ':',''
$dte = $dte -replace ' ',''
$idx0 = $($eventArgs.FullPath).IndexOf('.ps1')
if($idx0 -ne -1)
{
$pp = $($eventArgs.FullPath).Substring(0,$idx0)
$pp1 = $pp + '_' + $dte + '.ps1'
$np = $($eventArgs.FullPath) + '_' + $dte
if($pp1.IndexOf('MyFIMScripts') -gt -1)
{
$np2 = $pp1 -replace 'MyFIMScripts','VERSIONS2'
}
if($pp1.IndexOf('Collection') -gt -1 -and $pp1.IndexOf('Collection2') -eq -1)
{
$np2 = $pp1 -replace 'Collection','Collection2'
}
if($pp1.IndexOf('Scripts') -gt -1 -and $pp1.IndexOf('MyFIMScripts') -eq -1 -and $pp1.IndexOf('Scripts2') -eq -1)
{
write-host 'in Scripts\Scripts2'
$np2 = $pp1 -replace 'Scripts','Scripts2'
}

$wv = $pp1 -replace 'H:',''
$wv = $wv -replace '\\','_'
$s = $env:username.ToString().Replace('.','')
write-host $s
$wv = $s + '_' + $wv
$new_path = 'C:\WorkingVersions\' + $wv
Copy-Item $($eventArgs.FullPath) $new_path
write-host 'change $pp1 = ' + $pp1
write-host 'change $np2 = ' + $np2
}
}
catch
{
write-host $error[0]
write-host 'change gave an error...'

}

}

$created = Register-ObjectEvent $watcher "Created" -Action {

try
{
$idx0 = $($eventArgs.FullPath).IndexOf('.ps1')
if($idx0 -gt -1)
{
$pp = $($eventArgs.FullPath).Substring(0,$idx0)
$pp1 = $pp + '_' + $dte + '.ps1'
if($pp1.IndexOf('MyFIMScripts') -gt -1)
{
$np2 = $pp1 -replace 'MyFIMScripts','VERSIONS2'
}
if($pp1.IndexOf('Collection') -gt -1 -and $pp1.IndexOf('Collection2') -eq -1)
{
$np2 = $pp1 -replace 'Collection','Collection2'
}
if($pp1.IndexOf('Scripts') -gt -1 -and $pp1.IndexOf('MyFIMScripts') -eq -1 -and $pp1.IndexOf('Scripts2') -eq -1)
{
write-host 'in Scripts\Scripts2'
$np2 = $pp1 -replace 'Scripts','Scripts2'
}

$wv = $pp1 -replace 'H:',''
$wv = $wv -replace '\\','_'
$s = $env:username.ToString().Replace('.','')
write-host $s
$wv = $s + '_' + $wv
$new_path = 'C:\WorkingVersions' + $wv
Copy-Item $($eventArgs.FullPath) $new_path

write-host 'create $pp1 = ' + $pp1
write-host 'create $np2 = ' + $np2
}
}
catch
{
write-host $error[0]
write-host 'created gave an error in my profile'

}
}

$renamed = Register-ObjectEvent $watcher "Renamed" -Action {

try
{
$dte = Get-Date
$dte = $dte -replace '/','_'
$dte = $dte -replace ':',''
$dte = $dte -replace ' ',''
$idx0 = $($eventArgs.FullPath).IndexOf('.ps1')
$pp = $($eventArgs.FullPath).Substring(0,$idx0)
$pp1 = $pp + '_' + $dte + '.ps1'
$np = $($eventArgs.FullPath) + '_' + $dte
if($pp1.IndexOf('MyFIMScripts') -gt -1)
{
$np2 = $pp1 -replace 'MyFIMScripts','VERSIONS2'
}
if($pp1.IndexOf('Collection') -gt -1 -and $pp1.IndexOf('Collection2') -eq -1)
{
$np2 = $pp1 -replace 'Collection','Collection2'
}
if($pp1.IndexOf('Scripts') -gt -1 -and $pp1.IndexOf('MyFIMScripts') -eq -1 -and $pp1.IndexOf('Scripts2') -eq -1)
{
write-host 'in Scripts\Scripts2'
$np2 = $pp1 -replace 'Scripts','Scripts2'
}

$wv = $pp1 -replace 'H:',''
$wv = $wv -replace '\\','_'
$s = $env:username.ToString().Replace('.','')
write-host $s
$wv = $s + '_' + $wv
$new_path = 'C:\WorkingVersions' + $wv
Copy-Item $($eventArgs.FullPath) $new_path

write-host 'rename $pp1 = ' + $pp1
write-host 'rename $np2 = ' + $np2
}
catch
{
$error[0]
'rename gave an error'

}

}
do
{
[System.Windows.Forms.Application]::DoEvents()
}
while(1 -eq 1)