Skip to content

Instantly share code, notes, and snippets.

@jhochwald
Created February 20, 2021 15:15
Show Gist options
  • Save jhochwald/02d125342505a7ffee87d3b03903c8b5 to your computer and use it in GitHub Desktop.
Save jhochwald/02d125342505a7ffee87d3b03903c8b5 to your computer and use it in GitHub Desktop.
Install or update the Microsoft SecretManagement Module toolchain
#Requires -RunAsAdministrator
<#
.SYNOPSIS
Install or update the Microsoft SecretManagement Module toolchain
.DESCRIPTION
Install or update the Microsoft SecretManagement Module toolchain
If the Modules are not installed, the script will try to install them for all (AllUsers scope).
If the modules are installed, the script will try to update them.
No paramaters or any interaction needed.
.EXAMPLE
PS C:\> .\Setup-SecretManagement.ps1
.NOTES
By default, the script will try the AllUsers scope, that requires to run the script elevated (Run As Administrator)!
If you want to have the Modules installed foryourself, change the code and remove the #Requires line at the top!
#>
[CmdletBinding(ConfirmImpact = 'None')]
param ()
begin
{
$SecretManagementModules = 'Microsoft.PowerShell.SecretManagement', 'Microsoft.PowerShell.SecretStore', 'SecretManagement.KeePass'
}
process
{
foreach ($SecretManagementModule in $SecretManagementModules)
{
# Cleanup
$ModuleInfo = $null
try
{
# Get the Module Info
$ModuleInfo = (Get-Module -Name $SecretManagementModule -ListAvailable -ErrorAction Stop)
}
catch
{
# Cleanup
$ModuleInfo = $null
}
# Do we have the Module Info?
if ($ModuleInfo)
{
#region UpdateModule
try
{
# Module is installed, try an update
$null = (Update-Module -Name $SecretManagementModule -Scope AllUsers -Force -ErrorAction Stop)
}
catch
{
# Whooopsie Handler
[Management.Automation.ErrorRecord]$e = $_
# retrieve information about runtime error
$info = [PSCustomObject]@{
Exception = $e.Exception.Message
Reason = $e.CategoryInfo.Reason
Target = $e.CategoryInfo.TargetName
Script = $e.InvocationInfo.ScriptName
Line = $e.InvocationInfo.ScriptLineNumber
Column = $e.InvocationInfo.OffsetInLine
}
# Be verbose
$info | Out-String | Write-Verbose
Write-Warning -Message $info.Exception -WarningAction Continue -ErrorAction SilentlyContinueinfo
}
#endregion UpdateModule
}
else
{
#region InstallModules
try
{
# OK, let us try to install the Module from the Gallery
$null = (Install-Module -Name $SecretManagementModule -Repository PSGallery -Force -Scope AllUsers -ErrorAction Stop)
}
catch
{
# Whooopsie Handler
[Management.Automation.ErrorRecord]$e = $_
# retrieve information about runtime error
$info = [PSCustomObject]@{
Exception = $e.Exception.Message
Reason = $e.CategoryInfo.Reason
Target = $e.CategoryInfo.TargetName
Script = $e.InvocationInfo.ScriptName
Line = $e.InvocationInfo.ScriptLineNumber
Column = $e.InvocationInfo.OffsetInLine
}
# Be verbose
$info | Out-String | Write-Verbose
Write-Warning -Message $info.Exception -WarningAction Continue -ErrorAction SilentlyContinueinfo
}
#endregion InstallModules
}
}
}
end
{
# Cleanup and housekeeping
$SecretManagementModules = $null
$SecretManagementModule = $null
$ModuleInfo = $null
[GC]::Collect()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment