0

In a .net8 unit tests, I run a powershell command:

protected Collection<PSObject>? GetDisk(int index)
{
    var runspace = RunspaceFactory.CreateRunspace();
    runspace.Open();
    var pipeline = runspace.CreatePipeline();

    var dir = new Command("Get-Disk");
    dir.Parameters.Add("-Number", index);
    pipeline.Commands.Add(dir);
    pipeline.Commands.Add(new Command("Get-Partition"));
    pipeline.Commands.Add(new Command("Get-Volume"));

    return pipeline.Invoke();
}

And I have set Set-ExecutionPolicy Unrestricted as admin.

Despite this, I still get the following error, and I can't figure out why:

Message:  Test method DiskManagerTests.DeviceTests.GetDiskTest threw exception: System.Management.Automation.CommandNotFoundException: The 'Get-Disk' command was found in the module 'Storage', but the module could not be loaded due to the following error: [File C:\WINDOWS\system32\WindowsPowerShell\v1.0\Modules\Storage\StorageScripts.psm1 cannot be loaded because running scripts is disabled on this system. For more information, see about_Execution_Policies at https://go.microsoft.com/fwlink/?LinkID=135170.] For more information, run 'Import-Module Storage'.

---> System.Management.Automation.CmdletInvocationException: File C:\WINDOWS\system32\WindowsPowerShell\v1.0\Modules\Storage\StorageScripts.psm1 cannot be loaded because running scripts is disabled on this system. For more information, see about_Execution_Policies at https://go.microsoft.com/fwlink/?LinkID=135170.

---> System.Management.Automation.PSSecurityException: File C:\WINDOWS\system32\WindowsPowerShell\v1.0\Modules\Storage\StorageScripts.psm1 cannot be loaded because running scripts is disabled on this system. For more information, see about_Execution_Policies at https://go.microsoft.com/fwlink/?LinkID=135170.

---> System.UnauthorizedAccessException: File C:\WINDOWS\system32\WindowsPowerShell\v1.0\Modules\Storage\StorageScripts.psm1 cannot be loaded because running scripts is disabled on this system. For more information, see about_Execution_Policies at https://go.microsoft.com/fwlink/?LinkID=135170.

Stack Trace:  AuthorizationManager.ShouldRunInternal(CommandInfo commandInfo, CommandOrigin origin, PSHost host) ModuleCmdletBase.GetScriptInfoForFile(String fileName, String& scriptName, Boolean checkExecutionPolicy)

(...)

I am running VS2022 as a user (not as administrator). I am using the package Microsoft.Powershell.SDK 7.4 in the library I am testing.

2
  • Why are you even executing a PowerShell command at all? There's a C# way to list disks. stackoverflow.com/questions/781905/… Commented Nov 22, 2023 at 19:09
  • @gunr2171 those are not equivalent; with the function you mentionned I can't have the relation between the drive index and the rest; and I am starting with the drive index. Commented Nov 22, 2023 at 19:18

1 Answer 1

1

In order to change the execution policy for this command I had to set it for the process, not the user or the LocalMachine.

> get-ExecutionPolicy -list

        Scope ExecutionPolicy
        ----- ---------------
MachinePolicy       Undefined
   UserPolicy       Undefined
      Process       Undefined
  CurrentUser    Unrestricted
 LocalMachine    Unrestricted

I used this to set the execution policy of the process temporarily:

var iss = InitialSessionState.CreateDefault2();
iss.ExecutionPolicy = Microsoft.PowerShell.ExecutionPolicy.Unrestricted;
var runspace = RunspaceFactory.CreateRunspace(iss);

and then I could revert to the previous execution policy of the machine and yet run the tests.

> get-ExecutionPolicy -list

        Scope ExecutionPolicy
        ----- ---------------
MachinePolicy       Undefined
   UserPolicy       Undefined
      Process       Undefined
  CurrentUser    RemoteSigned
 LocalMachine    RemoteSigned
Sign up to request clarification or add additional context in comments.

1 Comment

Indeed, setting the process-level execution policy is the right choice for a PowerShell SDK project. The linked duplicate has background information.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.