【问题标题】:How can I extract "Path to executable" of all services with PowerShell如何使用 PowerShell 提取所有服务的“可执行路径”
【发布时间】:2014-08-18 09:41:24
【问题描述】:
Get-Service *sql* | sort DisplayName | out-file c:/servicelist.txt

我有一个 PowerShell 脚本来提取在我的本地计算机上运行的所有服务的列表,现在,除了显示“状态”、“名称”和“显示名称”之外,我还想显示 “路径可执行文件”

【问题讨论】:

  • 或者也只是wmic service get PathName。也适用于命令提示符。
  • 不应该是wmic service 'My Service' get pathname吗?

标签: powershell service powershell-2.0 powershell-3.0


【解决方案1】:

我认为您需要使用 WMI:

Get-WmiObject win32_service | ?{$_.Name -like '*sql*'} | select Name, DisplayName, State, PathName

更新 如果您想对选定的数据执行一些操作,可以使用 here 中描述的计算属性。

例如,如果您只想要路径名的引号内的文本,您可以用双引号拆分并获取数组项 1:

Get-WmiObject win32_service | ?{$_.Name -like '*sql*'} | select Name, DisplayName, @{Name="Path"; Expression={$_.PathName.split('"')[1]}} | Format-List

【讨论】:

  • 谢谢,但是使用“PathName”,我没有得到完整的路径,而是在路径截断了一定数量的字符后得到了类似C:\Program Files (x86)\Microsoft SQL S... E:\apps\MySQL\MySQL Server 5.0\bin\mys... 的东西,有什么办法可以获取完整路径?
  • 您的评论似乎遗漏了一些内容,我猜您已将其显示到屏幕上并且路径被截断。尝试通过 Format-List 管道查看完整路径。
  • 谢谢,我试过Format-List,效果很好。我可以在 .exe 之后截断部分路径吗?例如"C:\Program Files (x86)\Microsoft SQL Server\MSSQL.3\MSSQL\Binn\sqlservr.exe" -sSQLEXPRESS我不需要.exe之后的路径部分,我需要这样的东西"C:\Program Files (x86)\Microsoft SQL Server\MSSQL.3\MSSQL\Binn\sqlservr.exe"删除引号应该更好
  • 我添加了一个使用计算属性的示例,它应该对您有所帮助。
  • 如果 $_.PathName.Split 没有给您灵活性,那么您确实可以使用正则表达式:$_.PathName -replace 支持正则表达式,请参见此处:blogs.technet.com/b/heyscriptingguy/archive/2011/03/21/…
【解决方案2】:

可能更快的 WMI 查询变体(我只需要为 SCCM 客户端执行此操作)

$SQLService=(get-wmiobject -Query 'Select * from win32_service where Name like "*SQL*"') | Select-object Name, DisplayName, State, Pathname

如果你想要不带双引号的路径名,另一个技巧是捕获多个 SQL 结果(这样你就可以对它们采取行动)

$SQLService | Select-Object Name, DisplayName, State, @{Name='PathName';Expression=$_.Pathname.replace('"','')}

get-wmiobject(或get-ciminstance)中使用-query 的最大优势是处理速度。旧示例获取完整列表然后过滤,而后者获取非常直接的列表。

只需加两分钱:)

大家干杯! 肖恩 充满活力的科技

【讨论】:

  • 还有一个变体,但使用 -Filter 而不是 -Query。我希望它在引擎盖下非常相似,但打字可能会少一点:-) Get-WmiObject win32_service -Filter 'Name like "%SQL%"'
  • 建议的答案对我不起作用。问题是where Name like "*SQL*" 应该是where Name like "%SQL%"(注意% 是SQL 的通配符)
【解决方案3】:

由于 Get-WmiObject 在 PowerShell Core 中已被弃用,您可以使用

Get-CimInstance -ClassName win32_service | ?{$_.Name -match '^sql'} | Select Name, DisplayName, State, PathName >> C:\temp\sqlservices.txt

改为。

【讨论】:

    【解决方案4】:

    您还可以使用正则表达式模式并将结果转储到文件中。

    Get-WmiObject win32_service | ?{$_.Name -match '^sql'} | select Name, DisplayName, State, PathName >> C:\temp\sqlservices.txt
    

    【讨论】:

      【解决方案5】:

      我对接受的答案使用Expression={$_.PathName.split('"')[1]}} 感到不舒服,因为它不能处理我在数据中看到的引号、空格和参数的变体。

      这是一个笨拙的方法。

      function PathFromServicePathName($pathName) {
        # input can have quotes, spaces, and args like any of these:
        #   C:\WINDOWS\system32\lsass.exe
        #   "C:\Program Files\Realtek\Audio\HDA\RtkAudioService64.exe"
        #   C:\WINDOWS\system32\svchost.exe -k netsvcs -p
        #   "C:\Program Files\Websense\Websense Endpoint\wepsvc.exe" -k ss
      
        # if it starts with quote, return what's between first and second quotes
        if ($pathName.StartsWith("`"")) {
          $pathName = $pathName.Substring(1)
          $index = $pathName.IndexOf("`"")
          if ($index -gt -1) {
            return $pathName.Substring(0, $index)
          }
          else {
            # this should never happen... but whatever, return something
            return $pathName
          }
        }
        
        # else if it contains spaces, return what's before the first space
        if ($pathName.Contains(" ")) {
          $index = $pathName.IndexOf(" ")
          return $pathName.Substring(0, $index)
        }
        
        # else it's a simple path
        return $pathName
      }
      
      Get-WmiObject win32_service | select Name, DisplayName, @{Name="Path"; Expression={PathFromServicePathName $_.PathName}} | Format-List
      

      【讨论】:

        【解决方案6】:

        带有完整路径的 Format-List 变体,生成文件:

        Get-WmiObject win32_service | Format-Table -Wrap -AutoSize -Property State,Name,PathName | out-file C:\servicelist.txt
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2015-10-04
          • 1970-01-01
          • 1970-01-01
          • 2011-02-28
          • 1970-01-01
          • 2021-08-24
          相关资源
          最近更新 更多