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!