How to Execute Disable-NetAdapter from Constrained Runspace?

Copper Contributor

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.


var getDateCommand = new SessionStateCmdletEntry("get-date",
typeof(GetDateCommand), null);


I need to know the Class to use for Disbale-NetAdapter, please.

That is what type am I to use

3 Replies



Hi, Charles.


.NET had no class for manipulating network interfaces. It's done via WMI.


That said, I'm not sure what the issue is since constrained language mode on its own does not prevent you from running Disable-NetAdapter, as it falls under the allowed type of [wmi].



It seems more likely to me you're dealing with PowerShell's JEA (which uses the "no language mode" setting):



If JEA is being used then it's quite likely you have no way around it.


You can try the following alternative to Disable-NetAdapter, but if JEA is being used, I'd expect it to fail.


# Be sure you to replace 'WiFi' from the following example with your own adapter's display name. Or you can use a different filter altogether if you know what you're filtering on.
Invoke-CimMethod -Query "SELECT * FROM Win32_NetworkAdapter WHERE NetConnectionID = 'WiFi'" -MethodName Disable;




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);


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.







The output type for WMI resources is [Microsoft.Management.Infrastructure.CimInstance].