Powershell: get the return result from Powershell Script called from another PS Script

Background: I have a powershell script: script1 that accepts sourceDirectory and two destinations (name them dest1Directory and dest2Directory ).

SourceDirectory is structured as follows:

\ Source \ dest1 \ STUFF1

and

\ Source \ dest2 \ STUFF2

script1 calls another script: script2 , foreach STUFF (so script2 can be run 10 times, for example), providing script2 necessary destination parameter, which backs up all the contents of "STUFF", is replaced by dest1Directory and dest2Directory , and then copies all STUFF to the corresponding item destination.

script1 :

 foreach ($folder in $STUFF1) { & $script2 -stuffParameter $folder -destDrive $dest1Directory -backUpDrive $BackUpDirectory } 

The problem I am facing:

I call script1 from the visual studio website and would like script2 output all the backup directory paths that it creates, so I have links to them later. I tried this inside script2 :

 $returnRollBackObj = New-Object Object Add-Member -memberType NoteProperty -name "RollBackLocation" -value $folderobj -inputObject $returnRollBackObj return $returnRollBackObj 

But it does not seem to return objects, since they are calls in the subscript. I do not know how to return the undefined number of these objects from script1 so that I am at a loss. Can someone help me?

+6
source share
2 answers

Any unused output is returned to Powershell.

So something like

 return $returnRollBackObj 

equivalently

 $returnRollBackObj return 

So, since script2 returns these objects until these objects are written to script1, they will also be returned by script1. This way you don't have to do anything about returning an undefined number of objects

First, try running scripts from the console to see if you get the intended behavior.

+11
source

How to configure IIS?

Just think, if you enable eimpersonation, it may not have access to the second location of the script due to a double jump problem.

Something else that might help is transcription functionality. Add the start transcript at the top of the yu script / code, and you will get a dump of everything, lumps and results (incl. Errors and warnings).

Edit:

Just realized that you did not assign return values ​​from script2 to aything in script1.

You may need something like this:

$ ret = and $ script2 ...

Do something with $ ret ...

OR put it in the $ ret = @ () array

For the cycle ...

$ temp = and $ script2 ... $ ret = $ ret + $ temp

Then return $ ret after the for loop.

Matt

+2
source

Source: https://habr.com/ru/post/894586/


All Articles