Home > OS >  Calling PS1 script from other PS1 script failing after invoked for the 1st time in same Terminal ins
Calling PS1 script from other PS1 script failing after invoked for the 1st time in same Terminal ins

Time:06-29

As per the image showing below, I have two PS1 script files.

  1. main.ps1 (from where we call the call.ps1 function)
  2. call.ps1

Now, check the below SS for reference of structure - Both are on same Folder.

enter image description here


Now, When we call the main.ps1 1st time in New Powershell window it's working fine. (see blow)

enter image description here


Now the real issue is, If we call the same function again 2nd time it's not working and giving an error. (see below)

enter image description here

Am I missing something or understanding the things incorrectly? - Or this is not the way to call the PS1 from another PS1 file?

Surprisingly, If I close the existing Terminal and open New Terminal it works fine for the 1st time..!!!

enter image description here

Anyone has any clue what's wrong we're doing??

CodePudding user response:

Someone might write this better then me.

You need to rename call.ps1 to call.psm1

Then change the code in main.ps1 to the following

Import-Module '.\call.psm1'

callingthefunction

Also in your main.ps1 you used './call.ps1' the slash direction is wrong. Look at my code above.

For more information about modules which is what you are creating. Please read.

https://docs.microsoft.com/en-us/powershell/scripting/developer/module/how-to-write-a-powershell-script-module?view=powershell-7.2

  • Related