Forum Discussion

tarente's avatar
tarente
Copper Contributor
Jan 22, 2021

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.

 

 

 
 
 
 
 
 
 
 
 
  • farismalaeb's avatar
    farismalaeb
    Steel Contributor

    tarente 

    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?

    • tarente's avatar
      tarente
      Copper Contributor
      The 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.

Resources