powershell - get return from another power shell script -
i have ps script invoke-expression
on other scripts on same computer. here code:
$webmemory = "c:\memory_script\webmemory_script.ps1" $intmemory = "c:\memory_script\intmemory_script.ps1" $hungweb = "c:\scripts\hungweb_script.ps1" $hungint = "c:\scripts\hungint_script.ps1" $intmemoryresult = @() $webmemoryresult = @() $hungwebresult = @() $hungintresult = @() $date = get-date $shortdate = (get-date -format ddmmyyy.hhmm) $filepath = "c:\scripts\memory&hungresults\results" + $shortdate + ".txt" $break = "`r`n" out-file -filepath $filepath -inputobject $date -force -encoding ascii -width 50 out-file -filepath $filepath -append -inputobject $break -encoding ascii -width 50 $intmemoryresult += invoke-expression $intmemory $webmemoryresult += invoke-expression $webmemory $hungwebresult += invoke-expression $hungweb $hungintresult += invoke-expression $hungint write-host $webmemoryresult out-file -filepath -append -inputobject $intmemoryresult -encoding ascii -width 200 out-file -filepath -append -inputobject $break -encoding ascii -width 200 out-file -filepath -append -inputobject $webmemoryresult -encoding ascii -width 200 out-file -filepath -append -inputobject $break -encoding ascii -width 200 out-file -filepath -append -inputobject $hungintresult -encoding ascii -width 200 out-file -filepath -append -inputobject $break -encoding ascii -width 200 out-file -filepath -append -inputobject $hungwebresult -encoding ascii -width 200 out-file -filepath -append -inputobject $break -encoding ascii -width 200
code in 1 of scripts being called (the other 3 have similar functions)
$serverlist = @("list of servers") $w3wpmemory = @() $w3wpmemory += "---------- w3wp memory consumption ----------" $w3wpresult = @() $toberecycled =@() $toberecycled += "******************** int servers below need recycled (hung) ********************" + "`r`n" $date = get-date $shortdate = (get-date -format ddmmyyy.hhmm) $filepath = "c:\scripts\hungintresults\hungserverresults" + $shortdate + ".txt" $break = "`r`n" out-file -filepath $filepath -inputobject $date -force -encoding ascii -width 50 out-file -filepath $filepath -append -inputobject $break -encoding ascii -width 50 foreach($server in $serverlist) { $w3wpresult += (get-wmiobject win32_process -filter "commandline '%serviceoptimization%'" -computername $server).privatepagecount / 1gb $w3wpmemory += $server + ":" + $w3wpresult + "`n" } $i = 0 foreach($server in $serverlist) { $w3wpresult2 = (get-wmiobject win32_process -filter "commandline '%serviceoptimization%'" -computername $server).privatepagecount / 1gb write-host $w3wpresult2 " , " ($w3wpresult | select-object -index $i) if($w3wpresult -contains ($w3wpresult2)) { $toberecycled += $server + "`r`n" } $i = $i + 1 } $toberecycled += "*******************************************************************************" $toberecycled += "`r`n" write-host $toberecycled out-file -filepath $filepath -append -inputobject $toberecycled -encoding ascii -width 100 return $toberecycled
when script runs, see output of execution of other scripts. results "invoke-expression" command returning null, why this?
write-host
writes directly host display. if want capture output use write-output
instead or put variable on line because default output "output" stream:
$toberecycled
btw when execute powershell script powershell script, child script execute synchronously (unless using jobs).
Comments
Post a Comment