【问题标题】:Check if given process is running with elevated right with powershell and Get-WmiObject使用 powershell 和 Get-WmiObject 检查给定进程是否以提升的权限运行
【发布时间】:2020-04-09 23:11:18
【问题描述】:

我必须遵循我的脚本的一部分:

$active_processes = (Get-WmiObject -Class Win32_Process | where path -like $path | Select-Object -ExpandProperty Path | split-path -leaf | Select-Object -Unique)

它工作正常,但我需要检查我在所有脚本运行后获得的进程是否以提升的权限运行,以便在必要时启动另一个具有提升权限的进程,以便它可以与所述进程交互。我没有看到任何关于 Get-WmiObject 提升权限的信息,我想知道我是否错过了它,或者是否有其他方法可以获取该信息

我不需要以管理员身份运行 powershell 脚本。我需要的是找到 ff 任何可执行文件在启动时都需要提升的权限,我需要通过 powershell 找到此信息。

【问题讨论】:

    标签: powershell powershell-5.0


    【解决方案1】:

    在研究了windows如何知道它是否需要管理员来运行可执行文件后,我得出结论,有几种方法,但最推荐和最可靠的是读取可执行文件清单,因此我编写了以下函数:

    function Get-ManifestFromExe{
        Param(
        [Parameter(Mandatory=$true,Position=0,ValueFromPipelineByPropertyName=$true)]
        [Alias("Path")]
        [ValidateScript({Test-Path $_ -IsValid})]
        [String]$FullName
        )
        begin{
            $stringStart = '<assembly'
            $stringEnd = 'assembly>'
        }
        process{
            $content = Get-Content $FullName -Raw
            $indexStart = $content.IndexOf($stringStart)
            $content = $content.Substring($indexStart)
            $indexEnd = ($content.IndexOf($stringEnd)) + $stringEnd.Length
            $content = $content.Substring(0,$indexEnd)
            if($content -match "$stringStart(.|\s)+(?=$stringEnd)$stringEnd"){
                return [XML]$Matches[0]
            } 
        }
    }
    
    function Test-IsAdminRequired{
        Param(
        [Parameter(Mandatory=$true,Position=0)]
        [XML]$xml
        )
        $value = $xml.assembly.trustInfo.security.requestedPrivileges.requestedExecutionLevel.level
        if(-not [String]::IsNullOrEmpty($value)){
            return ($value -eq "requireAdministrator" -or $value -eq "highestAvailable")
        }else{
            Write-Error "Provided xml does not contain requestedExecutionLevel node or level property"
        }
    }
    
    $exe = '.\Firefox Installer.exe'
    Get-ManifestFromExe -Path $exe
    Test-IsAdminRequired -xml $exeManifest
    

    它通过从可执行文件中提取清单 XML 并检查 requestedExecutionLevel 节点的 level 属性来工作,此属性接受的值在此 page 中,并在此引用:

    asInvoker,不请求其他权限。这个级别需要 没有额外的信任提示。

    highestAvailable,请求最高权限 父进程。

    需要管理员,请求完整的管理员权限。

    因此,由此我们可以得出结论,只有 highestAvailablerequireAdministrator 需要管理员权限,所以我检查了这些,我们将完成除了我测试的一些可执行文件(主要是安装程序) 不需要管理员运行,而是当他们破坏他们的子可执行文件时提示 UAC,我真的没有办法检查这个.. 抱歉。

    顺便说一句,我真的很喜欢这个问题(特别是研究),希望这可以帮助你。


    来源

    1. What is the difference between "asInvoker" and "highestAvailable" execution levels?
    2. reading an application's manifest file?
    3. https://docs.microsoft.com/en-us/visualstudio/deployment/trustinfo-element-clickonce-application?view=vs-2019#requestedexecutionlevel

    【讨论】:

    • 我不需要以管理员身份运行 powershell 脚本。我需要的是找到任何可执行文件在启动时需要提升的权限,我需要通过 powershell 找到此信息,如果我没有解释自己,对不起
    • 好的,我先研究一下然后再回复你
    【解决方案2】:

    它在 System.Security.Principal 类中。如果当前用户被提升为本地管理员,则返回 $true:

    (New-Object System.Security.Principal.WindowsPrincipal([System.Security.Principal.WindowsIdentity]::GetCurrent())).IsInRole([System.Security.Principal.WindowsBuiltInRole]::Administrator)

    【讨论】:

      猜你喜欢
      • 2022-01-15
      • 2022-09-28
      • 1970-01-01
      • 1970-01-01
      • 2013-05-31
      • 2020-05-21
      • 2015-11-23
      • 2012-07-14
      • 2010-12-08
      相关资源
      最近更新 更多