2016-05-31 19 views
0

我想監視服務並在啓動模式爲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 

回答

0

你有一個對象$ WMI,與狀態值和顯示名稱中它。

當您執行$displayname = $WMI | select Displayname時,您仍然有一個對象,它現在名爲$displayname,其中有一個名爲displayname的值。所以你需要引用該值:

Start-Service $displayname.displayname

在某些情況下,最好使用Start-Service $($displayname.displayname),因此PS可以在將其用作參數之前評估該值。

+0

謝謝馬丁。這是我通常做的,但不起作用: $ displayname.displayname爲空。 是的,我確實運行了WMI命令 Start-Service $($ displayname.displayname) Start-Service:無法將參數綁定到參數'Name',因爲它爲空。 – ArKersten

+0

啊好的。 $ Displayname仍然是一個對象數組,你應該看到'$ displayname [0] .displayname'等等。嘗試在'start-Service'語句中使用'$($ servicename.displayname)'。 – Martin

+0

謝謝,$ displayname [0] .displayname確實有效。 不幸的是,我只得到其中一個返回消息。 試圖啓動SQL Server代理(JOURNYX)服務,但失敗 – ArKersten

0

您需要將'name'屬性傳遞給Start-Service。

gwmi win32_service | ? {$_.StartMode -eq 'Auto' -and $_.State -ne 'Running'} | Select -ExpandProperty Name | Start-Service 

請將您的過濾器添加到Where-Object(?)中。

+0

嗨,謝謝! 這有幫助。我確實使用了腳本的部分內容: 現在是: '$ WMI = gwmi win32_service | ? {$ _。StartMode -eq'Auto''和$ _。State - ''Running'} |選擇-ExpandProperty Name if($ wmi。計數-ge 1){ 的foreach在$ WMI($服務名){ 嘗試{ 啓動服務$服務名 「被失敗後,啓動了」 -ErrorAction停止 寫 - 主機 「服務」 $服務名 } 趕上{ 寫 - 主機 「試圖啓動」 $服務名 「但沒有」 } } } 其他{ 寫主機「服務報告確定」 退出0 }' – ArKersten

+0

太糟糕了,它給了我另一種服務的看法。 Displayname更清晰。 – ArKersten

+0

如果您需要DisplayName,則在開始服務之前首先創建Get-Service的對象。 – zerocool18