簡體   English   中英

在Powershell中使用加密的密碼作為CMD的參數

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

我正在嘗試使用加密的密碼文件通過Powershell執行Oracle EXPDP(Oracle數據泵)命令,以便可以將數據庫密碼保留在git repo中。 這是我生成文件的代碼如下所示:

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

顯然,Password1不是實際的密碼,但是您知道了...

我想編寫一個腳本來解密該文件,然后采用解密的“ Password1”值並將其在expdb命令中用作我的數據庫密碼。 到目前為止,這是我想出的:

$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

執行此操作時,出現以下錯誤:

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: 

我假設我需要使用等效於“ toString”命令的命令行使其完全純文本。 任何人都知道這是什么,或者是否有辦法使用PSCredential對象來執行此操作?

謝謝!

密碼不會自行解密,因此您需要自己進行解密。 @briantist所建議的,最簡單的方法是創建一個PSCredential對象。 它允許通過其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 + ...

但是,您正在將未加密的密碼存儲在文件中,並且您的外部命令似乎仍然希望使用純文本憑據,因此在從文件到命令的傳輸過程中,我看不到加密密碼的意義。 這就像在空曠的地方建造一扇門。

沒用的門是沒用的

我想知道問題是否出在您的密碼本身上。 您必須注意密碼中包含哪些特殊字符。 例如, &或管道| 或重定向字符<>或引號' (尤其是雙" ),甚至脫字號^可能會引起問題,因為它們是命令解釋器的特殊字符。每個字符的轉義方式將有所不同,因此取決於您所擁有的字符。

您可以先使用一個非常簡單的密碼嘗試相同的操作,以查看其是否有效,如果可以,則可以從僅包含字母數字的密碼開始,然后一次添加一個特殊字符以查看它們是否有效。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM