Windows 服务的 CSV 比较
CSV comparison of Windows Services
我是 Powershell 的新手,我被一个更大的脚本的这一部分困住了。我需要提取所有 Windows 服务并比较它们以查看它们的启动类型状态是否已更改。如果有任何变化,我需要对它们进行计数,以便将该值放入电子邮件正文中。此外,我需要附上一份 HTML 报告,显示已更改的 Windows 服务的先前和当前状态。
我所做的如下:
这段代码生成一个 CSV 文件,显示服务的当前状态。
Get-Service | Select-Object -Property Name,DisplayName,StartType,ServiceType,Status | Export-Csv -Path "C:\logs\after.csv"
然后我声明两个变量,一个用于当前状态,另一个用于“模板”,即所有 Windows 服务的期望状态。
$before = Import-Csv -Path "C:\logs\before.csv"
$after = Import-Csv -Path "C:\logs\after.csv"
然后,我将两者进行比较,仅解析那些已更改的服务,并基于该
生成 CSS 样式的 HTML 报告
Compare-Object $before $after -Property Name,DisplayName,StartType,ServiceType,Status | ConvertTo-html -Head $css | Set-Content "C:\logs\comparison.html"
这是我得到的:
它应该是这样的:
基本上,我想在原始 CSV 报告的状态列之后的新列中显示后一个 CSV 报告的状态。我还想在那之后做一个行计数,这样我就可以发送一封电子邮件,报告有多少服务发生了变化。
任何帮助将不胜感激。
您可以在 Compare-Object
之后使用 Group-Object
并从中解析出您需要的列。
$before = Import-Csv -Path "C:\logs\before.csv"
$after = Import-Csv -Path "C:\logs\after.csv"
# find the differences in the StartType and Status columns. Use -PassThru to be able to process further
$groups = Compare-Object -DifferenceObject $before -ReferenceObject $after -Property StartType, Status -PassThru |
Sort-Object Name | Group-Object Name
$result = foreach ($group in $groups) {
$refGroup = $after | Where-Object { $_.Name -eq $group.Name }
# output an object with new StartType_* and Status_* columns and capture that in variable $result
$group.Group[0] |
Select-Object *, @{Name = 'StartType_Before'; Expression = {$_.StartType}},
@{Name = 'StartType_After'; Expression = {$refGroup.StartType}},
@{Name = 'Status_Before'; Expression = {$_.Status}},
@{Name = 'Status_After'; Expression = {$refGroup.Status}} -ExcludeProperty StartType,Status, SideIndicator
}
# now convert the $result to HTML and add a summary line with the number of services that have changed
$result | ConvertTo-Html -Head $css -PostContent "<br />Services affected: $($result.Count)" |
Set-Content "C:\logs\comparison.html"
如果你还想在控制台中输出:
$result | Format-Table -AutoSize
当然也可以不使用 Compare-Object
并像下面那样做(会更慢,但我想更容易理解):
$before = Import-Csv -Path "C:\logs\before.csv"
$after = Import-Csv -Path "C:\logs\after.csv"
$result = foreach ($item in $before) {
$diff = $after | Where-Object { $_.Name -eq $item.Name -and
($_.StartType -ne $item.StartType -or $_.Status -ne $item.Status) }
if ($diff) {
$item | Select-Object *, @{Name = 'StartType_Before'; Expression = {$item.StartType}},
@{Name = 'StartType_After'; Expression = {$diff.StartType}},
@{Name = 'Status_Before'; Expression = {$item.Status}},
@{Name = 'Status_After'; Expression = {$diff.Status}} -ExcludeProperty StartType,Status
}
}
# output to console
$result | Format-Table -AutoSize
# convert to HTML
$result | ConvertTo-Html -Head $css -PostContent "<br />Services affected: $($result.Count)" |
Set-Content "C:\logs\comparison.html"
屏幕上的输出类似于
Name DisplayName ServiceType StartType_Before StartType_After Status_Before Status_After
---- ----------- ----------- ---------------- --------------- ------------- ------------
AarSvc_8246b1 Agent Activation Runtime_8246b1 224 Manual Automatic Stopped Stopped
AdobeARMservice Adobe Acrobat Update Service Win32OwnProcess Automatic Automatic Running Stopped
ALG Application Layer Gateway Service Win32OwnProcess Manual Automatic Stopped Stopped
WdNisSvc Microsoft Defender Antivirus Network Inspection Service Win32OwnProcess Manual Manual Running Stopped
我是 Powershell 的新手,我被一个更大的脚本的这一部分困住了。我需要提取所有 Windows 服务并比较它们以查看它们的启动类型状态是否已更改。如果有任何变化,我需要对它们进行计数,以便将该值放入电子邮件正文中。此外,我需要附上一份 HTML 报告,显示已更改的 Windows 服务的先前和当前状态。
我所做的如下:
这段代码生成一个 CSV 文件,显示服务的当前状态。
Get-Service | Select-Object -Property Name,DisplayName,StartType,ServiceType,Status | Export-Csv -Path "C:\logs\after.csv"
然后我声明两个变量,一个用于当前状态,另一个用于“模板”,即所有 Windows 服务的期望状态。
$before = Import-Csv -Path "C:\logs\before.csv"
$after = Import-Csv -Path "C:\logs\after.csv"
然后,我将两者进行比较,仅解析那些已更改的服务,并基于该
生成 CSS 样式的 HTML 报告Compare-Object $before $after -Property Name,DisplayName,StartType,ServiceType,Status | ConvertTo-html -Head $css | Set-Content "C:\logs\comparison.html"
这是我得到的:
它应该是这样的:
基本上,我想在原始 CSV 报告的状态列之后的新列中显示后一个 CSV 报告的状态。我还想在那之后做一个行计数,这样我就可以发送一封电子邮件,报告有多少服务发生了变化。
任何帮助将不胜感激。
您可以在 Compare-Object
之后使用 Group-Object
并从中解析出您需要的列。
$before = Import-Csv -Path "C:\logs\before.csv"
$after = Import-Csv -Path "C:\logs\after.csv"
# find the differences in the StartType and Status columns. Use -PassThru to be able to process further
$groups = Compare-Object -DifferenceObject $before -ReferenceObject $after -Property StartType, Status -PassThru |
Sort-Object Name | Group-Object Name
$result = foreach ($group in $groups) {
$refGroup = $after | Where-Object { $_.Name -eq $group.Name }
# output an object with new StartType_* and Status_* columns and capture that in variable $result
$group.Group[0] |
Select-Object *, @{Name = 'StartType_Before'; Expression = {$_.StartType}},
@{Name = 'StartType_After'; Expression = {$refGroup.StartType}},
@{Name = 'Status_Before'; Expression = {$_.Status}},
@{Name = 'Status_After'; Expression = {$refGroup.Status}} -ExcludeProperty StartType,Status, SideIndicator
}
# now convert the $result to HTML and add a summary line with the number of services that have changed
$result | ConvertTo-Html -Head $css -PostContent "<br />Services affected: $($result.Count)" |
Set-Content "C:\logs\comparison.html"
如果你还想在控制台中输出:
$result | Format-Table -AutoSize
当然也可以不使用 Compare-Object
并像下面那样做(会更慢,但我想更容易理解):
$before = Import-Csv -Path "C:\logs\before.csv"
$after = Import-Csv -Path "C:\logs\after.csv"
$result = foreach ($item in $before) {
$diff = $after | Where-Object { $_.Name -eq $item.Name -and
($_.StartType -ne $item.StartType -or $_.Status -ne $item.Status) }
if ($diff) {
$item | Select-Object *, @{Name = 'StartType_Before'; Expression = {$item.StartType}},
@{Name = 'StartType_After'; Expression = {$diff.StartType}},
@{Name = 'Status_Before'; Expression = {$item.Status}},
@{Name = 'Status_After'; Expression = {$diff.Status}} -ExcludeProperty StartType,Status
}
}
# output to console
$result | Format-Table -AutoSize
# convert to HTML
$result | ConvertTo-Html -Head $css -PostContent "<br />Services affected: $($result.Count)" |
Set-Content "C:\logs\comparison.html"
屏幕上的输出类似于
Name DisplayName ServiceType StartType_Before StartType_After Status_Before Status_After
---- ----------- ----------- ---------------- --------------- ------------- ------------
AarSvc_8246b1 Agent Activation Runtime_8246b1 224 Manual Automatic Stopped Stopped
AdobeARMservice Adobe Acrobat Update Service Win32OwnProcess Automatic Automatic Running Stopped
ALG Application Layer Gateway Service Win32OwnProcess Manual Automatic Stopped Stopped
WdNisSvc Microsoft Defender Antivirus Network Inspection Service Win32OwnProcess Manual Manual Running Stopped