PSKeePass / PoShKeePass

PowerShell module for KeePass
MIT License
255 stars 56 forks source link

Issue: Remove-Variable : Cannot find a variable with the name 'Credential'. #174

Open c3rberus opened 4 years ago

c3rberus commented 4 years ago

I am using PoShKeePass 2.1.3.0 on Windows 10.

I created my database and configuration, when i try to add or get commands they work but they all throw an error.

Get-KeePassEntry -DatabaseProfileName testprofile -Title testentry -MasterKey $credential -AsPlainText | Select -ExpandProperty notes

I can see the notes being retrieved, but it also outputs...

Remove-Variable : Cannot find a variable with the name 'Credential'.
At C:\Program Files\WindowsPowerShell\Modules\PoShKeePass\2.1.3.0\PoShKeePass.psm1:141 char:26
+         if($Credential){ Remove-Variable -Name 'Credential' }
+                          ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : ObjectNotFound: (Credential:String) [Remove-Variable], ItemNotFoundException
    + FullyQualifiedErrorId : VariableNotFound,Microsoft.PowerShell.Commands.RemoveVariableCommand

My credentials are setup as follows..

$password = ConvertTo-SecureString "password" -AsPlainText -Force
$credential = New-Object System.Management.Automation.PSCredential ('root', $password)

This is happening with New-KeePassEntry and Get-KeePassEntry.

I can see this was reported in a earlier version and marked as fixed in bug #100 so not sure why I am getting this on latest build?

c3rberus commented 4 years ago

After some testing it appears if I set my master password in $Credential it throws the error reported.

$pw = ConvertTo-SecureString "password" -AsPlainText -Force $Credential = New-Object System.Management.Automation.PSCredential ('root', $pw)

If I change this so that the variable is not called $Credential and instead something different such as $MasterPassword it works without any errors.

$pw = ConvertTo-SecureString "password" -AsPlainText -Force $MyMasterPassword = New-Object System.Management.Automation.PSCredential ('root', $pw)

The New-KeePassEntry and Get-KeePassEntry don't throw errors.

I just won't use $Credential variable as that seems to make the code unhappy.

fullenw1 commented 4 years ago

I have the same issue but I am not using credential, but a MasterKey instead.

It works, but every time I have the error message at the end of the result.

New-KeePassDatabaseConfiguration -DatabaseProfileName MyKeepPassDB -DatabasePath C:\Password.kdbx -UseMasterKey

$MasterKey = Read-Host -AsSecureString
Get-KeePassEntry -DatabaseProfileName MyKeepPassDB -MasterKey $MasterKey
Vitliz commented 4 years ago

Same issue here also not using $Credentials. Everything works fine I can create groups and users in my keepass database but I always get the error message.

 $encrypted = Get-Content $masterKeyPath | ConvertTo-SecureString
$MyMasterPassword = New-Object System.Management.Automation.PsCredential("Test", $encrypted)
New-KeePassDatabase -MasterKey $MyMasterPassword -DatabasePath $databasePath
New-KeePassDatabaseConfiguration -DatabaseProfileName $databaseProfileName -DatabasePath $databasePath -UseMasterKey 
New-KeePassGroup -DatabaseProfileName $databaseProfileName -KeePassGroupParentPath $databaseProfileName -KeePassGroupName "Benutzer" -MasterKey $encrypted
xlrod commented 4 years ago

This is very strange, I had to do $Credential = $null before Get-KeePassEntry and the error stopped appearing. I restarted the Powershell Session and tried again without initializing $Credential to $null and it didn't throw the error. If I initialize $Credential with something else like a string, it throws the error again.

markdomansky commented 4 years ago

I'm nearly positive this is a variable scope issue. If you have a $credential variable for any reason (and it's not null), then the test passes (if ($credential) {} is checking if it's not null/empty). If I get a few minutes, I'll probably try to fix this and issue a pull request.

Cynomus commented 2 years ago

I used this to get around the problem for now.

IF($Credential){$MySavedCredential = $Credential; $Credential = $NULL}

Update-KeePassEntry -KeePassEntry $KeePassEntry -DatabaseProfileName DAS-Database -Title $KeePassEntry.Title -KeePassPassword $PASAccountPasswordSecureString -KeePassEntryGroupPath $KeePassEntry.FullPath -ExpiryTime $PasswordExiresDate -MasterKey $MasterKey -Confirm:0

IF($MySavedCredential){$Credential = $MySavedCredential; $MySavedCredential = $NULL}

Also in file: PoShKeePass.psm1 Function: ConvertTo-KPPSObject

I changed line 141 to: if($script:Credential){ Remove-Variable -Name $script:Credential -EA SilentlyContinue -Confirm:0 }

dumpvn commented 2 years ago

I got same issue and found this thread.

McGreggor commented 1 year ago

Experiencing the same issue here on v2.1.3 when using Get-KeePassEntry, New-KeePassEntry and Update-KeePassEntry.

nczsl commented 7 months ago

$whitelist = @( "?", "^", "$", "args", "ConfirmPreference", "DebugPreference", "EnabledExperimentalFeatures", "Error", "ErrorActionPreference", "ErrorView", "ExecutionContext", "false", "FormatEnumerationLimit", "HOME", "Host", "InformationPreference", "input", "IsCoreCLR", "IsLinux", "IsMacOS", "IsWindows", "MaximumHistoryCount", "MyInvocation", "NestedPromptLevel", "null", "OutputEncoding", "PID", "PROFILE", "ProgressPreference", "PSBoundParameters", "PSCommandPath", "PSCulture", "PSDefaultParameterValues", "PSEdition", "PSEmailServer", "PSHOME", "PSNativeCommandArgumentPassing", "PSNativeCommandUseErrorActionPreference", "PSScriptRoot", "PSSessionApplicationName", "PSSessionConfigurationName", "PSSessionOption", "PSStyle", "PSUICulture", "PSVersionTable", "PWD", "ShellId", "StackTrace", "true", "VerbosePreference", "WarningPreference", "WhatIfPreference" ) function clear-user-vars{ gv | ? { $_.Name -notin $whitelist } | remove-variable -Force

echo-info "All user defined variables are removed" } // up info in the util.ps1 // and test is an other place test2.ps1 . ....\tool\psx\util.ps1

clear-user-vars

then report error "Cannot find a variable with the name xxx"