简体   繁体   English

将本地计算机上的所有服务与存储在 .html 文件中的允许服务的引用列表进行比较 - Powershell

[英]Compare all services on the local computer with referent list of allowed services stored in .html file - Powershell

I need a script in powershell that will check the status of all services on the local computer and compare with the reference list of allowed services stored in the.html file I created earlier.我需要powershell中的一个脚本,它将检查本地计算机上所有服务的状态,并与我之前创建的 .html 文件中存储的允许服务的参考列表进行比较。 After the comparison, the script will stop all services running on the computer that are not listed in the reference file.比较后,脚本将停止计算机上运行的所有未在参考文件中列出的服务。

First i would like to say this code has no validation and will not work well with other html tables.首先,我想说这段代码没有经过验证,不能很好地与其他 html 表一起使用。 And it is not optimized at all.而且它根本没有优化。

$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 $_}

For the future use CSV/XML/JSON export as you can import them quite easy.将来使用 CSV/XML/JSON 导出,因为您可以很容易地导入它们。 If you need something human readable to read in browser, then create it additionaly.如果你需要一些人类可读的东西在浏览器中阅读,那么额外创建它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM