Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
c3rberus opened this issue Nov 30, 2019 · 9 comments
Assignees
Labels

Comments

@c3rberus
Copy link

c3rberus commented Nov 30, 2019

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
Copy link
Author

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.

@jkdba jkdba self-assigned this Dec 3, 2019
@jkdba jkdba added the bug label Dec 3, 2019
@fullenw1
Copy link

fullenw1 commented Jan 10, 2020

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
Copy link

Vitliz commented Apr 28, 2020

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
Copy link

xlrod commented May 9, 2020

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
Copy link

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
Copy link

Cynomus commented Mar 7, 2022

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
Copy link

dumpvn commented Apr 6, 2022

I got same issue and found this thread.

@McGreggor
Copy link

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

@nczsl
Copy link

nczsl commented Feb 6, 2024

$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"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

10 participants