Sorry, nothing really intranet related here.  Instead, I have a few lines of code from my CorpNet ProjectFunctionLibrary.psm1 that I want to share for managing this system.

Update-ProfileSystemProfile: The goal for this system is to have the same code in each project’s $PROFILE, which is then replicated out via the subst.exe and \\tsclient\p drives.  However, each $PROFILE has one project-specific line, the one that specifies $global:ProfileData.ProjectName.  However, each project is in its own folder, and that fold shares the name of the project, so we can leverage this to auto-generate out the other projects’ $PROFILE by replicating the CorpNet one on top of the other files with that one line updated.  Of course, this means we’ll need to update the SHA hash, so that’s built-in.

Update-ProfileSystemSha256Hash: Speaking of updating hashes, this function blanket updates the hashes for all .PSM1 and .PS1 files under $HOME\WindowsPowerShell.   This relieves me of some of the headache when I am updating the Common- and ProjectFunctionLibrary.psm1 files.

If I need to make a change to the core $PROFILE (which I hope I don’t!) then I will make it in my CorpNet one, then run Update-ProfileSystemProfile.

If I need to make a change to one of the FunctionLibrary.psm1 files, I will make it in the appropriate directory, then run Update-ProfileSystemSha256Hash.

The snippet of code in the ‘#region code’ area is actually executed when the CorpNet ProjectFunctionLibrary.psm1 is loaded (i.e. each time I start a PowerShell window on my dev box.)  This one just scans all the project $PROFILEs and ensures they are all in-sync.  In other words, I have not made edits in CorpNet and not replicated them out to the other project $PROFILEs, or I have not made edits in any of the other project $PROFILEs, period.

===

Here’s the code:

 

#region header
<#
.synopsis

stuff for CorpNet logons

.description


.notes
who         What        when        why
timdunn     v1.0        2014-02-04  initial writeup
timdunn     v1.1        2014-02-06  Conditionally generate checksum in Update-ProfileSystemSha256Hash, set checksum file LastWriteTime to be same as source file's LastWritetime.

BTW, Get-Help doesn't work with comment-based help for .PSM1 modules.  Why?

#>
#endregion
#region functions


function Update-ProfileSystemProfile
{

   param (
        [string[]]$Path = @()
    );

    $scriptName = (&{$MyInvocation}).ScriptName;
    $dirName = Split-Path -Path $scriptName -Parent;
    $profileSystemFolder = Split-path -Path $dirName -Parent;
    $masterProfile = "$dirName\Microsoft.PowerShell_profile.ps1";
    $gcMasterProfile = Get-Content -Path $masterProfile;

    if (!$Path)
    {

        Get-ChildItem "$profileSystemFolder\*\Microsoft.PowerShell_profile.ps1" |
        % {
            $_.FullName;
        } |
        Set-Variable Path;

    } #if (!$Path)

    foreach ($_path in $Path)
    {

        if ($_path -eq $scriptName)
        {

            continue;

        } # if ($_path -eq $scriptName)

        $projectName = Split-Path -Leaf -Path (Split-Path -Parent -Path $_path);

        $gcMasterProfile -replace '^\$global:ProfileData.ProjectName = ''CorpNet'';', "`$global:ProfileData.ProjectName = '$projectName';" |
        Set-Content -path $_path;

        $sha256Path = "$_path.sha256";
        Get-Sha256Hash $_path > $sha256Path;
        (Get-Item $sha256Path).LastWriteTime = (Get-Item $_path).LastAccessTime;

        Write-Host -ForegroundColor Magenta -BackgroundColor Black "$_path updated.";

    } # foreach ($_path in $Path)

} # function Update-ProfileSystemProfile


function Update-ProfileSystemSha256Hash
{
    param (
        [string[]]$Path = @(),
        [switch]$Force
    );

    $scriptName = (&{$MyInvocation}).ScriptName;
    $dirName = Split-Path -Path $scriptName -Parent;
    $profileSystemFolder = Split-path -Path $dirName -Parent;

    if (!$Path)
    {

        Get-ChildItem "$profileSystemFolder" -recurse -Include "*.psm1", "*.ps1" |
        % {
            $_.FullName;
        } |
        Set-Variable Path;

    } #if (!$Path)

    foreach ($_path in $Path)
    {

        $sha256Path = "$_path.sha256";

        #Get-Sha256Hash $_path > $sha256Path;

        $storedCheckSum = Get-Content $sha256Path;
        $computedCheckSum = Get-Sha256Hash $_path;

        if ($force -or ($computedCheckSum -ne $storedCheckSum))
        {
        
            $computedCheckSum > $sha256Path;
            (Get-Item $sha256Path).LastWriteTime = (Get-Item $_path).LastWriteTime;
            Write-Host -ForegroundColor Magenta -BackgroundColor Black "$sha256Path updated.";
        
        } # if ($computedCheckSum -ne $storedCheckSum)

    } # foreach ($_path in $Path)

} # function Update-ProfileSystemSha256Hash


#endregion
#region code


& { # ensure all Microsoft.PowerShell_profile.ps1 files are synced up

    # only difference should be the "$global:ProfileData.ProjectName"
    
    $scriptName= (&{$MyInvocation}).ScriptName;
    $dirName = Split-Path -Parent -Path $scriptName;

    $referencePath = "$dirname\Microsoft.PowerShell_profile.ps1";
    $gcReference = Get-Content -Path $referencePath;

    Get-ChildItem -Path "$dirName\..\*\Microsoft.PowerShell_profile.ps1" |
    % {

        $Path = $_.FullName;
        $gcDifference = Get-Content -Path $Path;

        if (
            Compare-Object -ReferenceObject $gcReference -DifferenceObject $gcDifference |
            Where-Object { $_.InputObject -notmatch '^\$global:ProfileData.ProjectName'; }
        )
        {
            Write-Warning "$Path is not in sync with $ReferencePath";
        } # if ( Compare-Object -ReferenceObject $gcReference ...

    } # Get-ChildItem -Path "$dirName\..\*\Microsoft.PowerShell_profile.ps1" |
    
} # ensure all Microsoft.PowerShell_profile.ps1 files are synced up


#endregion