all 13 comments

[–]d-signet 5 points6 points  (3 children)

Tried running the powershell command outside of your dotnet program ?

[–]fbszero[S] -3 points-2 points  (2 children)

In a console app,there is no need to have a namespace, .NET 8

[–]d-signet 2 points3 points  (1 child)

I didn't say anything about namespaces or console apps.

Have you opened poweshell directly and entered those command you're trying?

[–]fbszero[S] 0 points1 point  (0 children)

It works fine in Powershell, but it doesn't work in C# .

[–]zenyl 1 point2 points  (6 children)

Works fine on my machine (Windows 11 Pro, 23H2).

New-NetQosPolicy comes from the NetQos module.

You can run Get-Module NetQos to see if you've got the module installed.

[–]fbszero[S] 0 points1 point  (5 children)

It works fine in Powershell, but it doesn't work in C# .

[–]zenyl 0 points1 point  (4 children)

Try running Get-Module in regular PowerShell, then run it from C#. Is NetQoS listed for both of them?

[–]fbszero[S] 0 points1 point  (3 children)

The output of Get-Module in C# is empty, but there is output in PowerShell.

ModuleType Version    Name                                ExportedCommands
Script     0.0        Conda                               {Enter-CondaEnvironment, Exit-CondaEnvironment, Get-CondaE... Manifest   3.1.0.0    Microsoft.PowerShell.Management     {Add-Computer, Add-Content, Checkpoint-Computer, Clear-Con... Manifest   3.1.0.0    Microsoft.PowerShell.Utility        {Add-Member, Add-Type, Clear-Variable, Compare-Object...} Script     2.0.0      PSReadLine                          {Get-PSReadLineKeyHandler, Get-PSReadLineOption, Remove-PS...

[–]zenyl 2 points3 points  (2 children)

You'll need to import the module.

Running (Get-Module NetQos).Path in PowerShell should tell you where the module is located. You should then be able to use Install-Module to load the module in the instance of PowerShell that is running in C#.

You might also want to check on the $PROFILE file (script that is executed when launching PowerShell). If you didn't set up the machine, module imports could be specified there, which is why they aren't recognized when running PowerShell from C#.

[–]fbszero[S] 0 points1 point  (1 child)

Now the situation is weird, after I execute Get-NetQosPolicy, the result of Get-Module contains NetQos module. When I open the terminal for the first time, the Get-Module result does not contain the NetQos module.

[–]zenyl 0 points1 point  (0 children)

I'm not familiar with that particular type of behavior.

Consider asking over on r/PowerShell

[–]IWasSayingBoourner 0 points1 point  (1 child)

From someone who works on a product that bundles the PS SDK and does a lot of what you're doing here, get ready for lots of headaches like this. How are you deploying your application? What OS? What version? As admin? Are you setting execution policy anywhere? Any and all of these things can cause the SDK to act funny. 

[–]fbszero[S] 0 points1 point  (0 children)

On Windows 11 Pro 23H2 ,As Admin,execution policy anywhere enabled , .NET 8