繁体   English   中英

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

[英]Powershell Start-Process bugs out when using credentials

因此,我设置了一些要从加密文本文件导入的凭据,并尝试将它们与“启动进程”cmdlet 结合使用。 I'm new to powershell so cld be missing smoething obvious, but if i type Start-Process powershell.exe it opens a new ps windows as expected, however trying StartProcess powershell.exe -Credential $cred doesnt work. 我知道这与没有提供的凭据的访问权限无关,因为我收到错误消息Start-Process: This command cannot be run due to the error; The filename or extension is too long 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'

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

$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 object,它应该可以正常工作。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM