サービスを監視して、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
ありがとうございました。それは私が通常行うものですが、うまくいきません。 $ displayname.displaynameが空です。 はい、WMIコマンドを実行しました Start-Service $($ displayname.displayname) Start-Service:引数がnullであるため、パラメータ 'Name'に引数をバインドできません。 – ArKersten
ああ。 $ Displaynameは依然としてオブジェクトの配列であり、 '$ displayname [0] .displayname'などが表示されます。あなたの 'start-Service'ステートメントで' $($ servicename.displayname) 'を使ってみましょう。 – Martin
ありがとうございます。$ displayname [0] .displaynameが機能しました。 残念ながら、私は2人のうちの1人にメッセージを返すだけです。 SQL Serverエージェント(JOURNYX)サービスを開始しようとしましたが、失敗しました – ArKersten