Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/http/4.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
使用不同的凭据调用另一个powershell pass参数_Powershell - Fatal编程技术网

使用不同的凭据调用另一个powershell pass参数

使用不同的凭据调用另一个powershell pass参数,powershell,Powershell,我想从主powershell脚本中调用另一个辅助powershell脚本。我想从主脚本向它传递一个参数,次脚本需要username参数,我想从主脚本传递给它,然后让我调用的次脚本使用不同的凭据。我想我可能会使用invoke命令,我只是不知道所有的语法,有人能给出一些我想要完成的例子,然后我会在需要的时候填空吗? 提前感谢!:-) 假设您的辅助脚本如下所示: param ( [string] $Username = $args[0] ) Write-Output -InputObject

我想从主powershell脚本中调用另一个辅助powershell脚本。我想从主脚本向它传递一个参数,次脚本需要username参数,我想从主脚本传递给它,然后让我调用的次脚本使用不同的凭据。我想我可能会使用invoke命令,我只是不知道所有的语法,有人能给出一些我想要完成的例子,然后我会在需要的时候填空吗?
提前感谢!:-)

假设您的辅助脚本如下所示:

param (
    [string] $Username = $args[0]
)
Write-Output -InputObject $Username;
您可以使用
Start Process
cmdlet使用备用凭据启动脚本

$Credential = Get-Credential;
Start-Process -Wait -NoNewWindow -FilePath powershell.exe -ArgumentList '"c:\path\to my\file.ps1" -Username "UsernameGoesHere!"' -Credential $Credential;
或者,您可以使用
Invoke命令
cmdlet:

Invoke-Command -FilePath 'c:\path\to my\script.ps1' -Credential $Credential -ArgumentList "UsernameGoesHere!";

我明白了,多亏特雷弗·沙利文为我指明了正确的方向。 最后,我将第二个ps1文件放入一个脚本块,并将其作为作业运行,然后将主脚本中的参数传递给它,如下所示

$job = Start-Job -scriptblock {
 param ($username)
 some code to run against the variable that was passed in
 } -Args $target -credential $Cred
$target是要传递给脚本块的变量 $username是脚本块接受的参数
谢谢。

在哪里指定从主脚本传递的参数??谢谢。谢谢特雷弗,我会试试看,然后告诉你进展如何。好的,我刚刚又更新了一次,因为我注意到你编辑了你的问题。看起来您需要一个
用户名
参数。