【问题标题】:Powershell -NoNewWindow not working as expectedPowershell -NoNewWindow 未按预期工作
【发布时间】:2019-04-08 23:19:52
【问题描述】:

基本上我想在同一个窗口中切换powershell中的用户(不想打开一个新的)。

$username = "xxxxx"
$password = ConvertTo-SecureString "xxxxx" -AsPlainText -Force
$creds = New-Objet System.Management.Automation.PSCredential $username,$password
Start-Process powershell.exe -NoNewWindow -Credential $creds

但不是在同一个窗口中启动 powershell,而是在一个甚至不起作用的新窗口中启动它,我无法在它只是一个闪烁的光标中输入任何内容。

【问题讨论】:

  • 您不能在这样的不同上下文中的进程中运行进程。
  • @TheIncorrigible1 除了在 Powershell 中切换用户还有其他方法吗?
  • 在同一个主机内?据我所知没有。

标签: windows powershell powershell-1.0


【解决方案1】:

首先,尝试详细描述您需要做什么,因为您使用的方法可能会被误导。您是否只是想在脚本中以不同用户身份运行命令?如果是这样,请使用此处描述的方法:https://www.itdroplets.com/run-a-command-as-a-different-user-in-powershell/

我特别喜欢我有时使用的start-job方法,例如:

#Shows who is the current user
whoami
""

$username = "DOMAIN\USER"
$password = ConvertTo-SecureString "PASSWORD" -AsPlainText -Force
$Credential = New-Object System.Management.Automation.PSCredential $username,$password


$GetProcessJob = Start-Job -ScriptBlock {
#Shows who is the current user, in this case it's the user you provided credentials for. Everything in this scriptblock will run in his context.

whoami


} -Credential $Credential

#Wait until the job is completed
Wait-Job $GetProcessJob | Out-Null

#Get the Job results
$GetProcessResult = Receive-Job -Job $GetProcessJob

#Print the Job results
$GetProcessResult

如果您真的只想以另一个用户身份启动另一个 powershell.exe 进程, 我知道的唯一方法是简单地启动新进程并在该命令之后退出第一个进程,这样您就只有新窗口按照用户提供的方式运行。

$username = "DOMAIN\USER"
$password = ConvertTo-SecureString "PASSWORD" -AsPlainText -Force
$creds = New-Object System.Management.Automation.PSCredential $username,$password
Start-Process powershell.exe -Credential $creds ;Exit

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-12-05
    • 1970-01-01
    • 2021-06-04
    • 2022-01-24
    • 2015-05-11
    • 2020-05-15
    相关资源
    最近更新 更多