PowerShell 获取服务的生产商 1


Get-Service 获取的对象属于System.ServiceProcess.ServiceController 类,该类中找不到生产商的信息。所以我们尝试通过 WMI 对象中的win32_service类,该类中包含了pathName属性,也就是Service的物理路径。通过路径获取文件的版本信息(VersionInfo),该信息通常可能包含了CompanyName。
但是也有例外,像这款产品“System Center 2012 Configuration Manager”的服务 “smstsmgr”就没有。

补充:最近写的几个脚本中很喜欢用NoteProperty,因为它可以在不改变原对象属性的基础上,来扩展对象,方便其他用户进行二次定制。
Retrive Manufacturer of Service

Get-WmiObject win32_service |ForEach-Object {
    $path=""
    $company=""

    #match the path that contains double quotes,
    #i.e "C:\Program Files (x86)\Microsoft\BingDesktop\BingDesktopUpdater.exe"
    if($_.pathName -match '"(?<SvcPath>.*)"') {
        $path=$Matches['SvcPath']
    }

    #match the path that not contains quotes,
    #i.e C:\Windows\CCM\RemCtrl\CmRcService.exe
    else  { 

        #split to handle path that contains parameter,
        #i.e C:\Windows\system32\svchost.exe -k LocalSystemNetworkRestricted
        $path=($_.pathName  -split ' ') | select -First 1 
    }
    if(-not [string]::IsNullOrWhiteSpace($path)) {
         $company=(Get-Item $path ).VersionInfo.CompanyName
    }

    $_ | Add-Member -MemberType NoteProperty -Name "Manufacturer" -Value $company
    $_
}  | Select-Object DisplayName, Manufacturer,State,Description |
Out-GridView

 

本文链接: https://www.pstips.net/retrive-manufacturer-of-service.html
请尊重原作者和编辑的辛勤劳动,欢迎转载,并注明出处!

关于 Mooser Lee

我是一个Powershell的爱好者,创建了PowerShell中文博客,热衷于Powershell技术的搜集和分享。本站部分内容来源于互联网,不足之处敬请谅解,并欢迎您批评指正。

发表评论

您的电子邮箱地址不会被公开。 必填项已用 * 标注

一条评论 “PowerShell 获取服务的生产商

  • 有朋小吃店

    我有补充,对于不是要看Out-GridView而是要输出format-list的我
    会看到有些奇怪的服务因为各种情况出红字,解决方法是
    $company=(Get-Item $path ).VersionInfo.CompanyName
    加上
    $company=(Get-Item $path -ErrorAction Ignore).VersionInfo.CompanyName