将本地计算机上的所有服务与存储在 .html 文件中的允许服务的引用列表进行比较 - Powershell
Compare all services on the local computer with referent list of allowed services stored in .html file - Powershell
我需要 powershell 中的脚本来检查本地计算机上所有服务的状态,并与存储在 .[=15= 中的允许服务的参考列表进行比较] 我之前创建的文件。比较后,脚本将停止计算机上未在参考文件中列出的所有服务运行。
首先我想说这段代码没有经过验证,不能很好地与其他 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/JSON 导出,因为您可以很容易地导入它们。如果您需要在浏览器中阅读人类可读的内容,请另外创建它。
我需要 powershell 中的脚本来检查本地计算机上所有服务的状态,并与存储在 .[=15= 中的允许服务的参考列表进行比较] 我之前创建的文件。比较后,脚本将停止计算机上未在参考文件中列出的所有服务运行。
首先我想说这段代码没有经过验证,不能很好地与其他 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/JSON 导出,因为您可以很容易地导入它们。如果您需要在浏览器中阅读人类可读的内容,请另外创建它。