首页 > 解决方案 > 如何运行 powershell 脚本或更改会话并使用不同的用户帐户执行代码?

问题描述

我需要使用当前作为系统用户运行的 ec2 userdata 中的域用户帐户运行脚本。

我的代码:

start-Process powershell.exe -Credential  $credential -ArgumentList “Start-Process powershell.exe 
'C:\Users\test\Desktop\Test\test.ps1' -Verb runAs”

但它正在创建一个可能导致执行错误的弹出窗口

标签: windowspowershellamazon-ec2user-accountsuser-data

解决方案


只需将 NoNewWindow 开关添加到末尾即可。

start-Process powershell.exe -Credential $credential -ArgumentList “Start-Process powershell.exe 'C:\Users\test\Desktop\Test\test.ps1' -Verb runAs” -NoNewWindow

推荐阅读