2011-01-11 57 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