Starting .ps1 Script from PowerShell with Parameters and Credentials and getting output from it

蓝咒 提交于 2020-04-11 18:13:30

问题


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 performing multiple tasks and return String Values.

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

Assuming script1.ps1 is:

$a = 1+2
$a

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.

Thank you very much!


回答1:


Start-Process produces no output by default.

(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.)

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]

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

  • 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).

  • 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

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.

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


[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 (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.

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.

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


来源:https://stackoverflow.com/questions/60885735/starting-ps1-script-from-powershell-with-parameters-and-credentials-and-getting

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!