简体   繁体   English

Powershell-将PSCredential对象传递到新的命令窗口

[英]Powershell - pass PSCredential object to new command window

I have a powershell script which calls Get-Credential. 我有一个Powershell脚本,它调用Get-Credential。 The entered credentials are to be used against multiple servers later on in the script and child scripts. 稍后在脚本和子脚本中,将针对多个服务器使用输入的凭据。

The code looks something like: 该代码如下所示:

$credentials = get-credential
CMD /c start powershell.exe -noexit -File subscript.ps1 $credentials

The issue is inside subscript.ps1 I have this code just to see whats coming in: 问题在subscript.ps1内部,我有这段代码只是为了看看即将发生的事情:

$args.length
$args[0]

which gives me 1 System.Management.Automation.PSCredential 这给了我1 System.Management.Automation.PSCredential

As you can see it does not pass the actual object returned by the Get-Credential call. 如您所见,它没有传递Get-Credential调用返回的实际对象。

My guess is this is happening because we are calling the script by a new command window. 我的猜测是这种情况正在发生,因为我们正在通过新的命令窗口调用脚本。 We need to open a new command window as we need to open 4 different subscript.ps1 (with different parameters for parallel processing). 我们需要打开一个新的命令窗口,因为我们需要打开4个不同的subscript.ps1(具有用于并行处理的不同参数)。

Any ideas? 有任何想法吗?

Thanks, Andrew 谢谢,安德鲁

If you are using PowerShell V3, use below syntax to start async background task 如果您使用的是PowerShell V3,请使用以下语法启动异步后台任务

$job = Start-Job -ScriptBlock {param($cred) . C:\test.ps1 $cred} -ArgumentList $cred
wait-job $job 
receive-job $job

由于它似乎已经在PowerShell中Start-Process ,因此请使用接受-CredentialStart-Process cmdlet。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM