Exact file search does not work

I should follow this question, but I can't get it to work.

(For testing) I have a powershell module with two scripts: variables.ps1 and function.ps1 and the manifest mymodule.psd1 (these files are in the same directory)

This is the contents of the .ps1 variables:

$a = 1;
$b = 2;

This is the contents of the .ps1 function

. .\variables.ps1
function myfunction
{
    write-host $a
    write-host $b
}

When importing a module and calling myfunction. This is the result:

C:\> Import-Module .\mymodule.psd1
C:\> myfunction
. : The term '.\variables.ps1' is not recognized as the name of a cmdlet, function, script file, or operable
program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again.
At C:\Users\Jake\mymodule\function.ps.ps1:8 char:4
+     . .\variables.ps1
+       ~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : ObjectNotFound: (.\variables.ps1:String) [], ParentContainsErrorRecordException
    + FullyQualifiedErrorId : CommandNotFoundException

Why is this not working?

+4
source share
1 answer

When you use relative paths in scripts, they refer to the callers $PWD— the current directory in which you are located.

, script , $PSScriptRoot

. (Join-Path $PSScriptRoot variables.ps1)

$PSScriptRoot PowerShell 3.0, PowerShell 2.0 :

if(-not (Get-Variable -Name 'PSScriptRoot' -Scope 'Script')) {
    $Script:PSScriptRoot = Split-Path -Path $MyInvocation.MyCommand.Definition -Parent
}
. (Join-Path $PSScriptRoot variables.ps1)
+9

Source: https://habr.com/ru/post/1614050/


All Articles