diff --git a/CHANGELOG.md b/CHANGELOG.md index 8dbe259..89412f2 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,4 +1,5 @@ * [PSProfile - ChangeLog](#psprofile---changelog) + * [0.6.0 - 2019-11-02](#060---2019-11-02) * [0.5.0 - 2019-10-08](#050---2019-10-08) * [0.4.1 - 2019-10-08](#041---2019-10-08) * [0.4.0 - 2019-09-22](#040---2019-09-22) @@ -19,6 +20,25 @@ # PSProfile - ChangeLog +## 0.6.0 - 2019-11-02 + +* [Issue #21](https://github.com/scrthq/PSProfile/issues/21) - _Thank you [@corbob](https://github.com/corbob)!_ + * Fixed: Project folder discovery logic to ensure that project folders with the same name are added to the dictionary without conflict. +* [Issue #22](https://github.com/scrthq/PSProfile/issues/22) - _Thank you [@corbob](https://github.com/corbob)!_ + * Added: `WithInsiders` switch parameter to `Open-Code`, `Edit-PSProfilePrompt`, and `Edit-PSProfileInitScript`. +* [Issue #23](https://github.com/scrthq/PSProfile/issues/23) + * Fixed: `$PSProfile` variable should exist regardless of when you import the module (removed conditional variable setting from PSM1). +* [Issue #26](https://github.com/scrthq/PSProfile/issues/26) + * Fixed: `$PSProfile._globalize()` internal method will now update `$PSDefaultParameterValue` to `$global":PSDefaultParameterValue` on InitScripts / ExternalScripts / etc so `$PSDefaultParameterValue` persists in the main session as intended. +* [Issue #27](https://github.com/scrthq/PSProfile/issues/27) + * Removed: `Set-Prompt` alias to prevent conflict with `oh-my-posh` module. +* [Issue #29](https://github.com/scrthq/PSProfile/issues/29) + * Fixed: Secrets now persist across refreshes and sessions as intended. Details: + * Removed `PSProfileVault` class, replaced with pure hashtable. + * Updated the Secrets management functions to work directly against the Vault hashtable. +* [Issue #31](https://github.com/scrthq/PSProfile/issues/31) + * Fixed: Same as [Issue #29](https://github.com/scrthq/PSProfile/issues/29). + ## 0.5.0 - 2019-10-08 * Miscellaneous diff --git a/PSProfile/Classes/PSProfile.Classes.ps1 b/PSProfile/Classes/PSProfile.Classes.ps1 index 7194ae2..7bfddae 100644 --- a/PSProfile/Classes/PSProfile.Classes.ps1 +++ b/PSProfile/Classes/PSProfile.Classes.ps1 @@ -52,45 +52,7 @@ class PSProfileSecret { $this.SecureString = $secureString } } -class PSProfileVault : Hashtable { - [hashtable] $_secrets - PSProfileVault() { - $this._secrets = @{ } - } - [void] SetSecret([string]$name, [string]$userName, [securestring]$password) { - $this._secrets[$name] = [PSCredential]::new( - $userName, - $password - ) - } - [void] SetSecret([pscredential]$psCredential) { - $this._secrets[$psCredential.UserName] = $psCredential - } - [void] SetSecret([string]$name, [pscredential]$psCredential) { - $this._secrets[$name] = $psCredential - } - [void] SetSecret([string]$name, [securestring]$secureString) { - $this._secrets[$name] = $secureString - } - [pscredential] GetSecret() { - if ($env:USERNAME) { - return $this._secrets[$env:USERNAME] - } - elseif ($env:USER) { - return $this._secrets[$env:USER] - } - else { - return $null - } - } - [object] GetSecret([string]$name) { - return $this._secrets[$name] - } - [void] RemoveSecret([string]$name) { - $this._secrets.Remove($name) - } -} class PSProfile { hidden [System.Collections.Generic.List[PSProfileEvent]] $Log [hashtable] $_internal @@ -112,11 +74,11 @@ class PSProfile { [string[]] $ScriptPaths [hashtable] $SymbolicLinks [hashtable] $Variables - [PSProfileVault] $Vault + [hashtable] $Vault PSProfile() { $this.Log = [System.Collections.Generic.List[PSProfileEvent]]::new() - $this.Vault = [PSProfileVault]::new() + $this.Vault = @{_secrets = @{}} $this._internal = @{ } $this.GitPathMap = @{ } $this.PSBuildPathMap = @{ } @@ -150,9 +112,9 @@ class PSProfile { Default = "AWS: " } } - PSReadline = @{ - Options = @{} - KeyHandlers = @{} + PSReadline = @{ + Options = @{ } + KeyHandlers = @{ } } } $this.RefreshFrequency = (New-TimeSpan -Hours 1).ToString() @@ -160,7 +122,7 @@ class PSProfile { $this.LastSave = [datetime]::Now $this.ProjectPaths = @() $this.PluginPaths = @() - $this.InitScripts = @{} + $this.InitScripts = @{ } $this.ScriptPaths = @() $this.PathAliases = @{ '~' = [System.Environment]::GetFolderPath([System.Environment+SpecialFolder]::UserProfile) @@ -245,7 +207,7 @@ if ($env:AWS_PROFILE) { "`n>> "' $plugPaths = @((Join-Path $PSScriptRoot "Plugins")) $curVer = (Import-Metadata (Join-Path $PSScriptRoot "PSProfile.psd1")).ModuleVersion - $this.PluginPaths | Where-Object {-not [string]::IsNullOrEmpty($_) -and ($_ -match "[\/\\](Modules|BuildOutput)[\/\\]PSProfile[\/\\]$curVer" -or $_ -notmatch "[\/\\](Modules|BuildOutput)[\/\\]PSProfile[\/\\]\d+\.\d+\.\d+") } | ForEach-Object { + $this.PluginPaths | Where-Object { -not [string]::IsNullOrEmpty($_) -and ($_ -match "[\/\\](Modules|BuildOutput)[\/\\]PSProfile[\/\\]$curVer" -or $_ -notmatch "[\/\\](Modules|BuildOutput)[\/\\]PSProfile[\/\\]\d+\.\d+\.\d+") } | ForEach-Object { $plugPaths += $_ } $this.PluginPaths = $plugPaths | Select-Object -Unique @@ -336,6 +298,7 @@ if ($env:AWS_PROFILE) { $content = $content.Replace($fullValue, "function global:$funcName {") } } + $content = $content -replace '\$PSDefaultParameterValues','$global:PSDefaultParameterValues' return $content } hidden [void] _cleanConfig() { @@ -351,16 +314,16 @@ if ($env:AWS_PROFILE) { "Verbose" ) [hashtable[]]$final = @() - $this.$section | Where-Object {$_ -is [hashtable] -and $_.Name} | ForEach-Object { + $this.$section | Where-Object { $_ -is [hashtable] -and $_.Name } | ForEach-Object { $final += $_ } - $this.$section | Where-Object {$_ -is [string]} | ForEach-Object { + $this.$section | Where-Object { $_ -is [string] } | ForEach-Object { $this._log( "[$section] Converting module string to hashtable: $_", "CleanConfig", "Verbose" ) - $final += @{Name = $_} + $final += @{Name = $_ } } $this.$section = $final } @@ -371,7 +334,7 @@ if ($env:AWS_PROFILE) { "Verbose" ) [string[]]$final = @() - $this.$section | Where-Object {-not [string]::IsNullOrEmpty($_)} | ForEach-Object { + $this.$section | Where-Object { -not [string]::IsNullOrEmpty($_) } | ForEach-Object { $final += $_ } $this.$section = $final @@ -638,7 +601,7 @@ if ($env:AWS_PROFILE) { $g = 0 $b = 0 $pInfo.EnumerateDirectories('.git',[System.IO.SearchOption]::AllDirectories) | ForEach-Object { - $PathName = $_.Parent.BaseName + $PathName = $_.Parent.Name $FullPathName = $_.Parent.FullName $g++ $this._log( @@ -646,25 +609,25 @@ if ($env:AWS_PROFILE) { 'FindProjects', 'Verbose' ) - $currPath = $_ - while($this.GitPathMap.ContainsKey($PathName)){ - $currPath = $currPath.Parent - $doublePath = [System.IO.DirectoryInfo]::new($this.GitPathMap[$PathName]) - $this.GitPathMap["$($doublePath.Parent)\$($doublePath.BaseName)"] = $doublePath.FullName - $this.GitPathMap.Remove($PathName) - if($this.PSBuildPathMap.ContainsKey($PathName)){ - $PSBuildPath = [System.IO.DirectoryInfo]::new($this.PSBuildPathMap[$PathName]) - $this.PSBuildPathMap["$($PSBuildPath.Parent)\$($PSBuildPath.BaseName)"] = $doublePath.FullName - $this.PSBuildPathMap.Remove($PathName) - } - $PathName = "$($currPath.Parent.BaseName)\$PathName" + $currPath = $_ + while ($this.GitPathMap.ContainsKey($PathName)) { + $currPath = $currPath.Parent + $doublePath = [System.IO.DirectoryInfo]::new($this.GitPathMap[$PathName]) + $this.GitPathMap["$($doublePath.Parent.Name)$([System.IO.Path]::DirectorySeparatorChar)$($doublePath.Name)"] = $doublePath.FullName + $this.GitPathMap.Remove($PathName) + if ($this.PSBuildPathMap.ContainsKey($PathName)) { + $PSBuildPath = [System.IO.DirectoryInfo]::new($this.PSBuildPathMap[$PathName]) + $this.PSBuildPathMap["$($PSBuildPath.Parent.Name)$([System.IO.Path]::DirectorySeparatorChar)$($PSBuildPath.Name)"] = $doublePath.FullName + $this.PSBuildPathMap.Remove($PathName) } + $PathName = "$($currPath.Parent.BaseName)$([System.IO.Path]::DirectorySeparatorChar)$PathName" + } $this.GitPathMap[$PathName] = $FullPathName $bldPath = [System.IO.Path]::Combine($FullPathName,'build.ps1') if ([System.IO.File]::Exists($bldPath)) { $b++ $this._log( - "Found build script @ $($_.FullName)", + "Found build script @ $($bldPath)", 'FindProjects', 'Verbose' ) @@ -954,74 +917,74 @@ if ($env:AWS_PROFILE) { ) if ($this.Plugins.Count) { $this.Plugins.ForEach( { - if ($_.Name -ne 'PSProfile.PowerTools') { - $plugin = $_ - $this._log( - "'$($plugin.Name)' Searching for plugin", - 'LoadPlugins', - 'Verbose' - ) - try { - $found = $null - $importParams = @{ - ErrorAction = 'Stop' - Global = $true - } - if ($plugin.ArgumentList) { - $importParams['ArgumentList'] = $plugin.ArgumentList - } - [string[]]$pathsToSearch = @($this.PluginPaths) - $env:PSModulePath.Split([System.IO.Path]::PathSeparator) | ForEach-Object { - $pathsToSearch += $_ - } - foreach ($plugPath in $pathsToSearch) { - $fullPath = [System.IO.Path]::Combine($plugPath,"$($plugin.Name).ps1") - $this._log( - "'$($plugin.Name)' Checking path: $fullPath", - 'LoadPlugins', - 'Debug' - ) - if (Test-Path $fullPath) { - $sb = [scriptblock]::Create($this._globalize(([System.IO.File]::ReadAllText($fullPath)))) - if ($plugin.ArgumentList) { - .$sb($plugin.ArgumentList) - } - else { - .$sb - } - $found = $fullPath - break + if ($_.Name -ne 'PSProfile.PowerTools') { + $plugin = $_ + $this._log( + "'$($plugin.Name)' Searching for plugin", + 'LoadPlugins', + 'Verbose' + ) + try { + $found = $null + $importParams = @{ + ErrorAction = 'Stop' + Global = $true } - } - if ($null -ne $found) { - $this._log( - "'$($plugin.Name)' plugin loaded from path: $found", - 'LoadPlugins', - 'Verbose' - ) - } - else { - if ($null -ne $plugin.Name -and $null -ne (Get-Module $plugin.Name -ListAvailable -ErrorAction SilentlyContinue)) { - Import-Module $plugin.Name @importParams + if ($plugin.ArgumentList) { + $importParams['ArgumentList'] = $plugin.ArgumentList + } + [string[]]$pathsToSearch = @($this.PluginPaths) + $env:PSModulePath.Split([System.IO.Path]::PathSeparator) | ForEach-Object { + $pathsToSearch += $_ + } + foreach ($plugPath in $pathsToSearch) { + $fullPath = [System.IO.Path]::Combine($plugPath,"$($plugin.Name).ps1") $this._log( - "'$($plugin.Name)' plugin loaded from PSModulePath!", - 'LoadPlugins' + "'$($plugin.Name)' Checking path: $fullPath", + 'LoadPlugins', + 'Debug' ) + if (Test-Path $fullPath) { + $sb = [scriptblock]::Create($this._globalize(([System.IO.File]::ReadAllText($fullPath)))) + if ($plugin.ArgumentList) { + .$sb($plugin.ArgumentList) + } + else { + .$sb + } + $found = $fullPath + break + } } - else { + if ($null -ne $found) { $this._log( - "'$($plugin.Name)' plugin not found! To remove this plugin from your profile, run 'Remove-PSProfilePlugin $($plugin.Name)'", + "'$($plugin.Name)' plugin loaded from path: $found", 'LoadPlugins', - 'Warning' + 'Verbose' ) } + else { + if ($null -ne $plugin.Name -and $null -ne (Get-Module $plugin.Name -ListAvailable -ErrorAction SilentlyContinue)) { + Import-Module $plugin.Name @importParams + $this._log( + "'$($plugin.Name)' plugin loaded from PSModulePath!", + 'LoadPlugins' + ) + } + else { + $this._log( + "'$($plugin.Name)' plugin not found! To remove this plugin from your profile, run 'Remove-PSProfilePlugin $($plugin.Name)'", + 'LoadPlugins', + 'Warning' + ) + } + } + } + catch { + throw } } - catch { - throw - } - } - }) + }) } else { $this._log( diff --git a/PSProfile/PSProfile.Aliases.ps1 b/PSProfile/PSProfile.Aliases.ps1 index 2458422..8deb1f9 100644 --- a/PSProfile/PSProfile.Aliases.ps1 +++ b/PSProfile/PSProfile.Aliases.ps1 @@ -5,7 +5,6 @@ 'Save-Prompt' = 'Add-PSProfilePrompt' 'Get-Prompt' = 'Get-PSProfilePrompt' 'Edit-Prompt' = 'Edit-PSProfilePrompt' - 'Set-Prompt' = 'Switch-PSProfilePrompt' 'Switch-Prompt' = 'Switch-PSProfilePrompt' 'Remove-Prompt' = 'Remove-PSProfilePrompt' 'Copy-DynamicParameters' = 'Copy-Parameters' diff --git a/PSProfile/PSProfile.psd1 b/PSProfile/PSProfile.psd1 index 6a4927a..a52c5a0 100644 --- a/PSProfile/PSProfile.psd1 +++ b/PSProfile/PSProfile.psd1 @@ -12,7 +12,7 @@ RootModule = 'PSProfile.psm1' # Version number of this module. - ModuleVersion = '0.5.0' + ModuleVersion = '0.6.0' # Supported PSEditions CompatiblePSEditions = @('Desktop','Core') diff --git a/PSProfile/PSProfile.psm1 b/PSProfile/PSProfile.psm1 index 8fefb45..217fcbf 100644 --- a/PSProfile/PSProfile.psm1 +++ b/PSProfile/PSProfile.psm1 @@ -1,24 +1,20 @@ -# If we're in an interactive shell, load the profile. -if ([Environment]::UserInteractive -or ($null -eq [Environment]::UserInteractive -and $null -eq ([Environment]::GetCommandLineArgs() | Where-Object { $_ -like '-NonI*' }))) { - $global:OriginalPrompt = - $global:PSProfile = [PSProfile]::new() - $global:PSProfile.Load() - Export-ModuleMember -Variable PSProfile - $global:PSProfileConfigurationWatcher = [System.IO.FileSystemWatcher]::new($(Split-Path $global:PSProfile.Settings.ConfigurationPath -Parent),'Configuration.psd1') - $job = Register-ObjectEvent -InputObject $global:PSProfileConfigurationWatcher -EventName Changed -Action { - [PSProfile]$conf = Import-Configuration -Name PSProfile -CompanyName 'SCRT HQ' -Verbose:$false - $conf._internal = $global:PSProfile._internal - $global:PSProfile = $conf +$global:PSProfile = [PSProfile]::new() +$global:PSProfile.Load() +Export-ModuleMember -Variable PSProfile +$global:PSProfileConfigurationWatcher = [System.IO.FileSystemWatcher]::new($(Split-Path $global:PSProfile.Settings.ConfigurationPath -Parent),'Configuration.psd1') +$job = Register-ObjectEvent -InputObject $global:PSProfileConfigurationWatcher -EventName Changed -Action { + [PSProfile]$conf = Import-Configuration -Name PSProfile -CompanyName 'SCRT HQ' -Verbose:$false + $conf._internal = $global:PSProfile._internal + $global:PSProfile = $conf +} +$PSProfile_OnRemoveScript = { + try { + $global:PSProfileConfigurationWatcher.Dispose() } - $PSProfile_OnRemoveScript = { - try { - $global:PSProfileConfigurationWatcher.Dispose() - } - finally { - Remove-Variable PSProfile -Scope Global -Force - Remove-Variable PSProfileConfigurationWatcher -Scope Global -Force - } + finally { + Remove-Variable PSProfile -Scope Global -Force + Remove-Variable PSProfileConfigurationWatcher -Scope Global -Force } - $ExecutionContext.SessionState.Module.OnRemove += $PSProfile_OnRemoveScript - Register-EngineEvent -SourceIdentifier ([System.Management.Automation.PsEngineEvent]::Exiting) -Action $PSProfile_OnRemoveScript } +$ExecutionContext.SessionState.Module.OnRemove += $PSProfile_OnRemoveScript +Register-EngineEvent -SourceIdentifier ([System.Management.Automation.PsEngineEvent]::Exiting) -Action $PSProfile_OnRemoveScript diff --git a/PSProfile/Public/Init Scripts/Edit-PSProfileInitScript.ps1 b/PSProfile/Public/Init Scripts/Edit-PSProfileInitScript.ps1 index ec48a02..70f27b6 100644 --- a/PSProfile/Public/Init Scripts/Edit-PSProfileInitScript.ps1 +++ b/PSProfile/Public/Init Scripts/Edit-PSProfileInitScript.ps1 @@ -9,6 +9,9 @@ function Edit-PSProfileInitScript { .PARAMETER Name The name of the InitScript to edit from $PSProfile.InitScripts. + .PARAMETER WithInsiders + If $true, looks for VS Code Insiders to load. If $true and code-insiders cannot be found, opens the file using VS Code stable. If $false, opens the file using VS Code stable. Defaults to $false. + .PARAMETER Save If $true, saves the updated PSProfile after updating. @@ -22,16 +25,26 @@ function Edit-PSProfileInitScript { [Parameter(Mandatory,Position = 0,ValueFromPipeline,ValueFromPipelineByPropertyName)] [String[]] $Name, + [Alias('wi')] + [Alias('insiders')] + [Switch] + $WithInsiders, [Parameter()] [Switch] $Save ) Process { + $codeCommand = @('code','code-insiders') + if ($WithInsiders) { + $codeCommand = @('code-insiders','code') + } + $code = (Get-Command $codeCommand -All | Where-Object { $_.CommandType -notin @('Function','Alias') })[0].Source foreach ($initScript in $Name) { if ($Global:PSProfile.InitScripts.Contains($initScript)) { $in = @{ StdIn = $Global:PSProfile.InitScripts[$initScript].ScriptBlock TmpFile = [System.IO.Path]::Combine(([System.IO.Path]::GetTempPath()),"InitScript-$($initScript)-$(-join ((97..(97+25)|%{[char]$_}) | Get-Random -Count 3)).ps1") + Editor = $code } $handler = { Param( @@ -39,9 +52,8 @@ function Edit-PSProfileInitScript { $in ) try { - $code = (Get-Command code -All | Where-Object { $_.CommandType -notin @('Function','Alias') })[0].Source $in.StdIn | Set-Content $in.TmpFile -Force - & $code $in.TmpFile --wait + & $in.Editor $in.TmpFile --wait } catch { throw @@ -67,7 +79,7 @@ function Edit-PSProfileInitScript { Register-ArgumentCompleter -CommandName Edit-PSProfileInitScript -ParameterName Name -ScriptBlock { param($commandName, $parameterName, $wordToComplete, $commandAst, $fakeBoundParameter) - $Global:PSProfile.InitScripts.Keys | Where-Object {$_ -like "$wordToComplete*"} | ForEach-Object { + $Global:PSProfile.InitScripts.Keys | Where-Object { $_ -like "$wordToComplete*" } | ForEach-Object { [System.Management.Automation.CompletionResult]::new($_, $_, 'ParameterValue', $_) } } diff --git a/PSProfile/Public/Prompts/Edit-PSProfilePrompt.ps1 b/PSProfile/Public/Prompts/Edit-PSProfilePrompt.ps1 index 6b0bd37..a3c61fa 100644 --- a/PSProfile/Public/Prompts/Edit-PSProfilePrompt.ps1 +++ b/PSProfile/Public/Prompts/Edit-PSProfilePrompt.ps1 @@ -6,6 +6,9 @@ function Edit-PSProfilePrompt { .DESCRIPTION Enables editing the prompt from the desired editor. Once temporary file is saved, the prompt is updated in $PSProfile.Prompts. + .PARAMETER WithInsiders + If $true, looks for VS Code Insiders to load. If $true and code-insiders cannot be found, opens the file using VS Code stable. If $false, opens the file using VS Code stable. Defaults to $false. + .PARAMETER Save If $true, saves prompt back to your PSProfile after updating. @@ -16,14 +19,24 @@ function Edit-PSProfilePrompt { #> [CmdletBinding()] Param( + [Alias('wi')] + [Alias('insiders')] + [Switch] + $WithInsiders, [Parameter()] [Switch] $Save ) Process { + $codeCommand = @('code','code-insiders') + if ($WithInsiders) { + $codeCommand = @('code-insiders','code') + } + $code = (Get-Command $codeCommand -All | Where-Object { $_.CommandType -notin @('Function','Alias') })[0].Source $in = @{ StdIn = Get-PSProfilePrompt -Global TmpFile = [System.IO.Path]::Combine(([System.IO.Path]::GetTempPath()),"ps-prompt-$(-join ((97..(97+25)|%{[char]$_}) | Get-Random -Count 3)).ps1") + Editor = $code } $handler = { Param( @@ -31,9 +44,8 @@ function Edit-PSProfilePrompt { $in ) try { - $code = (Get-Command code -All | Where-Object { $_.CommandType -notin @('Function','Alias') })[0].Source $in.StdIn | Set-Content $in.TmpFile -Force - & $code $in.TmpFile --wait + & $in.Editor $in.TmpFile --wait } catch { throw diff --git a/PSProfile/Public/Secrets/Add-PSProfileSecret.ps1 b/PSProfile/Public/Secrets/Add-PSProfileSecret.ps1 index c466a68..9aef68d 100644 --- a/PSProfile/Public/Secrets/Add-PSProfileSecret.ps1 +++ b/PSProfile/Public/Secrets/Add-PSProfileSecret.ps1 @@ -52,26 +52,29 @@ function Add-PSProfileSecret { Process { switch ($PSCmdlet.ParameterSetName) { PSCredential { - if ($Force -or $null -eq $Global:PSProfile.Vault.GetSecret($Credential.UserName)) { + if ($Force -or -not $Global:PSProfile.Vault._secrets.ContainsKey($Credential.UserName)) { Write-Verbose "Adding PSCredential for user '$($Credential.UserName)' to `$PSProfile.Vault" - $Global:PSProfile.Vault.SetSecret($Credential) + $Global:PSProfile.Vault._secrets[$Credential.UserName] = $Credential + if ($Save) { + Save-PSProfile + } } - elseif (-not $Force -and $null -ne $Global:PSProfile.Vault.GetSecret($Credential.UserName)) { + elseif (-not $Force -and $Global:PSProfile.Vault._secrets.ContainsKey($Credential.UserName)) { Write-Error "A secret with the name '$($Credential.UserName)' already exists! Include -Force to overwrite it." } } SecureString { - if ($Force -or $null -eq $Global:PSProfile.Vault.GetSecret($Name)) { + if ($Force -or -not $Global:PSProfile.Vault._secrets.ContainsKey($Name)) { Write-Verbose "Adding SecureString secret with name '$Name' to `$PSProfile.Vault" - $Global:PSProfile.Vault.SetSecret($Name,$SecureString) + $Global:PSProfile.Vault._secrets[$Name] = $SecureString + if ($Save) { + Save-PSProfile + } } - elseif (-not $Force -and $null -ne $Global:PSProfile.Vault.GetSecret($Name)) { + elseif (-not $Force -and $Global:PSProfile.Vault._secrets.ContainsKey($Name)) { Write-Error "A secret with the name '$Name' already exists! Include -Force to overwrite it." } } } - if ($Save) { - Save-PSProfile - } } } diff --git a/PSProfile/Public/Secrets/Get-MyCreds.ps1 b/PSProfile/Public/Secrets/Get-MyCreds.ps1 index 6314c35..c6e16a2 100644 --- a/PSProfile/Public/Secrets/Get-MyCreds.ps1 +++ b/PSProfile/Public/Secrets/Get-MyCreds.ps1 @@ -46,7 +46,7 @@ function Get-MyCreds { Process { if ($Item) { Write-Verbose "Checking Credential Vault for user '$Item'" - if ($creds = $global:PSProfile.Vault.GetSecret($Item)) { + if ($creds = $global:PSProfile.Vault._secrets[$Item]) { Write-Verbose "Found item in CredStore" if (!$env:USERDOMAIN) { $env:USERDOMAIN = [System.Environment]::MachineName @@ -59,7 +59,7 @@ function Get-MyCreds { else { $PSCmdlet.ThrowTerminatingError( [System.Management.Automation.ErrorRecord]::new( - ([System.Management.Automation.ItemNotFoundException]"Could not find secret item '$Item' in the PSProfileVault"), + ([System.Management.Automation.ItemNotFoundException]"Could not find secret '$Item' in `$PSProfile.Vault"), 'PSProfile.Vault.SecretNotFound', [System.Management.Automation.ErrorCategory]::InvalidArgument, $global:PSProfile diff --git a/PSProfile/Public/Secrets/Get-PSProfileSecret.ps1 b/PSProfile/Public/Secrets/Get-PSProfileSecret.ps1 index 5b3193f..5b1d448 100644 --- a/PSProfile/Public/Secrets/Get-PSProfileSecret.ps1 +++ b/PSProfile/Public/Secrets/Get-PSProfileSecret.ps1 @@ -7,7 +7,7 @@ function Get-PSProfileSecret { Gets a Secret from the $PSProfile.Vault. .PARAMETER Name - The name of the Secret you would like to retrieve from the Vault. + The name of the Secret you would like to retrieve from the Vault. If excluded, returns the entire Vault contents. .PARAMETER AsPlainText If $true and Confirm:$true, returns the decrypted password if the secret is a PSCredential object or the plain-text string if a SecureString. Requires confirmation. @@ -22,7 +22,7 @@ function Get-PSProfileSecret { #> [CmdletBinding(SupportsShouldProcess,ConfirmImpact = "High")] Param( - [parameter(Mandatory,Position = 0)] + [parameter(Position = 0)] [String] $Name, [parameter()] @@ -33,21 +33,28 @@ function Get-PSProfileSecret { $Force ) Process { - Write-Verbose "Getting Secret '$Name' from `$PSProfile.Vault" - $sec = $global:PSProfile.Vault.GetSecret($Name) - if ($AsPlainText -and ($Force -or $PSCmdlet.ShouldProcess("Return plain-text value for Secret '$Name'"))) { - if ($sec -is [pscredential]) { - [PSCustomObject]@{ - UserName = $sec.UserName - Password = $sec.GetNetworkCredential().Password + if ($Name) { + Write-Verbose "Getting Secret '$Name' from `$PSProfile.Vault" + if ($sec = $global:PSProfile.Vault._secrets[$Name]) { + if ($AsPlainText -and ($Force -or $PSCmdlet.ShouldProcess("Return plain-text value for Secret '$Name'"))) { + if ($sec -is [pscredential]) { + [PSCustomObject]@{ + UserName = $sec.UserName + Password = $sec.GetNetworkCredential().Password + } + } + else { + Get-DecryptedValue $sec + } + } + else { + $sec } - } - else { - Get-DecryptedValue $sec } } else { - $sec + Write-Verbose "Getting all Secrets" + $global:PSProfile.Vault._secrets } } } diff --git a/PSProfile/Public/Secrets/Remove-PSProfileSecret.ps1 b/PSProfile/Public/Secrets/Remove-PSProfileSecret.ps1 index 85c2c31..da0b34c 100644 --- a/PSProfile/Public/Secrets/Remove-PSProfileSecret.ps1 +++ b/PSProfile/Public/Secrets/Remove-PSProfileSecret.ps1 @@ -31,7 +31,7 @@ function Remove-PSProfileSecret { if ($PSCmdlet.ShouldProcess("Removing '$Name' from `$PSProfile.Vault")) { if ($Global:PSProfile.Vault._secrets.ContainsKey($Name)) { Write-Verbose "Removing '$Name' from `$PSProfile.Vault" - $Global:PSProfile.Vault.RemoveSecret($Name) + $Global:PSProfile.Vault._secrets.Remove($Name) | Out-Null } if ($Save) { Save-PSProfile