我想監視服務並在啓動模式爲Auto時啓動它們。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
它試圖啓動@{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
謝謝馬丁。這是我通常做的,但不起作用: $ displayname.displayname爲空。 是的,我確實運行了WMI命令 Start-Service $($ displayname.displayname) Start-Service:無法將參數綁定到參數'Name',因爲它爲空。 – ArKersten
啊好的。 $ Displayname仍然是一個對象數組,你應該看到'$ displayname [0] .displayname'等等。嘗試在'start-Service'語句中使用'$($ servicename.displayname)'。 – Martin
謝謝,$ displayname [0] .displayname確實有效。 不幸的是,我只得到其中一個返回消息。 試圖啓動SQL Server代理(JOURNYX)服務,但失敗 – ArKersten