【问题标题】:Loop services from txt file, change startup type if needed and start if needed从 txt 文件循环服务,根据需要更改启动类型并根据需要启动
【发布时间】:2020-07-28 12:36:15
【问题描述】:

我正在尝试创建将循环 txt 文件中列出的所有服务的脚本,检查服务启动类型是否正确(如果不更改)并在需要时启动服务。我对 Powershell 不是很好,并且不会真正从 Google 中找到任何有用的东西。

我的文本文件:

Service A
Service B
Service C
Service D
Service E

我当前的脚本目前看起来像这样,目前我能够从文本文件中打印每一项服务,但缺少后续步骤的信息。

$services = Get-Content .\services.txt

## Pass each service object to the pipeline and process them with the Foreach-Object cmdlet
foreach ($service in $services) {
    
    Get-Service $service | Select-Object -Property Name, StartType, Status, DisplayName
    }

困难的是每个服务都没有相同的启动类型和状态,所以它更复杂例如

  • 服务 A 需要手动运行
  • 服务 B 需要自动运行
  • 服务 C 需要手动停止

因此,如果服务 A 不是手动且正在运行,脚本将更改它们并提供有关更改的信息(写入主机?)。

我知道我可以使用命令 set-service 更改服务启动类型和状态并使用 get-service 列出状态,但我的技能还不足以在脚本中进行设置。不知道这是否可能以这种方式进行,或者它们是否是更好的方法。

【问题讨论】:

    标签: powershell foreach service


    【解决方案1】:

    最好将您的服务文本文件更改为 Csv 文件,您不仅可以在其中列出服务的名称,还可以列出所需的 StartType 和 Status,例如:

    服务,启动类型,状态 服务 A,手动,运行 服务 B,自动,运行 服务 C,手动,停止

    然后你可以编写类似的代码

    Import-Csv -Path .\services.csv | ForEach-Object {
        $changed = $false
        $service = Get-Service -Name $_.Service
        if ($service.StartType -ne $_.StartType) {
            Write-Host "Changing StartType for service $($service.Name)" -ForegroundColor Yellow
            $service | Set-Service -StartupType $_.StartType
            $changed = $true
        }
        if ($service.Status -ne $_.Status) {
            Write-Host "Changing Status for service $($service.Name)" -ForegroundColor Yellow
            $service | Set-Service -Status $_.Status
            $changed = $true
        }
    
        # refresh the info if you changed anything above
        if ($changed) { $service = Get-Service -Name $_.Service }
        # write out current status
        Write-Host "Service: $($service.Name) - StartType: $($service.StartType) - Status: $($service.Status)"
    }
    

    【讨论】:

      【解决方案2】:

      您不需要使用Select-ObjectGet-Service 直接接受服务名称的管道输入

      $Services = Get-Content .\services.txt | Get-Service
      
      ForEach( $Service in $Services )
      {
          If( $Service.StartType -eq 'Manual' -and $Service.Status -eq 'Running' )
          {        
              Set-Service -InputObject $service -StartupType 'Automatic'
          }
          # Add more logic as needed here!
      }
      

      但是,您的问题尚不清楚您将如何做出有关服务的决定。当然,有些服务应该是手动的、自动的等……您是否希望根据文本文件指示所需的状态?

      如果是这样,一个简单的解决方案可能是将配置存储在 CSV 文件中。格式为<ServiceName>, <DesiredStartType>。然后我们可以重新配置代码以应用所需的更改并更好地反馈给控制台。

      根据评论更新

      既然您确定了 csv 文件,并在 @Theo's helpful answer 上进一步构建。这是使用 CSV 输入文件的另一种方法。在这种情况下,我在提取服务后将输入转换为哈希表。这样可以轻松引用所需的配置。

      假设与 Theo 的回答相同的 CSV 布局:

      Service,StartType,Status
      Service A,Manual,Running
      Service B,Automatic,Running
      Service C,Manual,Stopped
      

      $DesiredConfig = Import-Csv c:\Temp\Services.csv
      $Services      = $DesiredConfig.Service | Get-Service
      
      # Flip config data to a dictionary
      $DesiredConfig = $DesiredConfig | Group-Object -Property Service -AsHashTable -AsString
      
      ForEach( $Service in $Services )
      {   
          $DesiredStart  = $DesiredConfig[$Service.Name].StartType
          $DesiredStatus = $DesiredConfig[$Service.Name].Status
      
          If( $Service.StartType -ne $DesiredStart -or $Service.Status -ne $DesiredStatus )
          {
              Write-Host "Changing $($Service.Name) StartType/Status : $($Service.StartType) / $($Service.Status) > $DesiredStart / $DesiredStatus) ..."
              $Service = $Service | Set-Service -StartupType $($DesiredConfig[$Service.Name].StartType) -Status $DesiredStatus -PassThru
              # You don't need to reassign or use -PassThru, however if you are going to post-report this spares you the need to
              # re-get-services.  You are going to run the set command anyhow!
          }
      }
      
      # Not needed, but just to check...
      $Services | Format-Table Name,Displayname,STartType,Status -AutoSize
      

      我没有对此进行测试,但方法应该是可靠的。

      让我知道这是否有帮助。谢谢。

      【讨论】:

      • 嗨,我已经知道哪些服务需要手动/自动,或者是否需要运行/停止,因此可以将它们存储在 csv/文本文件中。例如,当“即插即用”需要“运行”和“手动”以及“Windows 更新”需要“停止”和“手动”时,“Windows 防火墙”需要“运行”和“自动”
      • 安装特定软件时供应商列出的服务状态和启动类型
      猜你喜欢
      • 2022-08-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-05-19
      • 2020-07-15
      • 1970-01-01
      • 1970-01-01
      • 2012-10-05
      相关资源
      最近更新 更多