Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Powershell: Get return result from Powershell Script called from Within other PS Script

Background: I have a powershell script:script1 that takes in a sourceDirectory and two destinations (call them dest1Directory and dest2Directory).

The sourceDirectory is structured like this:

\Source\dest1\STUFF1

and

\Source\dest2\STUFF2

script1 calls another script:script2, foreach STUFF (so script2 may be run 10 times for example), providing script2 with the necessary destination parameter, which creates backups of all of the contents "STUFF" is replacing on dest1Directory and dest2Directory, and then copies all of STUFF to the corresponding destination.

Script1:

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

The issue I'm having is:

I'm calling script1 from a visual studio website and would like script2 to output all of the backup directory paths it creates so I have references to them for later. I have tried this inside of script2:

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

But it doesn't seem to return the objects since they are subscript calls. I don't know how to return an undefined number of these objects from script1 so I'm at a loss. Can someone help me out?

like image 487
Evan Layman Avatar asked Aug 08 '11 20:08

Evan Layman


People also ask

How do I call a PowerShell script from another PowerShell script?

You don't need Start-Process. PowerShell scripts can run other scripts. Just put the command that runs the second script as a command in the first script (the same way as you would type it on the PowerShell command line).

How do I get the return value of a PowerShell script?

The return keyword exits a function, script, or script block. It can be used to exit a scope at a specific point, to return a value, or to indicate that the end of the scope has been reached.

How do I run a PowerShell script from a PowerShell script?

To open the PowerShell console, click on the Start button (or search button), type powershell, and click Run as Administrator. To run a script in the PowerShell console, you can either: Use the full path to script, like: C:\TEMP\MyNotepadScript. ps1.

What is $_ called in PowerShell?

The “$_” is said to be the pipeline variable in PowerShell. The “$_” variable is an alias to PowerShell's automatic variable named “$PSItem“. It has multiple use cases such as filtering an item or referring to any specific object.


2 Answers

Any uncaptured output is returned in Powershell.

So something like

return $returnRollBackObj

is equivalent to

$returnRollBackObj
return

So, since script2 is returning these objects, as long as these objects are not captured in script1, they will be returned by script1 as well. So you not have to do anything about "returning undefined number of objects"

Try running the scripts from console first, to see if you are getting the intended behaviour.

like image 167
manojlds Avatar answered Oct 13 '22 13:10

manojlds


How's your IIS setup?

Just thinking , if you hve eimpersonation on, tha it might not have access to the second script's location due to the double hop issue.

Something else that may help is the transcript functionality. Add a start-transcript at the top of yu script/code and you'll get a dump of everything, comands and output (inc. Errors and warnings).

Edit:

Just realised your not assigning the returned values from script2 to aaything in script1.

You may need something like this:

$ret = & $script2 ...

Do something with $ret...

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

For loop...

$temp = & $script2 ... $ret = $ret + $temp

Then return $ret after the for loop.

Matt

like image 21
Matt Avatar answered Oct 13 '22 11:10

Matt