Skip to content

"Cannot load PSReadline module" when powershell is run with a service/daemon account #3011

Closed

Description

@0byt3

Steps to reproduce

create ./local/share/powershell/Modules directory in user's home to avoid errors from that
(also need to give it permissions to the account)
sudo -i -u cacti powershell -command Write-Host "hello"

Expected behavior

Output the text "hello"

Actual behavior

Write-Host : The term 'Write-Host' is not recognized as the name of a cmdlet,
function, script file, or operable program. Check the spelling of the name, or
if a path was included, verify that the path is correct and try again.
At line:1 char:1
+ Write-Host hello
+ ~~~~~~~~~~
    + CategoryInfo          : ObjectNotFound: (Write-Host:String) [], CommandN
   otFoundException
    + FullyQualifiedErrorId : CommandNotFoundException

Environment data

Name                           Value
----                           -----
PSVersion                      6.0.0-alpha
PSEdition                      Core
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0...}
BuildVersion                   3.0.0.0
GitCommitId                    v6.0.0-alpha.14
CLRVersion
WSManStackVersion              3.0
PSRemotingProtocolVersion      2.3
SerializationVersion           1.1.0.1

Activity

  1. 0byt3 commented on Jan 17, 2017

    @0byt3
    Author

    I am running a Cacti server and would like to use powershell scripts seeing as powershell is pretty awesome, however I have found that the scripts fail, because there seems to be some environment stuff preventing them from working.

  2. 0byt3 commented on Jan 17, 2017

    @0byt3
    Author

    I edited /etc/passwd and changed the default shell from /sbin/nologin to /usr/bin/powershell . When I enter the context of that user:
    sudo -i -u cacti
    I get the following error message:

    Cannot load PSReadline module. Console is running without PSReadline.

  3. mirichmo commented on Jan 18, 2017

    @mirichmo
    Member

    This seems like a module import issue rather a "can't run scripts issue." PowerShell resolves (and auto-loads) PS modules based on the PSModulePath. Within PowerShell, you can view it with this command: $env:psmodulepath. The default installation will set up the core modules (Commands.Management, Commands.Utility, PSReadline, etc) so that they auto-load when PowerShell starts.

    Can you confirm that your alternate account (service/daemon) has access to the directories listed on the PSModulePath?

  4. 0byt3 commented on Jan 18, 2017

    @0byt3
    Author

    $env:PSMODULEPATH is the following

    /usr/share/cacti/.local/share/powershell/Modules:/usr/local/share/powershell/Modules:/opt/microsoft/powershell/6.0.0-alpha.14/Modules

    • /usr/local/share/Modules doesn't exist.
    • /usr/share/cacti/.local/share/powershell/Modules didn't exist to start (makes sense that it wouldn't). This was an issue at first that kept powershell from running, so I created the folder. Now powershell loads, but I get the PSReadline error
    • /opt/microsoft/powershell/6.0.0-alpha.14/Modules has 775 root:root permissions on it and it's files. I even chmod -R 775 /opt/microsoft to be sure that 'other' has read and execute permissions.
  5. changed the title [-]Cannot run powershell scripts as service/daemon accounts[/-] [+]"Cannot load PSReadline module" when powershell is run with a service/daemon account[/+] on Jan 18, 2017
  6. SteveL-MSFT commented on Jan 19, 2017

    @SteveL-MSFT
    Member

    PSReadline is just another module, but one that PowerShell console tries to load for the interactive experience. The issue as Mike Richmond (@mirichmo) pointed out is that modules aren't being found. I tried to repro this locally but not familiar with Cacti. I installed it via apt-get on my Ubuntu 16.04 VM, but it didn't create the cacti user account. byte0 (@0byt3) how did you create that user?

  7. 0byt3 commented on Jan 19, 2017

    @0byt3
    Author

    You can do this with any service/daemon account. Do it with the apache account. Edit the /etc/passwd file, so that apache's shell is /usr/bin/bash instead of /sbin/nologin . Then enter the apache user context with
    sudo -i -u apache
    You could also enter the context of that user with
    su apache
    Then load powershell and you'll find that PSReadline cannot be loaded. If I create a normal user with useradd I can load powershell with no errors in the context of that user.

    I don't know how to make it load or what to do to determine why it isn't loading. The environment variable $env:PSMODULEPATH exists and includes /opt/microsoft/powershell/6.0.0-alpha.14/Modules in the context of the user, yet it doesn't load.
    I'm using CentOS 7 (3.10.0-327.36.2.el7.x86_64) if that helps you suggest troubleshooting steps.

  8. SteveL-MSFT commented on Jan 25, 2017

    @SteveL-MSFT
    Member

    byte0 (@0byt3) I wasn't able to get your exact repro, but I did hit an issue trying to start powershell under the sshd account. I made a change in my fork: https://github.com/stevel-msft/powershell, can you try building powershell from my fork and see if that resolves your issue?

  9. 0byt3 commented on Jan 25, 2017

    @0byt3
    Author

    Sorry, that didn't help me. I think I'll have to make a video to show how I come across the problem. Since no one seems to be able to re-create it, yet I'm doing this with fresh installs of Ubuntu 16.04.1 and CentOS 7.

  10. SteveL-MSFT commented on Jan 25, 2017

    @SteveL-MSFT
    Member

    I think I have a closer repro as I can now start powershell with the sshd account, but the psmodulepath is not correct, so write-host isn't found. I'll continue to work on this.

  11. 0byt3 commented on Jan 30, 2017

    @0byt3
    Author

    Thanks for all the help so far.

  12. SteveL-MSFT commented on Feb 7, 2017

    @SteveL-MSFT
    Member

    I looked into this and the problem appears to be that the analysiscache tries to write to disk the cache which defaults to a folder under the user home directory. Service accounts don't have a home directory and thus this disk activity fails and throws an exception and then everything else fails to work correctly as PowerShell at this point has no idea how to find commands/modules. Currently, this doesn't look like a simple fix. Will get someone to look at this later, but at this time I don't see this as must have for 6.0.0

  13. rkeithhill commented on Feb 7, 2017

    @rkeithhill
    Collaborator

    Perhaps a workaround would be to not have PSReadline load if -NonInteractive is specified. In that case, someone could configure the service/daemon to run without PSReadline being loaded. If there are other ways to prevent PSReadline from opening in this scenario, that'd be cool too. :-)

  14. 5 remaining items

  15. SteveL-MSFT commented on Feb 10, 2017

    @SteveL-MSFT
    Member

    Jason Shirk (@lzybkr) tried pointing XDG_CACHE_HOME to /dev/null and powershell starts, but nothing works as the type initializer for analysiscache throws an exception (probably when it tries to create the folder for the cache)

  16. SteveL-MSFT commented on Feb 11, 2017

    @SteveL-MSFT
    Member

    Been thinking about this and see no reason PowerShell can't just start if it can't create folders in the home directory. I'll make a change.

  17. 0byt3 commented on Feb 26, 2017

    @0byt3
    Author

    Making sure I have the process down correctly to use powershell from your fork:
    I created a folder /opt/Microsoft
    cd /opt/Microsoft
    git clone --recurse https://github.com/stevel-msft/powershell
    cd powershell
    ./build.sh
    ln -s /opt/Microsoft/powershell/bin/powershell /usr/bin/powershell

    Did I figure this out correctly?

  18. SteveL-MSFT commented on Feb 27, 2017

    @SteveL-MSFT
    Member

    byte0 (@0byt3) didn't complete the work yet, but that is how you would do it once I get a fix. I'll work on a fix this week.

  19. SteveL-MSFT commented on Mar 3, 2017

    @SteveL-MSFT
    Member

    byte0 (@0byt3) PR submitted, you can build private off my branch if you want to try it

  20. added a commit that references this issue on Aug 6, 2026
    7e6209d
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

Resolution-FixedWG-Engine

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions