我需要powershell中的一个脚本,该脚本将检查本地计算机上所有服务的状态,并与我之前创建的.html文件中存储的允许服务的参考列表进行比较。比较之后,脚本将停止计算机上运行的所有未在引用文件中列出的服务。
首先,我想说的是,这段代码没有经过验证,不能很好地与其他html表一起使用。而且它根本没有优化。
$ServicesFromHTML = (gc .services.htm | where {$_ -match '^<tr><td>(Name:|Status:|<hr>)'}) -replace "</?t(r|d)>","" -join ";" -replace ";<hr>;","`r`n" -replace "(Name:|Status:)","" | ConvertFrom-Csv -Delimiter ";" -Header "Name","Status"
$RunningServices = Get-Service | Where-Object {$_.Status -eq "Running"} | Select Name,Status
"Comparing services if they are currently running ("<=") or were running when exported ("=>")"
Compare-Object $RunningServices.Name $ServicesFromHTML.Name
$OnlyNowRunningServices = (Compare-Object $RunningServices.Name $ServicesFromHTML.Name | where {$_.SideIndicator -eq "<="}).InputObject
$OnlyNowRunningServices | foreach {"Trying to stop Service $_";Stop-Service $_}
对于未来使用CSV/XML/JON-导出,因为您可以很容易地导入它们。若你们需要一些可读的东西在浏览器中阅读,那个么就另外创建它。