.png)
사소한 문제를 해결하는 데 문제가 있습니다. 따라서 프로세스의 경우 다음 줄을 사용하여 $PID의 이름을 저장할 수 있습니다.
$process_name=get-Process -id $PID |선택 -이름 확장
그러나 프로세스를 다음과 같이 실행하고 싶습니다.서비스. 동일한 작업을 완료하고 싶습니다. 즉, 서비스 이름(예: [service_name].exe)을 $service_name 변수에 저장하고 싶습니다.
그 이유는 하나의 .ps1 파일만 갖고 싶기 때문에 자체 .exe.config 파일을 사용하는 여러 서비스로 전환할 수 있기 때문입니다. 따라서 단 하나의 .ps1 파일을 다음과 같은 서비스로 여러 번 컴파일할 수 있습니다.
[service1.exe, service2.exe, service3.exe, ...]
각 서비스는 다음과 같은 해당 .exe.config 파일을 사용합니다.
[service1.exe.config, service2.exe.config, service3.exe.config, ...]
Powershell로 이를 수행할 수 있는 방법이 있습니까?
답변1
Processid에서 서비스 정보를 얻을 수 있습니다.
(Get-WmiObject Win32_Service -Filter "ProcessId='$PID'")
답변2
Get-WmiObject를 사용하면 실행 파일을 수집할 수 있습니다.
(Get-WmiObject win32_service | Where-Object -Property Name -Like *wallet*).PathName
이 예에서는 스위치와 함께 실행 파일 이름을 표시합니다. 이것을 변수로 캡처하려면 다음을 시도하십시오.
$proc_name = (Get-WmiObject win32_service | Where-Object -Property Name -Like *wallet*).PathName
확장된 속성 정보가 필요한 경우 다음을 시도하십시오.
Get-WmiObject win32_service | ?{$_.Name -like '*wallet*'} | Select-Object -Property *
답변3
또 다른 방법이 있습니다. 위의 방법이 더 신뢰할 수 있을까요?
$id = 5556
# (debugging) Nice little table to make sure you've got the right thing...
Get-Process | Where-Object Id -EQ $id | Select ProcessName,Id | Format-Table
# Finally just hand me back the name proper
Get-Process | Where-Object Id -EQ $id | Select -Expand ProcessName
# Depending on what you're doing with it...
# Stop-Process -Id $id