Posh-SSH module only works when user is logged in

Josh Fox 0 Reputation points
2024-03-13T14:51:15.6366667+00:00

I use PoSH-SSH for a lot of automations. The problem is that the module is unavailable when scripts are run unless I am logged in and have a PowerShell session open as that user. Here is the behavior:

  1. If I open an interactive PowerShell session as that user, the module loads automatically. I do not need to import it.
  2. If I do get-module -listAvailable, it shows up with the locations where it is installed: C:\Windows\system32\WindowsPowerShell\v1.0\Modules, and C:\user\Documents\WindowsPowerShell\Modules
  3. If the script runs as the user, even if it performs import-module, it is unable to access the module unless I am logged in with a PowerShell session open as that user.

I am probably just missing something obvious, but any help would be greatly appreciated. Thanks!

Windows for business | Windows Server | User experience | PowerShell
{count} votes

1 answer

Sort by: Most helpful
  1. Rich Matheisen 47,901 Reputation points
    2024-03-13T15:10:45.8966667+00:00

    You don't say how the script is launched so I'm assuming(!) that it's run as a scheduled task. If that's the case the creation of the session is asking you to accept the certificate. A scheduled task has no UI so the script stalls.

    Try adding -AcceptKey and -Force as switches when you create the new session.


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.