Forum Discussion
Powershell issues running script from Windows Service 01/02 - powershell modules not available
Hi,
I am facing problems executing some powershell scripts from a Windows Service.
Background
The Windows Service (started with an AD user) will execute a command through Process Class.
It will launch a command like in the folder that we specify:
- powershell .\<script name>.ps1 -param1 'param1' -param2 'param2'...
Issue
Some of the powershell modules are not "available" to be imported, although they are available in the folder "C:\Program Files\WindowsPowerShell\Modules". E.g.:
- Import-Module -Name Az
- Import-Module -Name SqlServer
If we run the same command from a Command Prompt the script executes correctly!
Workaround
Run the following command:
- robocopy "C:\Program Files\WindowsPowerShell\Modules" %USERPROFILE%\Documents\WindowsPowerShell\Modules /mir
Thanks in advance,
Tiago R.
- Solved!
The problem was that powershell was executing in 32-bits thru the service and in 64-bits when launched from the command prompt.
I copied the code in described in https://stackoverflow.com/questions/19055924/how-to-launch-64-bit-powershell-from-32-bit-cmd-exe to the beginning of the script and now works as expected.
- farismalaebSteel Contributor
Is this user running this service is an admin, maybe this user doesn't have the proper permission to load the module from that directory,
Try to include the Import-Module XXXXX in your script and execute it.
what is the output? are you getting any errors when Import-module is executed?
- tarenteCopper ContributorThe user running the service is an administrator in the server.
If I connect to the server thru Remote Desktop and execute the command in a Command Prompt the script executes successfully.
I think the problem is that executing the command from the service's scope the complete profile of the user is not loaded. Is there a way to force loading the full profile of the user before running the script?
Thanks.- tarenteCopper ContributorSolved!
The problem was that powershell was executing in 32-bits thru the service and in 64-bits when launched from the command prompt.
I copied the code in described in https://stackoverflow.com/questions/19055924/how-to-launch-64-bit-powershell-from-32-bit-cmd-exe to the beginning of the script and now works as expected.