powershell - Pass arg through new shell within script -


i'm trying call new shell due memory leak library. when call shell, need pass arg (the real code pass 2 args). after block of code has executed in new shell, needs return value. wrote test code reproduce error:

function getlastname {     param ($firstname)      $lastname = powershell -firstname $firstname {         param ([string]$firstname)         $lastname = ''         if ($firstname = 'john')         {             $lastname = 'doe'             write-host "hello $firstname, last name registered $lastname"         }         write-host "last name not found"         write-output $lastname     }     write-output $lastname }  function main {     $firstname = 'john'      $lastname = getlastname $firstname      write-host "your name $firstname $lastname" }  main 

the error get...

powershell : -firstname : term '-firstname' not recognized name of cmdlet, function, script file, or operable @ c:\scripts\tests\test1.ps1:5 char:15 +         $lastname = powershell -firstname $firstname { +                     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~     + categoryinfo          : notspecified: (-firstname : th...e, or operable :string) [], remoteexception     + fullyqualifiederrorid : nativecommanderror  program. check spelling of name, or if path included, verify path correct , try again. @ line:1 char:1 + -firstname john -encodedcommand dqakaakacqajafaayqbyageabqagacgawwbzahqacgbpag4a ... + ~~~~~~~~~~     + categoryinfo          : objectnotfound: (-firstname:string) [], commandnotfoundexception     + fullyqualifiederrorid : commandnotfoundexception

can me how this?

the syntax calling powershell.exe execute scriptblock within powershell bit different:

powershell.exe -command { scriptblock content here } -args "arguments","go","here" 

so in script should be:

$lastname = powershell -command {     param ([string]$firstname)     $lastname = ''     if ($firstname = 'john')     {         $lastname = 'doe'         write-host "hello $firstname, last name registered $lastname"     } else {         write-host "last name not found"     }     write-output $lastname } -args $firstname 

Comments