2

I have a script with parameters:

param (
    [Parameter(Mandatory=$true)][string]$VaultName,
    [Parameter(Mandatory=$true)][string]$SecretName,
    [Parameter(Mandatory=$true)][bool]$AddToMono = $false
 )
...

In this script I want to include functions that I wrote in another ps1 file : common.ps1

I usually import this with

. .\common.ps1

but if I do that in the script I get:

The term '.\common.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.

How do I import common.ps1 in this script?

Thanks!

1
  • Give the full path of the common.ps1 in the dot source Commented Jul 19, 2017 at 3:17

1 Answer 1

7

The problem is that you are running the script from a different directory. PowerShell is looking for .\common.ps1 using the current directory, not the directory of the script. To get around this, use the built-in variable $PSScriptRoot, which contains the path of the current script. (I'm assuming you are using PowerShell v3.0 or later.)

common.ps1

function foo {
    return "from foo"
}

with_params.ps1

param (
    [Parameter(Mandatory=$true)][string]$VaultName,
    [Parameter(Mandatory=$true)][string]$SecretName,
    [Parameter(Mandatory=$true)][bool]$AddToMono = $false
 )

 . $PSScriptRoot\common.ps1

 Write-Output "Vault name is $VaultName"
 foo

I then executed this:

 PS> .\some_other_folder\with_params.ps1 -VaultName myVault -SecretName secretName -AddToMono $false

and got this output:

Vault name is myVault
from foo
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.