gpt4 book ai didi

windows - 如何使用powershell切换当前用户?

转载 作者:可可西里 更新时间:2023-11-01 13:32:26 25 4
gpt4 key购买 nike

我的任务是创建新的 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_name
  • 将最后一行更改为(-noexit 仅用于调试,没有它 powershell 将在执行完成后关闭窗口):

    Start-Process powershell.exe -Credential $mycreds -NoNewWindow -ArgumentList "-noexit -command FULL_PATH_TO_SCRIPT\init.ps1"

  • 更困难的方法是安装 WinRM 并使用带有 Enter-PSSession 的命令(此链接末尾的示例: http://technet.microsoft.com/en-us/library/hh849707.aspx)

    关于windows - 如何使用powershell切换当前用户?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15895877/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com