使用参数和凭据从PowerShell启动.ps1脚本并从中获取输出 [英] Starting .ps1 Script from PowerShell with Parameters and Credentials and getting output from it

查看:472
本文介绍了使用参数和凭据从PowerShell启动.ps1脚本并从中获取输出的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我认为我的问题有一个简单的解决方案.但是现在我有点困惑. 我有Java代码,该代码开始1个Powershell脚本.此Powershell脚本必须启动其他脚本.
Java->
Powershell.ps1->
Script1.ps1 Script2.ps1 Script3.ps1 脚本...

I think my problem has a simple solution. But now i'm a bit confused. I have Java Code, that starts 1 Powershell Script. This Powershell Script must start other scripts.
Java ->
Powershell.ps1 ->
Script1.ps1 Script2.ps1 Script3.ps1 Script....

Script1,2,.. etc执行多个任务并返回字符串值.

Script1,2,..etc performing multiple tasks and return String Values.

我尝试过 启动过程,调用命令和调用表达

I've tried Start-Process, Invoke-Command and Invoke-Expression

假设script1.ps1为:

Assuming script1.ps1 is:

$a = 1+2
$a

Start-Process对我来说是最好的,但是我没有得到输出:

Start-Process would work the best for me but im not getting the output:

$arguments = "C:\..\script1.ps1" + " -ClientName" + $DeviceName
$output = Start-Process powershell -ArgumentList $arguments -Credential $credentials
$output 

$ output ist NULL.

$output ist NULL.

非常感谢!

推荐答案

(使它直接产生输出 的唯一方法是使用-PassThru,它不返回 script的输出,而是一个System.Diagnostics.Process实例代表新创建的过程-参见下文.)

(The only way to make it produce output directly is to use -PassThru, which then doesn't return the script's output, but a System.Diagnostics.Process instance representing the newly created process - see below.)

通过Start-Process从脚本捕获输出的唯一方法是使用-RedirectStandardOutput
-RedirectStandardError参数以文本文件形式捕获脚本输出 ,位于文件中. [1]

The only way to capture output from your script via Start-Process is to use the -RedirectStandardOutput and
-RedirectStandardError parameters to capture the script's output as text, in files.[1]

您可以在新过程完成后在PowerShell 中读取这些文件,您可以通过以下两种方式之一来确保这一点:

You can then read those files in PowerShell after the new process has completed, which you can ensure in one of two ways:

  • 还将-Wait开关传递到Start-Process,以使调用同步,这意味着Start-Process返回时,输出已被捕获到指定文件中.

  • Also pass the -Wait switch to Start-Process, to make the invocation synchronous, which means that when Start-Process returns, the output has already been captured in the specified file(s).

使用-PassThru获取 System.Diagnostics.Process 实例并将其传递给 Wait-Process 之后(或直接使用其.WaitForExit()方法;属性.HasExited可用于检查进程是否仍在运行).

Use -PassThru to obtain a System.Diagnostics.Process instance and pass it to Wait-Process later (or use its .WaitForExit() method directly; property .HasExited can be used to check whether the process is still running).

以下是您可能遇到的情况:

Here's what may work in your situation:

$arguments = "-File C:\...\script1.ps1" + " -ClientName" + $DeviceName

# Launch the script in a new window running as the given user,
# capture its standard output in file ./out.txt,
# and wait for it to finish.
Start-Process -Wait -RedirectStandardOutput ./out.txt powershell -ArgumentList $arguments -Credential $credentials

"Running script1.ps1 produced the following output:"
Get-Content ./out.txt

很遗憾,PowerShell CLI报告了PowerShell 这个答案),因此以上内容捕获了脚本中的所有输出,包括错误输出.

The PowerShell CLI, regrettably, reports all of PowerShell's 6 output streams, via standard output (see this answer), so the above captures all output from your script, including error output.

但是,您可以使用例如-RedirectStandardError ./err.txt来分别捕获错误流.

However, you can use, e.g., -RedirectStandardError ./err.txt to capture the error stream separately.

[1] 通过其

[1] Calling another PowerShell instance via its CLI offers a structured alternative to capturing unstructured text (the for-display output as it would print to the console, which is what happens by default):

-OutputFormat xml (或-of xml/-o xml)使PowerShell格式的输出为CLIXML格式,这与序列化丰富对象 ,您可以使用更高版本的

-OutputFormat xml (or -of xml / -o xml) makes PowerShell format its output in CLIXML format, which is the same XML-based serialization format used in PowerShell remoting and background jobs for serializing rich objects, which you can "rehydrate" with a later Import-Clixml call.

注意:对于大多数复杂对象,会出现类型保真度的损失 :也就是说,它们被序列化为原始对象的 emulations ;简而言之就是没有方法的财产袋",但这可能就足够了-请参阅此答案.

Note: For most complex objects there is a loss of type fidelity: that is, they are serialized as emulations of the original objects; in short as "property bags" without methods, which, however may be sufficient - see this answer.

这是一个使用[datetime]实例的快速演示,该实例使用保真度类型反序列化:

Here's a quick demonstration, using a [datetime] instance, which does deserialize with type fidelity:

Start-Process -Wait -RedirectStandardOutput ./out.xml powershell '-of xml -c Get-Date'

"Type of the CLIXML-serialized and deserialized `Get-Date` output:"
(Import-CliXml ./out.xml).GetType().FullName # -> System.DateTime

这篇关于使用参数和凭据从PowerShell启动.ps1脚本并从中获取输出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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