2

I'm asking a question at the very first time here, so sorry for possible mistakes.

I have the following code:

public async Task TurnOnMobileHotspot()
{
    const string script = @"
            $connectionProfile = [Windows.Networking.Connectivity.NetworkInformation,Windows.Networking.Connectivity,ContentType=WindowsRuntime]::GetInternetConnectionProfile()
            $tetheringManager = [Windows.Networking.NetworkOperators.NetworkOperatorTetheringManager,Windows.Networking.NetworkOperators,ContentType=WindowsRuntime]::CreateFromConnectionProfile($connectionProfile)        
            return $tetheringManager";

    using var powershell = PowerShell.Create();
    var result = await powershell
            .AddScript("Set-ExecutionPolicy -Scope Process -ExecutionPolicy Unrestricted")
            .AddStatement()
            .AddScript(script)
            .InvokeAsync();
}

I'm using .NET 7.0 with the latest versions of required packages (Microsoft.PowerShell.SDK 7.3.4).

I expect that the script I execute will return something to C# like that (collection of 3 PsOjects):

ClientCount MaxClientCount TetheringOperationalState
----------- -------------- -------------------------
          0              8                       Off

But the actual result is null.

I tried to check if the code is executing under admin and the answer is yes.

Can you please help me to understand why is this happening? I believe there's something with calling code blocks from c# (maybe wrong Runspace. lack of modules or something like that), because Powershell console works correctly. And some simple commands like "dir" also work fine from the C# code.

Thank you for help!

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
  • You are not getting any errors or exception which means the code is working. You are not getting any nulls in middle of script which would lead to an exception. You are doing a "Set-ExecutionPolicy" which will not return any data. Check the variables and see if there is any data like : $tetheringManager | Format-List – jdweng Apr 30 '23 at 10:07
  • Why does this even need a Powershell script, why not just create the objects directly? `var profile = NetworkInformation.GetInternetConnectionProfile(); var manager = NetworkOperatorTetheringManager.CreateFromConnectionProfile(profile);` – Charlieface Apr 30 '23 at 10:33
  • Because it's just a part of the program. I'm not using UWP here... – Ivan Boganov Apr 30 '23 at 11:49
  • Well why not just link the correct DLL and be done? You don't need UWP to link one of the UWP libraries. – Charlieface Apr 30 '23 at 11:58
  • When you use `.AddScript()`, any errors that occur during execution of the PowerShell code do _not_ surface _as exceptions_ in your C# program; instead, you must examine the `.Streams.Error` collection (`.HadErrors` is a Boolean that indicates whether any errors occurred) - see [this answer](https://stackoverflow.com/a/69051991/45375) for details. – mklement0 Apr 30 '23 at 12:12
  • 2
    Also note that you can only load UWP assemblies in _Windows PowerShell_, so if you're targeting the PowerShell Core SDK this won't work. – mklement0 Apr 30 '23 at 12:13
  • Thank you, guys! I thought UWP libraries cannot be used separately – Ivan Boganov Apr 30 '23 at 14:33
  • @Charlieface can you please tell me which libraries I should use from UWP? I was trying to find something and found Microsoft.Toolkit.Uwp.Connectivity, but this one is not compatible with .net 7.0 (and 6.0 too). Thanks! – Ivan Boganov May 02 '23 at 18:01

0 Answers0