Jes*_*mon 5 powershell networking share network-programming
我正在尝试通过PowerShell连接到网络共享.网络共享位于不同的域中,因此我需要提供凭据.由于New-PSDrive不支持凭证,我打算使用网络使用,但我担心将我的用户名/密码以明文形式放入脚本中.我强制ConvertTo-SecureString从我的密码中生成一个安全的字符串,然后使用ConvertFrom-SecureString并将结果存储在一个文件中.然后,我把它从这个文件中拉出来并尝试使用net:
$password = Get-Content <locationOfStoredPasswordFile> | ConvertTo-SecureString
net use q: "\\server\share" $password /user:domain\username
Run Code Online (Sandbox Code Playgroud)
但网络使用无法识别安全字符串.
任何人对如何存储凭据有任何想法,以便网络使用可以使用它们?谢谢!
这是我用来加密/解密字符串的两个函数.他们改编自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
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
4603 次 |
| 最近记录: |