powershell - Dot sourcing a file doesn't work -
im following this question can't seem working.
(for sake of testing) have powershell module 2 scripts: variables.ps1 , function.ps1 , manifest mymodule.psd1 (these files in same directory)
this content of variables.ps1:
$a = 1; $b = 2;
this content of function.ps1
. .\variables.ps1 function myfunction { write-host $a write-host $b }
when import module , call myfunction. output:
c:\> import-module .\mymodule.psd1 c:\> myfunction . : term '.\variables.ps1' not recognized name of cmdlet, function, script file, or operable program. check spelling of name, or if path included, verify path correct , try again. @ c:\users\jake\mymodule\function.ps.ps1:8 char:4 + . .\variables.ps1 + ~~~~~~~~~~~~~~~~~~~~~ + categoryinfo : objectnotfound: (.\variables.ps1:string) [], parentcontainserrorrecordexception + fullyqualifiederrorid : commandnotfoundexception
why doesn't work?
when use relative paths in scripts, they're relative callers $pwd
- current directory you're in.
to make relative directory current script located on file system, can use automatic variable $psscriptroot
. (join-path $psscriptroot variables.ps1)
the $psscriptroot
variable introduced in powershell version 3.0, powershell 2.0 can emulate with:
if(-not (get-variable -name 'psscriptroot' -scope 'script')) { $script:psscriptroot = split-path -path $myinvocation.mycommand.definition -parent } . (join-path $psscriptroot variables.ps1)
Comments
Post a Comment