检查提升的进程状态? [英] Check elevated process status?

查看:42
本文介绍了检查提升的进程状态?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想找到一种方法来确定进程是否以提升的方式运行或未使用 Powershell.

I would like to find a way to find out if a process is running as elevated or not using Powershell.

用例:能够以本地域用户的身份运行具有提升权限的控制面板任务,例如添加或删除程序.

Use Case: Being able to run control panel tasks with elevated privilage as local domain user e.g. Add or Remove programs.

任何帮助将不胜感激.

#Start add or remove as admin
start-process appwiz.cpl -verb runas

#Check if path exists. Answer is Yes, so process is NOT elevated
get-wmiobject -class win32_process | select-object -properties name, path

推荐答案

以下是两个常用选项:

  1. 在脚本中使用 #requires -RunAsAdministrator 行(需要 PowerShell 3.0 或更高版本).如果您在脚本顶部使用此行,它将抛出终止错误,并且如果当前进程未提升,则不会执行.

  1. Use the #requires -RunAsAdministrator line in your script (requires PowerShell 3.0 or later). If you use this line at the top of your script, it will throw a terminating error and won't execute if the current process isn't elevated.

使用如下代码检测当前进程是否被提升:

Use code like the following to detect whether the current process is elevated:

$IsElevated = ([Security.Principal.WindowsPrincipal] [Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole]::Administrator)

这篇关于检查提升的进程状态?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆