Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/311.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在windows 7 enterprise上访问windows powershell与c#程序中的WMI_C#_Powershell_Wmi - Fatal编程技术网

在windows 7 enterprise上访问windows powershell与c#程序中的WMI

在windows 7 enterprise上访问windows powershell与c#程序中的WMI,c#,powershell,wmi,C#,Powershell,Wmi,我的帐户具有管理权限。 我使用powershell访问Windows 7 Enterprise VM上的WMI,如下所示: Get-WmiObject -Namespace root\SecurityCenter2 -Class AntiVirusProduct -ComputerName $computername 和C#如下所示: string computer = Environment.MachineName; string wmipath = @"

我的帐户具有管理权限。 我使用powershell访问Windows 7 Enterprise VM上的WMI,如下所示:

 Get-WmiObject -Namespace root\SecurityCenter2 -Class AntiVirusProduct  -ComputerName $computername
和C#如下所示:

        string computer = Environment.MachineName;
        string wmipath = @"\\" + computer + @"\root\SecurityCenter2";
        try
        {
            ManagementObjectSearcher searcher = new ManagementObjectSearcher(wmipath,
              "SELECT * FROM AntivirusProduct");
            ManagementObjectCollection instances = searcher.Get();
            //MessageBox.Show(instances.Count.ToString()); 
            foreach (ManagementObject queryObj in instances)
            {
                return queryObj[type].ToString();
            }
        }

        catch (Exception e)
        {
            MessageBox.Show(e.Message);
        }

但是,Powershell中的代码始终有效,而C#中的代码只有在我以管理员身份显式运行程序时才有效。我是否可以向C代码中添加任何内容,以便它可以为具有管理权限的用户运行,而无需以管理员身份显式启动C程序?

您可以通过编辑(与C可执行文件位于同一目录中的XML文件)强制UAC提示,而无需显式地“以管理员身份运行”)


请参阅StackOverflow答案。

通常情况下,我的应用程序只能从我使用的计算机管理员处运行 此方法用于验证管理员权限:

public static bool HasAdministrativeRight()
        {
            WindowsPrincipal principal = new WindowsPrincipal(WindowsIdentity.GetCurrent());
            return principal.IsInRole(WindowsBuiltInRole.Administrator);
        }
在我的代码的主要部分(窗体或控制台应用程序)

以高架方式运行的代码:

private static bool RunElevated(string args)
        {
            ProcessStartInfo processInfo = new ProcessStartInfo();
            processInfo.Verb = "runas";
            processInfo.FileName = Application.ExecutablePath;
            processInfo.Arguments = args;
            try
            {
                Process.Start(processInfo);
                return true;
            }
            catch (Exception)
            {
                //Do nothing. Probably the user canceled the UAC window
            }
            return false;
        }

不,你不能。在W7中激活UAC后,当需要权限时,您的“管理权限”仅允许您在UAC弹出窗口中回答“是”。如果您的应用程序需要管理员权限,则需要使用“以管理员身份运行”启动应用程序,或在软件中内置一个签入,以触发UAC弹出窗口。请记住,您可以通过快捷方式->快捷方式->前进->始终管理进入属性,以始终以管理员身份运行。最后一个选择是ofc。停用UAC,但这在与工作相关时并不流行:)如果我错了,请纠正我。有趣的是,在另一台机器上,相同的代码不要求以管理员身份运行,我有点困惑。。。。
private static bool RunElevated(string args)
        {
            ProcessStartInfo processInfo = new ProcessStartInfo();
            processInfo.Verb = "runas";
            processInfo.FileName = Application.ExecutablePath;
            processInfo.Arguments = args;
            try
            {
                Process.Start(processInfo);
                return true;
            }
            catch (Exception)
            {
                //Do nothing. Probably the user canceled the UAC window
            }
            return false;
        }