powershell 在没有管理员权限提示的情况下禁用网络适配器

jqjz2hbq  于 2022-11-10  发布在  Shell
关注(0)|答案(3)|浏览(198)

当用户点击按钮时,我需要启用/禁用Windows 8平板电脑上的所有网络适配器(有点像飞行模式)。
这可以通过PowerShell中的以下cmdlet来完成:“Disable-NetAdapter*-Confirm:$False”及其对应的Enable-NetAdapter。他们做的正是我期望他们做的,但我有两个问题:
1.我不想从WPF应用程序运行PowerShell。由于它是基于.NET框架构建的,有没有办法在不调用cmdlet的情况下执行相同的操作?
1.它需要提升权限(比如右击+以管理员身份运行启动应用)。我可以从代码中获得提升的权限,但我总是会看到请求批准的用户访问控制弹出窗口。有没有办法总是以提升的权限启动应用程序,而不会出现弹出窗口?

g6ll5ycj

g6ll5ycj1#

Win32_NetworkAdapter类别包含启用/禁用方法http://msdn.microsoft.com/en-us/library/aa394216
以下是来自Programmatically Enable / Disable Connection的代码示例
如果操作需要,您需要在管理员或系统上下文中运行,理想情况下,因为系统没有UAC,所以您可以作为服务运行!

tcbh2hod

tcbh2hod2#

以下是我在生产中实际使用的一些VB.NET代码的示例:

Imports System.Management
Imports System.Text.RegularExpressions

            Try
                Dim scope As New ManagementScope("\\" + computername + "\root\CIMV2")
                scope.Connect()

                Dim query As New ObjectQuery( _
                    "SELECT * FROM Win32_NetworkAdapter WHERE Manufacturer != 'Microsoft' AND NOT PNPDeviceID LIKE 'ROOT\\%'")

                Dim searcher As New ManagementObjectSearcher(scope, query)

                For Each queryObj As ManagementObject In searcher.Get()

                    Dim ServiceName As String = queryObj("ServiceName")
                    Dim ProductName As String = queryObj("Description")
                    If Regex.IsMatch(ServiceName, ".*NETw.*") Then
                        'if we detect a wireless connection service name...

                        If Regex.IsMatch(queryObj("netenabled"), ".*true.*", RegexOptions.IgnoreCase) Then                                
                           MessageBox.Show(ProductName + " is already enabled! [ " + queryObj("netenabled") + " ]")

                        Else
                            'Try to enable the wireless connection here
                            queryObj.InvokeMethod("Enable", Nothing)                                
                                MessageBox.Show(ProductName + " was successfully enabled!")                               
                        End If
                    End If
                Next
            Catch ex As Exception
                Messagebox.show(ex.Message)
            End Try

编辑:添加C#等效项:

try {
ManagementScope scope = new ManagementScope("\\\\" + computername + "\\root\\CIMV2");
scope.Connect();

ObjectQuery query = new ObjectQuery("SELECT * FROM Win32_NetworkAdapter WHERE Manufacturer != 'Microsoft' AND NOT PNPDeviceID LIKE 'ROOT\\\\%'");

ManagementObjectSearcher searcher = new ManagementObjectSearcher(scope, query);

foreach (ManagementObject queryObj in searcher.Get()) {
    string ServiceName = queryObj("ServiceName");
    string ProductName = queryObj("Description");
    if (Regex.IsMatch(ServiceName, ".*NETw.*")) {
        //if we detect a wireless connection service name...

        if (Regex.IsMatch(queryObj("netenabled"), ".*true.*", RegexOptions.IgnoreCase)) {
            MessageBox.Show(ProductName + " is already enabled! [ " + queryObj("netenabled") + " ]");

        } else {
            //Try to enable the wireless connection here
            queryObj.InvokeMethod("Enable", null);
            MessageBox.Show(ProductName + " was successfully enabled!");
        }
    }
}
} catch (Exception ex) {
Messagebox.show(ex.Message);
}
qmelpv7a

qmelpv7a3#

我知道这个问题很古老,但对将来的某个人可能会有用,因此在这里发布了我的答案。

public class Program
{     
    static void Main(string[] args)
    {
        string command = "netsh interface set interface \"Ethernet\" enable";
        ProcessStartInfo processStartInfo = new ProcessStartInfo("CMD", command);
        Process proc = new Process();
        proc.StartInfo = processStartInfo;           
        proc.StartInfo.WindowStyle = ProcessWindowStyle.Hidden; 
        proc.StartInfo.UseShellExecute = true;
        proc.StartInfo.Verb = "runas";            
        proc.StartInfo.Arguments = "/env /user:" + "Administrator" + " cmd /K"+ command;
        proc.StartInfo.CreateNoWindow = true;
        proc.Start();
        proc.WaitForExit();
    }
}

Ethernet是网络接口的名称。要获取网络接口的名称,只需在任何命令提示符中键入以下内容
Netsh接口显示接口
这对我来说很好,有用户帐户和管理员帐户,不需要批准管理员权限。

相关问题