서비스를 모니터링하고 시작 모드가 자동 일 때 서비스를 시작하고 싶습니다. 실행할 때 내 경우Powershell에서 변수의 문자열 줄
$WMI = Get-WmiObject Win32_Service |
Where-Object { $_.StartMode -eq 'Auto' -and $_.State -ne 'Running' -and $_.DisplayName -notlike 'Microsoft .NET Framework NGEN v4.0.30319_X86' -and $_.DisplayName -notlike 'Windows Image Acquisition (WIA)' -and $_.DisplayName -notlike 'Microsoft .NET Framework NGEN v4.0.30319_X64' -and $_.DisplayName -notlike 'Software Protection' -and $_.DisplayName -notlike 'Google Update Service (gupdate)' -and $_.DisplayName -notlike 'Google Update-service (gupdate)' -and $_.DisplayName -notlike 'Pml Driver HPZ12' -and $_.DisplayName -notlike 'Shell Hardware Detection' -and $_.DisplayName -notlike 'Group Policy Client' -and $_.DisplayName -notlike 'Multimedia Class Scheduler' -and $_.DisplayName -notlike 'Skype Updater' -and $_.DisplayName -notlike 'Remote Registry' -and $_.DisplayName -notlike 'TPM Base Services' -and $_.DisplayName -notlike 'Windows Update' -and $_.DisplayName -notlike 'Windows Modules Installer' -and $_.DisplayName -notlike 'Smart Card'} | Select DisplayName,State
이 나에게 두 가지 결과를 제공합니다 :
$displayname = $WMI | select Displayname
결과 :
DisplayName
-----------
SQL Server Agent (JOURNYX)
Performance Logs and Alerts
나는 그것이 이것은 foreach
사용하여 동작하지 않습니다 결국 내가 Start-Service $displayname
을 사용할 때 시작하려고 시도하기 때문에당신은 그것을의 값 상태와 표시 이름을 가진 객체 $ WMI를 가지고
$WMI = Get-WmiObject Win32_Service |
Where-Object { $_.StartMode -eq 'Auto' -and $_.State -ne 'Running' -and $_.DisplayName -notlike 'Microsoft .NET Framework NGEN v4.0.30319_X86' -and $_.DisplayName -notlike 'Windows Image Acquisition (WIA)' -and $_.DisplayName -notlike 'Microsoft .NET Framework NGEN v4.0.30319_X64' -and $_.DisplayName -notlike 'Software Protection' -and $_.DisplayName -notlike 'Google Update Service (gupdate)' -and $_.DisplayName -notlike 'Google Update-service (gupdate)' -and $_.DisplayName -notlike 'Pml Driver HPZ12' -and $_.DisplayName -notlike 'Shell Hardware Detection' -and $_.DisplayName -notlike 'Group Policy Client' -and $_.DisplayName -notlike 'Multimedia Class Scheduler' -and $_.DisplayName -notlike 'Skype Updater' -and $_.DisplayName -notlike 'Remote Registry' -and $_.DisplayName -notlike 'TPM Base Services' -and $_.DisplayName -notlike 'Windows Update' -and $_.DisplayName -notlike 'Windows Modules Installer' -and $_.DisplayName -notlike 'Smart Card'} | Select DisplayName,State
$displayname = $WMI | select Displayname
foreach ($servicename in $displayname) {
try {
start-service $Servicename.DisplayName -ErrorAction stop
Write-host "Service" $servicename.displayname "started, after being failed"
exit 1001
}
catch {
Write-Host "Tried to start" $servicename.displayname "Service, but failed"
exit 1001
}
}
write-host "Services reporting OK"
Exit 0
고마워요. 그게 내가 보통하는 일이지만 작동하지 않습니다. $ displayname.displayname이 비어 있습니다. 예, WMI 명령을 실행했습니다. 시작 서비스 $ ($ displayname.displayname) 시작 서비스 : 'Name'매개 변수가 null이므로 바인딩 할 수 없습니다. – ArKersten
아, 그래. $ Displayname은 여전히 객체의 배열이며,'$ displayname [0] .displayname' 등을 볼 수 있습니다. 'start-Service' 문에서'$ ($ servicename.displayname)'을 사용해보십시오. – Martin
감사합니다. $ displayname [0] .displayname이 (가) 작동했습니다. 불행히도, 나는 메시지를 반환하는 두 사람 중 하나만 얻습니다. SQL Server Agent (JOURNYX) 서비스를 시작하려고했으나 실패했습니다. – ArKersten