Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/windows/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Windows 如何使用powershell切换当前用户?_Windows_Powershell_Switch User - Fatal编程技术网

Windows 如何使用powershell切换当前用户?

Windows 如何使用powershell切换当前用户?,windows,powershell,switch-user,Windows,Powershell,Switch User,我的任务是创建新的windows本地用户,登录并使用它,然后执行一些操作。创建新用户不是问题,但我不知道如何将当前用户切换到新用户 我所做的是一段脚本,它使用新用户启动新的powershell窗口: $secpasswd = ConvertTo-SecureString $password -AsPlainText -Force $mycreds = New-Object System.Management.Automation.PSCredential ($config_name, $secp

我的任务是创建新的windows本地用户,登录并使用它,然后执行一些操作。创建新用户不是问题,但我不知道如何将当前用户切换到新用户

我所做的是一段脚本,它使用新用户启动新的powershell窗口:

$secpasswd = ConvertTo-SecureString $password -AsPlainText -Force
$mycreds = New-Object System.Management.Automation.PSCredential ($config_name, $secpasswd)
Start-Process powershell.exe -Credential $mycreds -NoNewWindow

是否可以在此新窗口中开始执行脚本的其余部分???

简单方法如下:

  • 创建一个脚本(我们称之为init.ps1)
  • 将您希望为用户调用的所有操作都放入其中
  • 将此脚本的执行权限添加到$config\u name
  • 将最后一行更改为(-noexit仅用于调试,没有它,powershell将在执行完成后关闭窗口):

    启动进程powershell.exe-凭证$mycreds-nonewindow-ArgumentList“-noexit-命令完整\u路径\u到\u脚本\init.ps1”
  • 更困难的方法是安装WinRM并使用带有Enter PSSession的命令(本链接末尾的示例:)


    $config_name实际上是用户名“3.Add execute right to this script to$config_name”:::这是在哪里完成的?我没有看到任何地方提到$config\u名称。
    sudo su-