I don’t know what problem you are actually trying to solve but from a PowerShell console or a PowerShell script you can call another PowerShell script simply by providing its name - maybe including a relative or absolute path if it’s not in the working or same directory.
So assuming your script1.ps1 contains the following code snippet …
scripta.ps1 will call scriptb.ps1 and scriptb.ps1 passes back an array to scripta.ps1.
I need a way to pass back more than just a return code. Using $rc = (start-process -filepath powershell.exe -file scriptb.ps1).exitcode you can get the exitcode but I need more. Start-process allows you to examine exitcode on the return but not multiple elements.
I want to pass back an array INCLUDING the exitcode.
I got around it by having scriptb.ps1 create a text file with what I want in it and then scripta.ps1 reads the file.
Start-Process creates an object of the type System.Diagnostics.Process. One of its members is the property Exitcode. The default exit codes for a PowerShell process are 0 and 1. Where 0 means success and 1 stands for failure.
So assigning the output of Start-Process to a variable does not catch what the script outputs.
You could advice PowerShell to output a particular exitcode with the keyword exit followed by the number you want it to return to the calling process.
If you want to catch the output of your script you have to use -RedirectStandardOutput to write the output to a file.
Why are you actually starting a new PowerShell process from inside of your already running PowerShell process? That apporach actually consumes time and system ressources without providing any additional benefit.
You could use my suggestion from above and tweak it like this:
$result = .\currentPSfile_03.ps1
Now you can access each individual value with the so called dot notation by its name …
$result.file
or
$result.ostDecide
or
$result.rc
That will not work.
If you insist using a second PowerShell process that’s one of the ways. You could also use registry keys or eventlog entries if it’s about simple string values. If you want to pass complex objects you could use files you create with Export-CliXml.
Long story on having a main ps1 call others. I am running into TLS/SSL errors but I am using the ever popular posh statment = “[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls -bor [Net.SecurityProtocolType]::Tls11 -bor [Net.SecurityProtocolType]::Tls12” to no avail. Looked for windows events, nothing.
Unfortunately, it takes my main script about 90m to get to code where it dies with the TLS error. Script does bare metal image creaton, so it does take that long, which includes applying current MS maint to the image server and installing all the other stuff I want installed.
So, I wanted to isolate the code that is failing into its own ps1 and try calling it as a separate script to see if I can get around the TLS errors.
TerminatingError(Invoke-WebRequest): “The request was aborted: Could not create SSL/TLS secure channel.”
FAILURE
EXCEPTION: The request was aborted: Could not create SSL/TLS secure channel.