calling function in external file

Ask your Windows 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.
Locked
User avatar
Domtar
Posts: 63
Joined: Sun Mar 03, 2013 12:42 pm

calling function in external file

Post by Domtar » Fri Sep 28, 2018 10:18 am

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!

User avatar
jvierra
Posts: 13725
Joined: Tue May 22, 2007 9:57 am
Contact:

Re: calling function in external file

Post by jvierra » Fri Sep 28, 2018 10:25 am

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: 63
Joined: Sun Mar 03, 2013 12:42 pm

Re: calling function in external file

Post by Domtar » Fri Sep 28, 2018 10:32 am

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: 63
Joined: Sun Mar 03, 2013 12:42 pm

Re: calling function in external file

Post by Domtar » Fri Sep 28, 2018 10:47 am

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?

User avatar
jvierra
Posts: 13725
Joined: Tue May 22, 2007 9:57 am
Contact:

Re: calling function in external file

Post by jvierra » Fri Sep 28, 2018 11:14 am

Your question is far too vague to answer.

You can create a file with many functions and call them as needed.

Locked