calling function in external file

Ask your PowerShell-related questions, including questions on cmdlet development!
Forum rules
Do not post any licensing information in this forum.

Any code longer than three lines should be added as code using the 'Select Code' dropdown menu or attached as a file.
This topic is 5 years and 5 months old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked
User avatar
Domtar
Posts: 133
Last visit: Mon Mar 11, 2024 5:38 am
Has voted: 2 times

calling function in external file

Post by Domtar »

hi all,

aside from dot sourcing, is there some oter way to call a function inside an external file?

I know about . .\myfile.ps1, I also know about modules, but in my case, both these options aren't doing what I want.

The issue is that I need to force a script to be launched in the 64-bit system from a SCCM 32-bit client. I want to launch like this;

start-process "C:\Windows\sysnative\WindowsPowerShell\v1.0\powershell.exe" -arg...

using dot sourcing won't enforce the bitness.

thanks!
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: calling function in external file

Post by jvierra »

The version of PowerShell enforces the bitness. This has nothing to do with the script or function calls. Functions always run in the architecture calling them.
Only dot sourcing will work

Dot sourcing is not what you posted. That is just loafing from the current folder. In a call like this you should always use the full path

Dot sourcing only a file will never execute any function.
User avatar
Domtar
Posts: 133
Last visit: Mon Mar 11, 2024 5:38 am
Has voted: 2 times

Re: calling function in external file

Post by Domtar »

Well, looks like I badly explained my situation.

In SCCM, when you want to deploy something to a bunch of computers, you create a "package" that is sent to those computers. The package has a command line to execute something.

On the computers, there is an agent that receives those instructions and proceed with the required installation. Thanks to Microsoft, the SCCM client in question is 32-bit, so our scripts are launched in that bitness. Now, if I use the command line to start the process of PowerShell.exe from c:\windows\sysnative..., Windows will comply and launch the required script in 64-bit on a 64-bit computer.

We have an issue with this because some scripts require to be executed in the 64-bit system even when launched from the 32-bit SCCM client. My way of going around it is to have the main wrapper script call an external script by enforcing the 64-bit system.

I hope this makes more sense now.

thanks!
User avatar
Domtar
Posts: 133
Last visit: Mon Mar 11, 2024 5:38 am
Has voted: 2 times

Re: calling function in external file

Post by Domtar »

In other words;

I can easily start another script each time with that sysnative thing but that means I would have to create a script for each function and that is what I want to avoid.

any hint?
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: calling function in external file

Post by jvierra »

Your question is far too vague to answer.

You can create a file with many functions and call them as needed.
This topic is 5 years and 5 months old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked