【发布时间】:2020-11-19 03:13:52
【问题描述】:
我正在处理的用例是从命令提示符中提取为特定应用程序(例如 Tableau 等)运行的服务列表的详细信息。输出通常采用以下格式
Tableau 服务中的文件内容
'Service 0' is running
'Service 1' is running
'Service 2' is running
'Service 3' is running
'Service 4' is running
'Test Service 5' is stopped
'SAML Service 6' is stopped
'Database Service 7' is stopped
'Tableau Service 8' is stopped
'Service 9' is running
'Service 10' is running
'Service 11' is running
'Service 12' is stopped
'Service 13' is running
'Service 14' is running
'Service 15' is stopped
最终的结果应该是已经停止的服务列表
Service 12
Service 15
脚本
#EXTRACT THE CONTENTS FROM THE TABLEAU SERVICES
$content = Get-Content 'C:\Tableau Services\TableauServices.txt'
$content.replace("`t", "").Replace(" is ",",").replace(".","").Replace("'","").Trim() | select -Skip 2 | Out-File 'C:\Tableau Services\TableauServices.csv'
$iRunningNumber=1
#Extract Services which have Stopped
Write-Host ("The List of Services that have stopped are as below: - ")
Import-CSV -Path 'C:\Tableau Services\TableauServices.csv' `
-delimiter ”,” `
-Header ServiceName, Status |
Where-Object {$_.ServiceName -notlike "*Test Service 5*" -AND $_.ServiceName - notlike "*SAML Service 6*" -AND $_.ServiceName -notlike "*Database Service 7*" -AND $_.ServiceName -notlike "*Tableau Service 8*"} |
Select-Object ServiceName, Status |
Where-Object {$_.Status -match "Stopped*"} |
ForEach-Object {"$($iRunningNumber).) $($_.ServiceName)"; $iRunningNumber++}
目前,要排除的服务在代码中是硬编码的。我想从配置文件中提取这些条件,然后从最终列表中搜索并排除。
我有另一个文件,其中包含要忽略的服务列表,如下所示:- 搜索文件.txt
Test Service 5
SAML Service 6
Database Service 7
Tableau Service 8
我已经尝试了我所知道的一切,但仍然没有解决方案。请求您的帮助
【问题讨论】:
-
您的代码正在创建您正在阅读的 CSV 文件。该文件的内容在哪里?
标签: powershell csv powershell-3.0