After connecting to the Linux (Ubuntu) with success, the script executes the command which npm
with an empty result. I think the npm is not in the script session PATH.
How can I load the user PATH on the script session? Another approach to execute the npm from a Windows console?
In the server with the same user, npm works fine using ssh. The echo $PATH
shows the npm path too.
# Define the SSH connection parameters
$sshParams = @{
Credential=$userName
HostName = $sshHostName
KeyFile = $sshIdentityFile
}
# Connect to the Linux machine using SSH
$sshSession = New-SSHSession @sshParams
$command_npm_install = "which npm;"
Write-Output $command_npm_install
$sshOutput = Invoke-SSHCommand -SessionId $sshSession.SessionId -Command $command_npm_install
Write-Output $sshOutput.Output
It appears that your script may not have the same environment variables, including the PATH variable, as your interactive SSH session. When you connect to a Linux server via SSH, your interactive shell session typically loads your user’s environment variables, including the PATH variable. However, when you run a command remotely using a script or automation tool, it may not load the same environment.
To load your user’s PATH variable when running a command remotely, you can source your user’s profile (e.g., .bashrc or .bash_profile) before running the command. Here’s how you can modify your script to do that:
# Define the SSH connection parameters
$sshParams = @{
Credential = $userName
HostName = $sshHostName
KeyFile = $sshIdentityFile
}
# Connect to the Linux machine using SSH
$sshSession = New-SSHSession @sshParams
# Source the user's profile to load environment variables
$command_source_profile = "source ~/.bashrc;"
$sshOutput = Invoke-SSHCommand -SessionId $sshSession.SessionId -Command $command_source_profile
# Check if sourcing the profile was successful
if ($sshOutput.ExitStatus -eq 0) {
# Now, you can run your npm command
$command_npm_install = "which npm;"
$sshOutput = Invoke-SSHCommand -SessionId $sshSession.SessionId -Command $command_npm_install
Write-Output $sshOutput.Output
} else {
Write-Output "Failed to source the user's profile."
}
# Close the SSH session
Remove-SSHSession -SessionId $sshSession.SessionId
In this modified script, we first source the user’s profile (e.g., ~/.bashrc) using source ~/.bashrc;. This command loads the user’s environment variables, including the PATH variable. After sourcing the profile, you can run your npm command or any other command that relies on the user’s environment.
Make sure to replace ~/.bashrc with the appropriate profile file if your user is using a different shell configuration file (e.g., ~/.bash_profile).
Additionally, always make sure to handle errors and edge cases in your script, such as checking the exit status of commands, as shown in the script above.
Try following $env:PSModulePath = $env:PSModulePath + “:/ModulePath” which is recommended on following : stefanstranger.github.io/2018/11/11/…
The problem is with the PATH on the remote Linux server, PSModulePath use the path on the local machine.
@jdweng,
$env:PSModulePath
is fundamentally unrelated to invocation of external programs. Only$env:PATH
matters for the latter.