如何从Windows命令提示符调用作为特定用户的PowerShell脚本? [英] How can I call a PowerShell script as a particular user from the Windows command prompt?

查看:352
本文介绍了如何从Windows命令提示符调用作为特定用户的PowerShell脚本?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我以名为abc的用户登录到计算机时运行PowerShell脚本时,脚本作为用户abc运行。

When I run a PowerShell script while already logged into a machine as the user named 'abc' the script runs as the user 'abc'.

命令I在cmd中运行的是:

The command I am running in cmd is:

powershell pshell.ps1

如何使用特定域用户运行PowerShell脚本,而不是当前登录的用户?

How can I run a PowerShell script using a particular domain user, other than the currently logged in user?

意图是通过Windows命令行运行PowerShell脚本。

My intention is to run the PowerShell script through the Windows command line.

推荐答案

有几种方法来运行程序或脚本来自脚本中的另一个用户:

There are a few ways to run a program or script as another user from within a script:

如果您能够指定凭证,Windows命令行 RUNAS 命令将看起来像一个很好的解决方案。

The Windows Command Line RUNAS command would look like a good solution to your problem if you were able to specify the credentials.

RUNAS  /user:user@domain.microsoft.com "powershell pshell.ps1"

正如你所说的,你需要从CONTROL-M运行这个任务,所以这是不可能的。这也是不可能从任务计划程序运行任务,我建议在一个评论和另一个答案建议。

As you have said, however, you need to run this task from CONTROL-M, so this is not possible. It is also then not possible to run the task from Task Scheduler as I suggested in a comment and another answer has suggested.

因此,我的下一个建议是使用PowerShell脚本执行此操作:

Therefore, my next suggestion is to use a PowerShell script to do this:

首先,您需要启用Win-RM以允许此工作。为此,请在提升的(即以管理员身份运行)命令提示符下键入以下内容:

Firstly, you need to enable Win-RM to allow this to work. To do so, type the following in an elevated (i.e. run as administrator) command prompt:

winrm /quickconfig

接下来,使用所需的存储凭证编写脚本。请注意,这将以纯文本形式存储,因此如果您不愿意,您将需要查看使用安全凭据文件。

Next, write a script with the stored credentials you want. Note that this will be stored as plain text, so if you are not willing to do that you will need to look at using a secure credential file.

$username = "DOMAIN\User"
$password = "Password"
$secstr = New-Object -TypeName System.Security.SecureString
$password.ToCharArray() | ForEach-Object {$secstr.AppendChar($_)}
$cred = new-object -typename System.Management.Automation.PSCredential -argumentlist $username, $secstr
Invoke-Command -FilePath "C:\Script\To\Execute.ps1" -Credential $cred -Computer localhost

自然,您需要用要运行的PowerShell脚本的文件路径替换 C:\Script\To\Execute.ps1 ,并替换 DOMAIN \User 密码分别与您要运行的用户及其密码。

Naturally, you need to replace C:\Script\To\Execute.ps1 with the file path to your PowerShell script you want to run, and replace DOMAIN\User and Password with the user you want to run as and their password, respectively.

此脚本现在将以上面指定的用户身份运行。

This script will now run as the user specified above.

但是,您可能不愿意或无法使用PowerShell脚本,最后一个解决方案驻留在第三方应用程序中,例如:

However, you may be unwilling or unable to use a PowerShell script, and so your last solution rests in a third party application, such as:

PsExec 是一个完全免费的工具,可在TechNet上下载,专门用于运行命令,应用程序等。在远程计算机上。它在本地机器上工作非常好,并且相信它,允许您指定希望应用程序运行的特定用户(和密码!)。

PsExec is a completely free tool offered for download on TechNet specifically designed for running commands, applications, etc. on remote computers. It works perfectly well on the local machine and believe it or not, allows you to specify the specific user (and password!) you want the application to run with.


  1. 下载并解压缩应用

  2. 将应用程序放置在 PATH 属性( SET PATH = C:\PsExec;%PATH%工程,如果您将它安装到C:\PsExec)

  3. 运行命令 psexec -u DOMAIN\user -p password script.ps1 ,并进行适当的更改。

  1. Download and extract the application
  2. Put the application somewhere in your PATH attribute (SET PATH=C:\PsExec;%PATH% works, if you installed it to C:\PsExec)
  3. Run the command psexec -u DOMAIN\user -p password script.ps1 with the appropriate changes.

这篇关于如何从Windows命令提示符调用作为特定用户的PowerShell脚本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆