使用凭据时Powershell启动过程出现错误

时间:2020-06-02 14:48:19

标签: powershell start-process

因此,我设置了一些要从加密的文本文件中导入的凭据,我正在尝试将其与“启动进程” cmdlet结合使用。 我是Powershell的新手,所以cld缺少明显的平滑处理,但是如果我输入Start-Process powershell.exe,它将按预期方式打开一个新的ps窗口,但是尝试StartProcess powershell.exe -Credential $cred无效。我知道这不是没有对所提供凭据的访问权,因为我被错误消息Start-Process: This command cannot be run due to the error ; The filename or extension is too long击中了 这让我感到困惑,因为我还没有更改文件名/源-我提供的所有身份证明都已提供。我什至已经确认$cred确实是一个凭证,并且如果我在终端中键入它,我的确会返回用户名和System.Security.SecureString。

有什么想法吗?

编辑:我的代码如下:

[string]$pswd= Get-Content 'C:\Users\USER\pswd.txt'
[SecureString]$ss= $pswd | ConvertTo-SecureString -AsPlainText -Force
[string]$un ='USERNAME'
[PSCredential]$cred = New-Object System.Management.Automation.PSCredential ($un, $ss)
Start-Process powershell -Credential $cred 

并且密码设置如下:

[string]$Password='PASSWORD'
[SecureString]$SS = $Password | ConvertTo-SecureString -AsPlainText -Force
[string]$STR = $SS | ConvertFrom-SecureString
$STR | Set-Content -Path 'C:\Users\USER\pswd.txt'

1 个答案:

答案 0 :(得分:0)

好的,问题在于您保存安全字符串以供以后读取的方式。试试这个:

$pwd = "Password"
$pwds = $pwd | ConvertTo-SecureString -AsPlainText -Force
$pwdse = ConvertFrom-SecureString -SecureString $pwds
$pwdse | Out-File "C:\Users\User\pswd.txt"

然后将其读回:

$pwd = Get-Content "C:\Users\User\pswd.txt" | ConvertTo-SecureString

从那时起,您可以像平常一样创建PSCredential对象,它应该可以正常工作。