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.