我希望监控服务并在启动模式为自动时启动它们。
$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
时,它会尝试启动@{DisplayName=SQL Server Agent (JOURNYX)}
这是我的脚本:
$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
答案 0 :(得分:0)
你有一个对象$ WMI,其中包含值state和displayname。
执行$displayname = $WMI | select Displayname
时,您仍然有一个对象,现在名为$displayname
,并且其中包含一个名为displayname
的值。所以你需要引用这个值:
Start-Service $displayname.displayname
。
在某些情况下,最好使用Start-Service $($displayname.displayname)
,因此PS可以在将其用作参数之前对其进行评估。
答案 1 :(得分:0)
你需要通过' name'属于Start-Service。
gwmi win32_service | ? {$_.StartMode -eq 'Auto' -and $_.State -ne 'Running'} | Select -ExpandProperty Name | Start-Service
请在Where-Object(?)中添加过滤器。