Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Powershell scripts invoked by C# application

I have this powershell script (test1.ps1) calling another powershell script(test2.ps1) to do the job.

Both scriptfiles are in the same folder

test1.ps1

echo "from test1.ps1"

.\test2.ps1

test2.ps1

echo "from test2.ps1"

When I invoke test1.ps1 in C# by creating runspace, adding commands to pipeline and invoking it, I get an error message saying "The term '.\test2.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."

like image 273
kannan Avatar asked Apr 25 '26 17:04

kannan


1 Answers

Both the scripts might be in the same folder, but .\test2.ps1 will look for test2.ps1 in the same folder as the calling application, which is the C# app.

Have this in test.ps1:

$scriptDir = Split-Path -parent $MyInvocation.MyCommand.Path
.$scriptdir\test2.ps1
like image 191
manojlds Avatar answered Apr 27 '26 08:04

manojlds



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!