Powershell-非Windows未按预期工作
基本上,我想在同一窗口中切换powershell中的用户(不想打开新窗口)Powershell-非Windows未按预期工作,windows,powershell,powershell-1.0,Windows,Powershell,Powershell 1.0,基本上,我想在同一窗口中切换powershell中的用户(不想打开新窗口) 但是,与其在同一个窗口中启动powershell,不如在一个新窗口中启动它,该窗口甚至不起作用。我无法在它的窗口中键入任何内容,只是一个闪烁的光标。首先,请尝试详细描述您需要做的事情,因为您使用的方法可能会被误导。您只是想在脚本中以不同的用户身份运行命令吗?如果是,请使用此处描述的方法: 我特别喜欢有时使用的“开始工作”方法,例如: #Shows who is the current user whoami "" $u
但是,与其在同一个窗口中启动powershell,不如在一个新窗口中启动它,该窗口甚至不起作用。我无法在它的窗口中键入任何内容,只是一个闪烁的光标。首先,请尝试详细描述您需要做的事情,因为您使用的方法可能会被误导。您只是想在脚本中以不同的用户身份运行命令吗?如果是,请使用此处描述的方法: 我特别喜欢有时使用的“开始工作”方法,例如:
#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
您不能在不同的上下文中运行进程内的进程。@不可更正1是否有其他方法可以在同一主机中切换powershell中的用户?据我所知没有。
$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