You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
"Cannot load PSReadline module" when powershell is run with a service/daemon account #3011
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
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.
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.
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?
/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.
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
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?
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.
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?
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.
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.
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
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. :-)
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)
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
Activity
0byt3 commentedon Jan 17, 2017
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.
0byt3 commentedon Jan 17, 2017
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 cactiI get the following error message:
mirichmo commentedon Jan 18, 2017
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?
0byt3 commentedon Jan 18, 2017
$env:PSMODULEPATH is the following
chmod -R 775 /opt/microsoftto be sure that 'other' has read and execute permissions.[-]Cannot run powershell scripts as service/daemon accounts[/-][+]"Cannot load PSReadline module" when powershell is run with a service/daemon account[/+]SteveL-MSFT commentedon Jan 19, 2017
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?
0byt3 commentedon Jan 19, 2017
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 apacheYou could also enter the context of that user with
su apacheThen 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.
SteveL-MSFT commentedon Jan 25, 2017
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?
0byt3 commentedon Jan 25, 2017
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.
SteveL-MSFT commentedon Jan 25, 2017
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.
0byt3 commentedon Jan 30, 2017
Thanks for all the help so far.
SteveL-MSFT commentedon Feb 7, 2017
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 havefor 6.0.0rkeithhill commentedon Feb 7, 2017
Perhaps a workaround would be to not have PSReadline load if
-NonInteractiveis 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. :-)5 remaining items
SteveL-MSFT commentedon Feb 10, 2017
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)
SteveL-MSFT commentedon Feb 11, 2017
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.
0byt3 commentedon Feb 26, 2017
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?
SteveL-MSFT commentedon Feb 27, 2017
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.
SteveL-MSFT commentedon Mar 3, 2017
byte0 (@0byt3) PR submitted, you can build private off my branch if you want to try it
Allow profile directory creation failures for Service Account scenari…