【问题标题】:Powershell: How do I connect to a network folder on a different domain with stored non-plaintext username/passwordPowershell:如何使用存储的非明文用户名/密码连接到不同域上的网络文件夹
【发布时间】:2011-06-07 09:14:33
【问题描述】:

我正在尝试通过 powershell 连接到网络共享。网络共享位于不同的域中,因此我需要提供凭据。由于 New-PSDrive 不支持凭据,我打算使用 net use,但我担心将我的用户名/密码以纯文本形式放入脚本中.我强制 ConvertTo-SecureString 从我的密码中创建一个安全字符串,然后使用 ConvertFrom-SecureString 并将结果存储在一个文件中。然后,我像这样将它从文件中取出并尝试net use

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

net use 无法识别安全字符串。

任何人对如何存储凭据以便 net use 可以使用它们有任何想法吗?谢谢!

【问题讨论】:

    标签: networking powershell network-programming share


    【解决方案1】:

    这是我用于加密/解密字符串的两个函数。它们改编自 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
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-12-31
      • 2010-12-04
      • 2021-04-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-04-11
      • 1970-01-01
      相关资源
      最近更新 更多