microsoft / winget-cli

WinGet is the Windows Package Manager. This project includes a CLI (Command Line Interface), PowerShell modules, and a COM (Component Object Model) API (Application Programming Interface).
https://learn.microsoft.com/windows/package-manager/
MIT License
23.37k stars 1.45k forks source link

When running Get-WinGetPackage in a Windows Service context returns error This cmdlet is not supported in Windows PowerShell. However, if not ran in Service context, runs perfectly. Why? #4838

Open jonygomes06 opened 2 months ago

jonygomes06 commented 2 months ago

Brief description of your issue

I have a rust program that can run as a normal console program or as a Windows Service and I need to run this command "C:\Windows\system32\WindowsPowerShell\v1.0\powershell.exe -ExecutionPolicy Bypass -Command { Get-WinGetPackage | ConvertTo-Json }".

When I run the rust program as a normal console program everything works perfectly and I get the data I need, as well as if I run that command directly in the console.

However, if I run that same rust program as a Windows Service it gives this error: Get-WinGetPackage: This cmdlet is not supported in Windows PowerShell.

It is using the same powershell path but I receive different responses. Why??

I have only tested this in a VM environment, i used Windows 11 Pro.

Thank you for your time.

Steps to reproduce

Create a rust program that executes this command "C:\Windows\system32\WindowsPowerShell\v1.0\powershell.exe -ExecutionPolicy Bypass -Command { Get-WinGetPackage | ConvertTo-Json }", and run it as a normal console program and as a Windows Service.

Expected behavior

The command should return a json containing information about the winget packages (Get-WinGetPackage)

Actual behavior

The command returns this error: Get-WinGetPackage: This cmdlet is not supported in Windows PowerShell.

Environment

Windows 11 Pro
Rust Program
Windows Service
Trenly commented 2 months ago

This is most likely because the service is attempting to run using the System context instead of a user context

stephengillie commented 1 month ago

There are frequent issues with the System context and Windows PowerShell. Many of these issues are not present on PowerShell 7.0 - is it possible to install the latter?

jonygomes06 commented 1 month ago

Thank you for both answers. It is not possible to install powershell 7.0 for this. I actually have in complement a system tray app so I think I will need to execute the command in there. Or is there any more solutions to this?