PowerShell - script 1 calls script 2 - how to return value from script 2 to script 1

前端 未结 1 721
南旧
南旧 2020-12-21 14:52

I have two PowerShell scripts. One script invokes another PowerShell script using elevated credentials, using Start-Process.

But I am struggling with how to make the

相关标签:
1条回答
  • 2020-12-21 15:24

    You could make use of ProcessStartInfo and Process which would allow you to read the StandardOutput.

    Here's an example of what you might do:

    $startInfo = New-Object System.Diagnostics.ProcessStartInfo
    $startInfo.FileName = "powershell.exe"
    $startInfo.Arguments = "C:\script\script2.ps1"
    
    $startInfo.RedirectStandardOutput = $true
    $startInfo.UseShellExecute = $false
    $startInfo.CreateNoWindow = $false
    $startInfo.Username = "DOMAIN\Username"
    $startInfo.Password = $password
    
    $process = New-Object System.Diagnostics.Process
    $process.StartInfo = $startInfo
    $process.Start() | Out-Null
    $standardOut = $process.StandardOutput.ReadToEnd()
    $process.WaitForExit()
    
    # $standardOut should contain the results of "C:\script\script2.ps1"
    $standardOut
    
    0 讨论(0)
提交回复
热议问题