From a7710f6ed4aab9b28fa9e8912b6e3b2004825ba7 Mon Sep 17 00:00:00 2001 From: Raymond Piller Date: Tue, 10 Jan 2023 00:19:19 -0600 Subject: [PATCH] Log Version used in Registry --- PSWriteLog/PSWriteLog.psm1 | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/PSWriteLog/PSWriteLog.psm1 b/PSWriteLog/PSWriteLog.psm1 index 3d5b457..77a4acc 100644 --- a/PSWriteLog/PSWriteLog.psm1 +++ b/PSWriteLog/PSWriteLog.psm1 @@ -15,6 +15,24 @@ foreach ($import in $ps1s) { # The PSD1 is generated from the build pipeline. #endregion $psd1 = Import-PowerShellDataFile ([IO.Path]::Combine($PSScriptRoot, 'PSWriteLog.psd1')) + +# Check if the current context is elevated (Are we running as an administrator?) +if ((New-Object System.Security.Principal.WindowsPrincipal([System.Security.Principal.WindowsIdentity]::GetCurrent())).IsInRole([System.Security.Principal.WindowsBuiltInRole]::Administrator)) { + # Anytime this Module is used, the version and timestamp will be stored in the registry. + # This will allow more intelligent purging of unused versions. + $versionUsed = @{ + LiteralPath = 'Registry::HKEY_LOCAL_MACHINE\SOFTWARE\VertigoRay\PSWriteLog\VersionsUsed' + Name = $psd1.ModuleVersion + Value = (Get-Date -Format 'O') + Force = $true + } + Write-Debug ('Version Used: {0}' -f ($versionUsed | ConvertTo-Json)) + if (-not (Test-Path $versionUsed.LiteralPath)) { + New-Item -Path $versionUsed.LiteralPath –Force + } + Set-ItemProperty @versionUsed +} + $moduleMember = @{ Cmdlet = $psd1.CmdletsToExport Function = $psd1.FunctionsToExport