简体   繁体   English

在Powershell中使用加密的密码作为CMD的参数

[英]use encrypted password as parameter for CMD in Powershell

I am trying to execute an Oracle EXPDP(Oracle Data Pump) command through Powershell, using an encrypted password file so I can keep my database password out of my git repo. 我正在尝试使用加密的密码文件通过Powershell执行Oracle EXPDP(Oracle数据泵)命令,以便可以将数据库密码保留在git repo中。 Here's what my code to generate the file looks like: 这是我生成文件的代码如下所示:

"Password1" | ConvertTo-SecureString -AsPlainText -Force | ConvertFrom-SecureString | Out-File "C:\\Backups\\dbPassword.txt"

Obviously Password1 isn't the actual password, but you get the idea... 显然,Password1不是实际的密码,但是您知道了...

I want to write a script to decrypt that file, then take the decrypted "Password1" value and use it in the expdb command as my db password. 我想编写一个脚本来解密该文件,然后采用解密的“ Password1”值并将其在expdb命令中用作我的数据库密码。 Here's what I've come up with so far: 到目前为止,这是我想出的:

$dbPassword =  cat C:\backups\dbPassword.txt | convertto-securestring -AsPlainText -Force
$timeStamp = "$(get-Date -f MMddyyyy)"
$expdb = 'EXPDP'
$dbCredential = 'system/'+$dbPassword
$expdbDirectory = 'directory=backups'
$expdbFull = 'full=Y'
$expdbDRFileNamePrefix = 'EXPALL_DR_' + $timeStamp
$expdbDRFileNameDMP = $expdbDRFileNamePrefix + '.DMP'
$expdbDRFileNameLOG = $expdbDRFileNamePrefix + '.log'
$expdbDRFile = 'file=' + $expdbDRFileNameDMP
$expdbDRLog = 'log=' + $expdbDRFileNameLOG

$command = $expdb + ' ' + $dbCredential + ' ' + $expdbDirectory + ' ' + $expdbFull + ' ' + $expdbDRFile + ' ' + $expdbDRLog

Invoke-Expression $command

When I execute this, I get the following error: 执行此操作时,出现以下错误:

EXPDP : 
At line:1 char:1
+ EXPDP system/System.Security.SecureString directory=backups full=Y fi ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:String) [], RemoteException
    + FullyQualifiedErrorId : NativeCommandError

Export: Release 11.2.0.1.0 - Production on Fri Oct 14 16:09:55 2016
Copyright (c) 1982, 2009, Oracle and/or its affiliates.  All rights reserved.
UDE-01017: operation generated ORACLE error 1017
ORA-01017: invalid username/password; logon denied
Username: 

I assume I need to use the equivalent of a "toString" command to make it fully plain text for the command line. 我假设我需要使用等效于“ toString”命令的命令行使其完全纯文本。 Anyone know what this is, or if there's a way to use the PSCredential object to do this? 任何人都知道这是什么,或者是否有办法使用PSCredential对象来执行此操作?

Thanks! 谢谢!

The password doesn't decrypt itself, so you'd need to do it yourself. 密码不会自行解密,因此您需要自己进行解密。 The easiest way to do this, is to create a PSCredential object, as @briantist suggested. @briantist所建议的,最简单的方法是创建一个PSCredential对象。 It allows to retrieve the (unencrypted) password via its GetNetworkCredential() method. 它允许通过其GetNetworkCredential()方法检索(未加密的)密码。

$dbPassword = Get-Content 'C:\backups\dbPassword.txt' |
              ConvertTo-SecureString -AsPlainText -Force
$cred = New-Object Management.Automation.PSCredential('system', $dbPassword)
...
$command = $expdb + ' ' + $cred.UserName + '/' +
           $cred.GetNetworkCredential().Password + ...

However, you're storing the unencrypted password in a file, and your external command seems to expect plaintext credentials anyway, so I don't see a point in encrypting the password during the transfer from file to command. 但是,您正在将未加密的密码存储在文件中,并且您的外部命令似乎仍然希望使用纯文本凭据,因此在从文件到命令的传输过程中,我看不到加密密码的意义。 That would be like building a gate in the middle of an empty place. 这就像在空旷的地方建造一扇门。

没用的门是没用的

I wonder if the problem is your password itself. 我想知道问题是否出在您的密码本身上。 You have to be careful about what special characters are in the password. 您必须注意密码中包含哪些特殊字符。 For example, an ampersand & or a pipe | 例如, &或管道| or redirection characters <> or quotes ' (especially double " ), and even caret ^ may cause problems because they are special characters for the command interpreter. How you escape each of these will be different, so it depends on which ones you have. 或重定向字符<>或引号' (尤其是双" ),甚至脱字号^可能会引起问题,因为它们是命令解释器的特殊字符。每个字符的转义方式将有所不同,因此取决于您所拥有的字符。

You might try the same thing first with a very simple password to see if it works, and if so you can start with a password containing only alphanumerics and then add special characters one at a time to see if they work. 您可以先使用一个非常简单的密码尝试相同的操作,以查看其是否有效,如果可以,则可以从仅包含字母数字的密码开始,然后一次添加一个特殊字符以查看它们是否有效。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM