Forum Discussion
MrCamaradarie
Apr 05, 2024Copper Contributor
How to Execute Disable-NetAdapter from Constrained Runspace?
Please, I need help ASAP!!! What .NET Class maps to Disbale-NetAdapter Commandlet? E.g., the .NET Class, GetDateCommand is an Implementation of the get-date commandlet. ------------------------...
MrCamaradarie
Apr 06, 2024Copper Contributor
Thank you for taking the time to read and respond .
As you may know, to implement a Constrained PowerShell Runspace, one would have to explicitly state the Modules and Commands one wishes to use.
E.g., if I wish to use PowerShell's C# Module, I'll write the following C# Code
C# Code for explicitly adding the Microsoft.PowerShell.Core and NetAdapter Modules to the PowerShell Session.
var coreModuleSpecification = new ModuleSpecification("Microsoft.PowerShell.Core");
var netAdapterModuleSpecification = new ModuleSpecification("NetAdapter");
powerShellSession.ImportPSModule(new[] { coreModuleSpecification, netAdapterModuleSpecification });
Now, after, adding the Modules I require, I would also have to explicitly include the Commandlets within the Module I wish to execute as follows;
C# Code for explicitly adding the Import-Module Command to the PowerShell Session.
var importModuleCommand = new SessionStateCmdletEntry("Import-Module",
typeof(ImportModuleCommand), null);
powerShellSession.Commands.Add(importModuleCommand);
Please, note the .NET Class, ImportModuleCommand being passed as an argument to the typeof operator in the code above. It corresponds to the Import-Module PowerShell Commandlet. Quite a number of PowerShell Commandlets have their corresponding .NET Classes. For instance, Get-Date PowerShell Commandlet has a GetDateCommand corresponding class in the .NET namespace, Microsoft.PowerShell.Commands, so does Get-Command PowerShell Commandlet, a corresponding .NET Class, GetCommandCommand.
I NEED TO KNOW WHAT CORRESPONDING .NET CLASS TO PASS TO THE typeof operator WHEN CONSTRUCTING SessionStateCmdletEntry for the Disable-NetAdapter Commandlet.
LainRobertson
Apr 07, 2024Silver Contributor
The output type for WMI resources is [Microsoft.Management.Infrastructure.CimInstance].
Cheers,
Lain