Replies: 2 comments 1 reply
|
By the time Set-ExecutionPolicy -Scope Process -ExecutionPolicy RemoteSigned | Get-PhysicalDisk
Clear the command list before building the next invocation: using var ps = PowerShell.Create();
ps.AddCommand("Set-ExecutionPolicy")
.AddParameter("Scope", "Process")
.AddParameter("ExecutionPolicy", "RemoteSigned")
.Invoke();
ps.Commands.Clear();
Collection<PSObject> result1 = ps.AddCommand("Get-PhysicalDisk")
.Invoke();
ps.Commands.Clear();
Collection<PSObject> result2 = ps.AddScript("Get-PhysicalDisk")
.Invoke();With the clears in place, both variants should return the disks. The difference you are seeing is therefore not command lookup or If the intention is to execute both operations in one invocation without piping the first into the second, Collection<PSObject> result = ps
.AddCommand("Set-ExecutionPolicy")
.AddParameter("Scope", "Process")
.AddParameter("ExecutionPolicy", "RemoteSigned")
.AddStatement()
.AddCommand("Get-PhysicalDisk")
.Invoke(); |
|
Thanks a lot for these valuable tips :-). |

Uh oh!
There was an error while loading. Please reload this page.
Output:
Why does the variant with
AddCommandreturn zero collection members?P.S.: The result for the 1st collection is zero even when replacing
Get-DiskwithGet-Process, orGet-Volumeetc.All reactions