Powershell,以ArgumentList中的多个参数作为不同用户调用脚本

Powershell, calling a script as a different user with multiple arguments in ArgumentList

我有一个 cmd 脚本(最终将成为一个任务调度程序触发的任务)需要以不同的用户身份启动一个新的 Powershell 进程来调用另一个脚本,远程。ps1.

说明

层级如下:

1.0 - cmd 文件,运行s powershell:

powershell -ExecutionPolicy Bypass -File %SCRIPT_DIR%\credential.ps1

2.1 - 凭据。ps1 从通过 Powershell ISE Read-Host -AsSecureString 创建的其他用户的加密密码文件创建凭证对象ConvertFrom-安全字符串 |输出文件对象

$username = "myuser"
$securestringpwd = Get-Content -Path "C:\Desktop\pwd" | ConvertTo-SecureString
$credential = New-Object System.Management.Automation.PSCredential $username, $securestringpwd

2.2 - ...最后调用一个新的 powershell 进程,以执行 remote.ps1 带有附加参数的脚本

$abc = "example1"
$def = "example2"
$dir = "$env:SCRIPT_DIR"

Start-Process powershell.exe -Credential $credential -ArgumentList @('-ExecutionPolicy"Bypass"', '-File"$dir\remote.ps1"', '$abc', '$def')

我已经测试过,凭证 obj 确实成功验证并在新用户下创建了一个新的 PS 进程。

问题

我似乎想不出启动新的 powershell 脚本文件并传入多个参数的方法。我想我不能在 ArgumentList 中使用 ExecutionPolicy。

预期结果

为了让我 运行 最后的 remote.ps1 脚本作为新用户同时传入多个,正确的方法是什么争论?提前致谢。

平台:Windows服务器 2008 R2

Powershell:$PSVersionTable.PSVersion

Major  Minor  Build  Revision
-----  -----  -----  --------
2      0      -1     -1

你在远程有什么。ps1?我愿意添加一条评论,但这很难阅读。我不确定,但这看起来像是缺少空格和错误引号的问题。 (在单引号内,变量不被解释)

如果我 运行 这个,在需要解释的变量周围加上正确的引号,或者甚至没有引号作为最后一个例子(我添加了另一个参数“-noexit”以便能够看到输出在屏幕上并手动定义 $credential 以摆脱正在工作的整个 .bat 等):

$credential = Get-Credential
$abc = "example1"
$def = "example2"
$dir = "c:\temp"
Start-Process powershell.exe -Credential $creds -ArgumentList @('-ExecutionPolicy "Bypass"', '-noexit', "-File ""$dir\remote.ps1""", "$abc", $def)

然后,在 c:\temp\remote.ps1 我只有 :

whoami
$args

并且输出如预期的那样:

DOMAIN\username 
example1
exemple2

注意:我 运行 来自 powershell 提示的第一个脚本 运行 带有“-version 2”