【问题标题】:Powershell script for service startups pulling from another file从另一个文件中提取服务启动的 Powershell 脚本
【发布时间】:2022-12-16 08:17:01
【问题描述】:

我正在尝试实现一个 powershell 脚本并且真的需要一些帮助,因为我真的不知道该去哪里。

基本上我有一个包含以下内容的文件:

Service1 , 0
Service2 , 150
Service3 , 0
Service4 , 210

service1/2/3/4表示服务名,数字表示服务启动后延迟的秒数。

所以 service1 开始了,然后是 0 秒的延迟。然后 service2 启动,然后延迟 150 秒,然后 service3 启动,然后延迟 0 秒。等等。

有没有人之前创建过这样的 powershell 脚本或知道通过 powershell 将此信息拉入 Start-Service 脚本的方法?

任何帮助表示赞赏。

谢谢, 担

【问题讨论】:

    标签: powershell service


    【解决方案1】:

    一种简单的做法是:

    $services = get-content C:	empsample.txt 
    
    foreach ($line in $services) {
        $serviceName = $line.Split(",")[0]
        $timeWait = $line.Split(",")[1]
        Start-Service -Name $serviceName
        Start-sleep $timeWait
        $status = (Get-Service -Name $serviceName).Status
        Write-Host "The current status of $($serviceName) is $status"
    
    }
    

    【讨论】:

      【解决方案2】:

      假设 csv 文件的文件格式与您提供的一样,您可以使用 Import-Csv,使用 -Header 参数添加一些标头,然后使用 ForEach-Object 循环遍历值并将这些值通过管道传递给命令以执行相应操作你比较喜欢。

      电源外壳

      Import-csv -Path "C:FolderServices.csv" -Header "service","seconds" | ForEach-Object {
          Start-Service -Name $_.service.trim();
          Start-Sleep -Seconds $_.seconds;
          };
      

      配套资源

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2022-08-22
        • 2015-07-07
        • 2015-11-02
        • 1970-01-01
        • 2022-01-17
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多