我不会深入了解我为什么需要这样做,但用户必须能够以服务账户启动PowerShell,当PowerShell加载时,它需要运行一个脚本。我已经可以用存储的凭证(存储为安全字符串)启动PowerShell,但我无法让脚本(位于$args中)运行。我尝试了各种方法,下面是我目前的情况。如果有任何帮助,我将不胜感激。
$user = "domain\service.account"
$pwd1 = "big long huge string of characters"
$pwd = ($pwd1 | ConvertTo-SecureString)
$Credential = New-Object System.Management.Automation.PSCredential $user, $pwd
$args = "\\domain.local\location\location\location\Script\script.ps1"
Start-Process powershell.exe -Credential $Credential -ArgumentList ("-file $args")
我发现这对我很有效。
$username = 'user'
$password = 'password'
$securePassword = ConvertTo-SecureString $password -AsPlainText -Force
$credential = New-Object System.Management.Automation.PSCredential $username, $securePassword
Start-Process Notepad.exe -Credential $credential
更新:改为使用单引号,以避免Paddy指出的特殊字符问题。