PowerShell을 사용하여 모든 서비스의 "실행 경로"를 추출하는 방법
Get-Service *sql* | sort DisplayName | out-file c:/servicelist.txt
로컬 컴퓨터에서 실행 중인 모든 서비스 목록을 추출하는 한 줄의 PowerShell 스크립트가 있으며, "Status", "Name" 및 "DisplayName"을 표시할 뿐만 아니라 "Path to executable"도 표시합니다.
WMI를 사용해야 할 것 같습니다.
Get-WmiObject win32_service | ?{$_.Name -like '*sql*'} | select Name, DisplayName, State, PathName
Update 선택한 데이터에 대해 약간의 조작을 수행하려면 여기에 설명된 대로 계산된 속성을 사용할 수 있습니다.
예를 들어, Pathname에 대한 따옴표 내의 텍스트를 원하는 경우에는 큰 따옴표로 나누어 배열 항목 1을 선택할 수 있습니다.
Get-WmiObject win32_service | ?{$_.Name -like '*sql*'} | select Name, DisplayName, @{Name="Path"; Expression={$_.PathName.split('"')[1]}} | Format-List
Get-CimInstance를 사용하여 동일한 작업을 수행할 수도 있습니다. CIM과 WMI의 차이점은 여기를 참조하십시오.
Get-CimInstance win32_service | ?{$_.Name -like '*sql*'} | select Name, DisplayName, @{Name="Path"; Expression={$_.PathName.split('"')[1]}} | Format-List
부터Get-WmiObjectPowerShell Core에서 더 이상 사용하지 않도록 설정되었습니다.
Get-CimInstance -ClassName win32_service | ?{$_.Name -match '^sql'} | Select Name, DisplayName, State, PathName >> C:\temp\sqlservices.txt
대신.
정규 표현식을 확인할 필요가 없는 경우 사용할 수도 있습니다.-Filter매개 변수:
Get-CimInstance -ClassName win32_service -Filter "Name like 'sql%'" | Select Name, DisplayName, State, PathName >> C:\temp\sqlservices.txt
더 빠를 수 있는 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('"','')}
사용의 큰 장점-query에서get-wmiobject(또는get-ciminstance)는 처리 속도입니다.이전 예는 전체 목록을 얻은 다음 필터링하는 반면 후자는 매우 직접적인 목록을 가져옵니다.
그냥 2센트만 더하면 됩니다 :)
다들 파이팅!숀 더 에너제이션 테크
정규 표현식 패턴을 사용하여 결과를 파일로 덤프할 수도 있습니다.
Get-WmiObject win32_service | ?{$_.Name -match '^sql'} | select Name, DisplayName, State, PathName >> C:\temp\sqlservices.txt
저는 수용된 답변의 사용이 마음에 들지 않습니다.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
전체 경로를 가진 Format-List를 가진 변형은 다음과 같은 결과를 가져옵니다.
Get-WmiObject win32_service | Format-Table -Wrap -AutoSize -Property State,Name,PathName | out-file C:\servicelist.txt
파워셸 7에서 이 속성이 추가되었으므로 사용할 수 있습니다.Get-Service한편,
Get-Service *sql* | Select-Object -Property Status, Name, DisplayName, BinaryPathName
언급URL : https://stackoverflow.com/questions/24449113/how-can-i-extract-path-to-executable-of-all-services-with-powershell
'codememo' 카테고리의 다른 글
| __attribute__((시공자)) VC에서 동치입니까? (0) | 2023.09.15 |
|---|---|
| 이 memcpy 구현에서 누락된/최적의 것은 무엇입니까? (0) | 2023.09.15 |
| rand() 함수 없이 난수를 생성하려면 어떻게 해야 합니까? (0) | 2023.09.15 |
| setTimeout/clearTimeout 문제 (0) | 2023.09.15 |
| jQuery에서 태그 이름을 제공할 수 있습니까? (0) | 2023.09.15 |