c# - How to properly set the path for executing a child powershell script? -
resolved: solution below use following join-path: [system.reflection.assembly]::loadfrom((join-path (pwd) "myassembly.dll")) | out-null
original question:
i have powershell scripts , assembly file placed follows:
d:\path1\script1.ps1 d:\path2\script2.ps1 d:\path2\myassembly.dll
within d:\path1\script1.ps1 need call d:\path2\script2.ps1. then, script2.ps1 in turn load c# assemblies placed in d:\path2 folder. under impression if either set-location or push-location working directory set happens within script2.ps1.
script1.ps1 looks this:
$otherpath = "d:\path2" set-location -path $otherpath push-location -path $otherpath .\script2.ps1
script2.ps1 looks this:
[system.reflection.assembly]::loadfrom("myassembly.dll") | out-null
however, when i'm in d:\path1, execute script1.ps1 follows, , filenotfound exception:
d:\path1>powershell .\script1.ps1 exception calling "loadfrom" "1" argument(s): "could not load file or assembly 'file:///d:\path1\myassembly.dll' or 1 of dependencies. system cannot find file specified." @ d:\path2\script2.ps1:1 char:1 + [system.reflection.assembly]::loadfrom("myassembly.dll") | out-null + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + categoryinfo : notspecified: (:) [], methodinvocationexception + fullyqualifiederrorid : filenotfoundexception
how set path correctly such when call script1.ps1 d:\path1 environment has d:\path2 set working directory calls assemblies in d:\path2?
using [system.reflection.assembly]::loadfrom((join-path (pwd) "myassembly.dll")) | out-null
work long there no change working directory inside script2.
the alternative use $myinvocation.mycommand.path
give full path of current script. use this:
[system.reflection.assembly]::loadfrom((join-path (split-path $myinvocation.mycommand.path) "myassembly.dll")) | out-null
Comments
Post a Comment