powershell - Run function with Start-process within script -
i trying initialize ton of powershell windows complete scan quickly. each window needs different input , have array variable giving input. start-process function seems work, gives me unknown function error if use "
, gives me absolutely no response when use '
foreach($search in $partlist){ start-process powershell.exe -argumentlist '-nologo -noprofile -executionpolicy bypass -command .\get-data -search $search -department $pndept -accuracy $accuracy; pause' start-sleep -s 5 }
i not understand why unknown function error, believe no response because variables not global, i've declared variables global , still no luck. maybe i'm missing something. thanks.
to add ansgar wiechars' answer, there following differentiator:
- jobs start new processes under hood give lot of framework running multiple instances, queuing , getting data back. however, starting new process each job expensive in windows.
- starting new powershell window going @ least slow job overhead of visual elements, , difficulty of getting results calling process
- runspaces going execute lot faster because run threads within same process, cheaper instantiate because use existing process block (this windows structure, not ps one).
a great article here: https://learn-powershell.net/2012/05/13/using-background-runspaces-instead-of-psjobs-for-better-performance/ aware of going far: https://newsqlblog.com/2012/05/22/concurrency-in-powershell-multi-threading-with-runspaces/
to answer point global space, not global desktop session of computer sitting at. global powershell process working in. example: create environment variable following:
[system.environment]::setenvironmentvariable("myvar", 22, "user")
you spawn new powershell window , check this:
$env:myvar
you delete this:
remove-item env:\myvar
you spawn new powershell window , environment variable still exists. further experimentation show new global scope spawned each new process.
so! why "unknown function"? because expect aspects of environment match calling process, not hold. in case:
- your command not in path new process spawned at
- your variable values not assigned, because you're passing them in single-quotes , new process doesn't have them in scope
use runspaces, or refer this: powershell executing function within script block using start-process weird things double quotes
Comments
Post a Comment