简体   繁体   English

Powershell:从其他PS脚本中调用的Powershell脚本获取返回结果

[英]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 ). 背景:我有一个powershell脚本: script1 ,它接受一个sourceDirectory和两个目的地(称为dest1Directorydest2Directory )。

The sourceDirectory is structured like this: sourceDirectory的结构如下:

\\Source\\dest1\\STUFF1 \\来源\\ DEST1 \\ STUFF1

and

\\Source\\dest2\\STUFF2 \\来源\\ 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调用另一个脚本: script2foreach STUFF( script2 ,脚本script2可以运行10次),为script2提供必要的目标参数,这将创建“STUFF”正在替换dest1Directorydest2Directory的所有内容的备份,然后复制所有STUFF到相应的目的地。

Script1 : 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. 我正在从visual studio网站调用script1 ,并希望script2输出它创建的所有备份目录路径,所以我稍后会引用它们。 I have tried this inside of script2 : 我在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. 我不知道如何从script1返回未定义数量的这些对象,所以我很茫然。 Can someone help me out? 有人可以帮我吗?

Any uncaptured output is returned in Powershell. 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. 因此,由于script2正在返回这些对象,只要这些对象没有在script1中捕获,它们也将由script1返回。 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. 尝试首先从控制台运行脚本,以查看是否获得了预期的行为。

How's your IIS setup? 您的IIS设置如何?

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. 只是想一想,如果你已经进行了eimpersonation,那么由于双跃点问题,它可能无法访问第二个脚本的位置。

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). 在yu脚本/代码的顶部添加一个start-transcript,你将得到一切,命令和输出的转储(包括错误和警告)。

Edit: 编辑:

Just realised your not assigning the returned values from script2 to aaything in script1. 刚刚意识到你没有将脚本2中的返回值分配给script1中的aaything。

You may need something like this: 你可能需要这样的东西:

$ret = & $script2 ... $ ret =&$ script2 ...

Do something with $ret... 用$ ret做点什么......

OR put it in an array $ret = @() 或者把它放在一个数组中$ ret = @()

For loop... 对于循环......

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

Then return $ret after the for loop. 然后在for循环后返回$ ret。

Matt 马特

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM