复制代码
- $processes=[Collections.Generic.HashSet[string]]((gps).Path);
- if([System.IO.File]::Exists('processBackup')){
- $processBack=[Collections.Generic.HashSet[string]]([string[]](gc '.\processBackup' -read 0));
- sc '.\processBackup' -Value ($processes -join "`r`n");
- $processes.ExceptWith($processBack);
- Write-Host "进程新增:`n"([string[]]$processes -join "`n");
- }else{
- sc '.\processBackup' -Value ($processes -join "`r`n");
- }
- pause;
复制代码
- # 定义备份文件名
- $previousServiceFile = "previous_services_backup.txt"
- $currentServiceFile = "current_services_backup.txt"
- # 检查是否存在上一次的服务备份文件
- if (Test-Path $previousServiceFile) {
- # 如果存在上一次的服务备份文件,将其删除
- Remove-Item -Path $previousServiceFile
- }
- # 如果当前服务备份文件存在,则将其重命名为上一次服务备份文件
- if (Test-Path $currentServiceFile) {
- Rename-Item -Path $currentServiceFile -NewName $previousServiceFile
- }
- # 备份当前系统服务状态
- $services = Get-Service | Select-Object Name, Status, StartType
- # 检查是否成功获取服务信息
- if ($services -ne $null) {
- $services | Out-File -FilePath $currentServiceFile -Encoding UTF8
- Write-Host "系统服务备份已保存到: $currentServiceFile"
- } else {
- Write-Host "无法获取服务信息。脚本已退出。"
- Exit
- }
- # 比较两次服务备份文件的差异
- $previousServiceResult = Get-Content -Path $previousServiceFile
- $currentServiceResult = Get-Content -Path $currentServiceFile
- # 手动比较服务的更改,并逆序显示
- $serviceChanges = Compare-Object -ReferenceObject $previousServiceResult -DifferenceObject $currentServiceResult
- # 定义备份文件名
- $previousProcessFile = "previous_processes_backup.txt"
- $currentProcessFile = "current_processes_backup.txt"
- # 检查是否存在上一次的进程备份文件
- if (Test-Path $previousProcessFile) {
- # 如果存在上一次的进程备份文件,将其删除
- Remove-Item -Path $previousProcessFile
- }
- # 如果当前进程备份文件存在,则将其重命名为上一次进程备份文件
- if (Test-Path $currentProcessFile) {
- Rename-Item -Path $currentProcessFile -NewName $previousProcessFile
- }
- # 备份当前系统进程
- $processes = Get-Process | Select-Object Name
- # 检查是否成功获取进程信息
- if ($processes -ne $null) {
- $processes | Out-File -FilePath $currentProcessFile -Encoding UTF8
- Write-Host "系统进程备份已保存到: $currentProcessFile"
- } else {
- Write-Host "无法获取进程信息。脚本已退出。"
- Exit
- }
- # 比较两次进程备份文件的差异
- $previousProcessResult = Get-Content -Path $previousProcessFile
- $currentProcessResult = Get-Content -Path $currentProcessFile
- # 手动比较进程的更改,并逆序显示
- $processChanges = Compare-Object -ReferenceObject $previousProcessResult -DifferenceObject $currentProcessResult
- if ($serviceChanges) {
- Write-Host "最新服务运行结果与上次结果有差异:"
- # 逆序输出结果,确保上次的结果在上面,本次的结果在下面
- $serviceChanges = $serviceChanges | Sort-Object -Descending
- $previousService = $null
- $currentService = $null
- $serviceChanges | ForEach-Object {
- $change = $_.InputObject
- $changeType = $_.SideIndicator
- # 解析服务名称、状态和启动类型
- $serviceName = $change -replace '服务 (\w+).*', '$1'
- $statusChange = $change -match '的状态从 (\w+) 变为 (\w+)'
- $startTypeChange = $change -match '启动类型从 (\w+) 变为 (\w+)'
- # 构建显示文本
- $changeText = ""
- if ($statusChange) {
- $previousStatus = $Matches[1]
- $currentStatus = $Matches[2]
- $changeText += "状态从 $previousStatus 变为 $currentStatus"
- }
- if ($startTypeChange) {
- $previousStartType = $Matches[1]
- $currentStartType = $Matches[2]
- if (-not [string]::IsNullOrEmpty($changeText)) {
- $changeText += ","
- }
- $changeText += "启动类型从 $previousStartType 变为 $currentStartType"
- }
- # 根据 SideIndicator 添加标记
- if ($changeType -eq "=>") {
- $changeText = "【本次】 服务 $serviceName $changeText"
- } elseif ($changeType -eq "<=") {
- $changeText = "【上次】 服务 $serviceName $changeText"
- }
- # 只输出包含内容的变化
- if (-not [string]::IsNullOrEmpty($changeText)) {
- Write-Host $changeText
- }
- }
- } else {
- Write-Host "最新服务运行结果与上次结果相同。"
- }
- if ($processChanges) {
- Write-Host "最新进程运行结果与上次结果有差异:"
- # 逆序输出结果,确保上次的结果在上面,本次的结果在下面
- $processChanges = $processChanges | Sort-Object -Descending
- $previousProcess = $null
- $currentProcess = $null
- $processChanges | ForEach-Object {
- $change = $_.InputObject
- $changeType = $_.SideIndicator
- # 解析进程名称
- $processName = $change
- # 构建显示文本
- $changeText = ""
- # 根据 SideIndicator 添加标记
- if ($changeType -eq "=>") {
- $changeText = "【本次】 进程 $processName 启动"
- } elseif ($changeType -eq "<=") {
- $changeText = "【上次】 进程 $processName 停止"
- }
- # 只输出包含内容的变化
- if (-not [string]::IsNullOrEmpty($changeText)) {
- Write-Host $changeText
- }
- }
- } else {
- Write-Host "最新进程运行结果与上次结果相同。"
- }
欢迎光临 批处理之家 (http://bbs.bathome.net/) | Powered by Discuz! 7.2 |