2016-05-31 10 views
0

サービスを監視して、startmodeが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にオブジェクト$ 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 

答えて

0

これが今の私のスクリプトです。

$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:引数がnullであるため、パラメータ 'Name'に引数をバインドできません。 – ArKersten

+0

ああ。 $ Displaynameは依然としてオブジェクトの配列であり、 '$ displayname [0] .displayname'などが表示されます。あなたの 'start-Service'ステートメントで' $($ servicename.displayname) 'を使ってみましょう。 – Martin

+0

ありがとうございます。$ displayname [0] .displaynameが機能しました。 残念ながら、私は2人のうちの1人にメッセージを返すだけです。 SQL Serverエージェント(JOURNYX)サービスを開始しようとしましたが、失敗しました – ArKersten

0

「名前」属性を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 -ne 'Running'} | -ExpandProperty Nameを選択してください。 if($ wmi。カウント-ge 1){ foreachの($の$ WMIでのサービス名){ 試し{ スタート・サービスの$ SERVICENAME を "失敗した後に開始した、" "サービス" の$ SERVICENAMEは 書き込みホストを停止-ErrorAction } キャッチ{他 } } } {$のサービス名を "しかし、失敗した" "を開始しようとしました" 書き込みホスト write-host "OKを報告しているサービス" 出口0 } – ArKersten

+0

サービスの別の見方を教えてください。 Displaynameははるかに明確です。 – ArKersten

+0

DisplayNameが必要な場合は、まずStart-Serviceを実行する前にGet-Serviceのオブジェクトを作成します。 – zerocool18

関連する問題