具有Runas和凭据的Powershell启动过程

时间:2020-08-13 18:36:28

标签: powershell

我正在尝试使用硬编码用户和pwd运行进程,但是我似乎只能指定凭证或符文,但不能同时指定:

$username = "[redacted]"
$password = "[redacted]"
$credentials = New-Object System.Management.Automation.PSCredential -ArgumentList @($username,(ConvertTo-SecureString -String $password -AsPlainText -Force))


# Self-elevate the script if required
# From:  https://blog.expta.com/2017/03/how-to-self-elevate-powershell-script.html
if (-Not ([Security.Principal.WindowsPrincipal] [Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole] 'Administrator')) {
 if ([int](Get-CimInstance -Class Win32_OperatingSystem | Select-Object -ExpandProperty BuildNumber) -ge 6000) {
  $CommandLine = "-noexit -noprofile -File `"" + $MyInvocation.MyCommand.Path + "`" " + $MyInvocation.UnboundArguments
   Start-Process Powershell -Verb Runas -ArgumentList $CommandLine # work, but no credential
  # Start-Process PowerShell -Cred $credentials -Verb Runas -ArgumentList $CommandLine # both credential and runas, doesnt work.
  Exit
 }
}

我如何同时使用凭据和符文运行Start-Process

0 个答案:

没有答案