I am trying to get drive information for a server through PowerShell 6.0 using the 'GET-PSDrive' command. Running the command directly in PowerShell, I see the values for 'Used' and 'Free' within the output table, but running the same command in code though using the Microsoft.Powershell.Sdk the 'Used' and 'Free' fields are not available.
I see both items listed under the PSObject.Properties array, but trying to access the value I receive the exception:
"There is no Runspace available to run scripts in this thread. You can provide one in the DefaultRunspace property of the System.Management.Automation.Runspaces.Runspace type. The script block you attempted to invoke was: ##"
The following is the POC code that I am working with:
using (var psCon = PowerShell.Create())
{
psCon.AddCommand("GET-PSDrive");
var psReturn = psCon.Invoke();
foreach (var psObj in psReturn)
{
var driveUsedValue = psObj.Properties["Used"].Value;
}
}
I am expecting to get the value of that property, but each time the value is evaluated I get an error saying that no runspace is available. Inspecting the property I do see that its a ScriptProperty, so how do you go about getting that generated value?