2011-01-11 30 views
5

我試圖通過PowerShell連接到網絡共享。網絡共享位於不同的域中,因此我需要提供憑據。由於新PSDrive不支持的憑據,我打算使用網絡使用,但我擔心把我的用戶名/密碼在那裏以純文本到腳本。我強制ConvertTo-SecureString使我的密碼安全的字符串,然後使用ConvertFrom-SecureString並將結果存儲在一個文件中。然後,我把它弄成這樣的文件,並試圖淨使用Powershell:如何連接到存儲非純文本用戶名/密碼的其他域上的網絡文件夾

$password = Get-Content <locationOfStoredPasswordFile> | ConvertTo-SecureString 
net use q: "\\server\share" $password /user:domain\username 

淨使用不承認安全字符串。

任何人有關於如何存儲憑據的任何想法,所以淨使用可以使用它們嗎?謝謝!

回答

7

這是我用來加密/解密字符串的兩個函數。他們是從powershell.com文章改編的,但我沒有鏈接方便。我們的想法是,您的密碼文件將存儲Protect-String的輸出,然後轉換回常規字符串,讀入文件並調用UnProtect-String,從UnProtect字符串返回的值將是您的$ password變量。

####################### 
function Protect-String 
{ 
    param([string]$InputString) 
    $secure = ConvertTo-SecureString $InputString -asPlainText -force 
    $export = $secure | ConvertFrom-SecureString 
    write-output $export 

} #Protect-String 

####################### 
function UnProtect-String 
{ 
    param([string]$InputString) 

    $secure = ConvertTo-SecureString $InputString 
    $helper = New-Object system.Management.Automation.PSCredential("SQLPSX", $secure) 
    $plain = $helper.GetNetworkCredential().Password 
    write-output $plain 

} #UnProtect-String 
+1

輝煌,它的工作原理。非常感謝你! – Jeshii 2011-01-13 18:36:40

相關問題