Reputation: 3183
I have a power shell script which has include script in it as below.
. "$PSScriptRoot\CheckPermissions.ps1"
When the script is invoked from c#, I am getting this error
{The term '\CheckPermissions.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.}
The script works fine when run from PS window.
Is not $PSScriptRoot variable available when script is run from c#?
Upvotes: 2
Views: 920
Reputation: 1
Try the below code
$scriptpath = "your second file path"
$psContent = @(Get-Content $scriptPath)
[string]$psCmd = $psContent -join "`n"
$secondscriptoutput = iex $psCmd
$secondscriptoutput #output returned by second file
Myinvocation will not work when ps scripts are invoked from c# code. Please mark this as answer if it works for you. Thanks!
Upvotes: 0
Reputation: 7000
You could try using the following to get the script directory:
$scriptDir = Split-Path $script:MyInvocation.MyCommand.Path
Note that the folder path returned will end with a backslash (\
).
Upvotes: 2