简体   繁体   English

Powershell - 用于服务启动和复制文件的数组循环

[英]Powershell - Array Loop for service start and copy files

I need help on a specific issue with Powershell.我需要有关 Powershell 特定问题的帮助。

What I am trying to do is that starting multiple services one bye one and after successfully start process, I need to copy some files from one location to another.我想要做的是逐一启动多个服务,并在成功启动过程后,我需要将一些文件从一个位置复制到另一个位置。 These files are created only after the service/app is up.这些文件仅在服务/应用程序启动后创建。 I need to check it from a string in a text file (like "Service successfully started").我需要从文本文件中的字符串中检查它(如“服务已成功启动”)。

I tried to make a for-each loop but because of copying and text check locations are different, I couldn't manage to do it.我试图做一个 for-each 循环,但由于复制和文本检查位置不同,我无法做到。 And honestly, I don't have much information about nested loops.老实说,我没有太多关于嵌套循环的信息。 Maybe you can give me some ideas to make this work.也许你可以给我一些想法来完成这项工作。

For examples, for 1 service;例如,对于 1 个服务;

  • Source folder file locations;源文件夹文件位置;

C:\\sourcepath\\location1\\folder\\abc.dat C:\\sourcepath\\location1\\folder\\abc.dat

C:\\sourcepath\\location1\\folder\\cde.dat C:\\sourcepath\\location1\\folder\\cde.dat

  • txt file which needs to be checked if there is a string line called "Service successfully started" (to understand the service-app successfully started)需要检查的txt文件中是否有一行名为“Service successfully started”(以了解service-app已成功启动)

C:\\sourcepath\\folder1\\logs\\logfile.txt C:\\sourcepath\\folder1\\logs\\logfile.txt

  • Destination folder file locations D:\\destinationpath\\location1\\ (abc.dat and cde.dat files should be in same folder)目标文件夹文件位置 D:\\destinationpath\\location1\\(abc.dat 和 cde.dat 文件应在同一文件夹中)

--- The flow should be like that; ---流程应该是这样的;

  • Start a service启动服务
  • Make sure it's up as checking the txt file string确保它在检查 txt 文件字符串时启动
  • After controls, make copying process from source folder to destination for the specified files (as creating destination folder based on source folder)控制后,对指定文件进行从源文件夹到目标的复制过程(如基于源文件夹创建目标文件夹)
  • Stop the service停止服务
  • After checking it's status as stopped, again start another service and do the same processes until the last service but for different locations For example, location1 should be location2 and then location3 but the file names are the same.检查它的状态为停止后,再次启动另一个服务并执行相同的过程,直到最后一个服务,但位置不同。例如,location1 应为 location2,然后为 location3,但文件名相同。 Also destination folder should be created according to source folder.还应根据源文件夹创建目标文件夹。

Even any directions will be helpful.甚至任何方向都会有所帮助。

Edit1:编辑1:

So far, I could write code.到目前为止,我可以编写代码。

[array]$serviceNames = "lfsvc", "iphlpsvc"
[array]$app = "app1", "app2"
$sourceStart = "C:\Source\"
$destinationStart = "C:\Target\"
$logs = "\logs"

$sourceFull = $sourceStart+$app.Get(0)+"\data"
$destinationFull = $destinationStart+$app.Get(0)

ForEach ($serviceNames in $serviceNames) 
{
    Start-Service $serviceNames -ErrorAction SilentlyContinue;
    $text = Select-String -Path $sourceStart+$app.Get(0)+$logs\log.txt -Pattern "Service successfully started"
    if ($text -ne $null)
    { 
        md $destination;
        Copy-Item -Path $sourceFull\123.txt -Destination $destinationFull\123.txt
        Copy-Item -Path $sourceFull\456.txt -Destination $destinationFull\456.txt
    }
}
  • I need to point other $app values in a row as pointing other $serviceNames values accordingly.我需要将其他 $app 值连续指向其他 $serviceNames 值。
  • I need to take control the if values if wait till it shows the service successfully started line如果等到它显示服务成功启动行,我需要控制 if 值

Thanks谢谢

Edit2: If I want to write it in long way, that should be something like that. Edit2:如果我想用很长的方式写它,那应该是这样的。 (Ofc, if I can check the string from a specified text file, it would be gr8) (Ofc,如果我可以从指定的文本文件中检查字符串,它将是 gr8)

I need to shorten the codes我需要缩短代码

[array]$serviceNames = "aService", "bService"

Start-Service $serviceNames[0] -ErrorAction SilentlyContinue;
Start-Sleep -Seconds 75;
md "C:\Dest\aService\fld";
Copy-Item -Path "C:\Source\aService\fld\123.txt" -Destination "C:\Dest\aService\fld\123.txt";
Copy-Item -Path "C:\Source\aService\fld\456.txt" -Destination "C:\Dest\aService\fld\456.txt";
Copy-Item -Path "C:\Source\aService\fld\789.txt" -Destination "C:\Dest\aService\fld\789.txt";
Stop-Service $serviceNames[0] -ErrorAction SilentlyContinue;
Start-Sleep -Seconds 15;

Start-Service $serviceNames[1] -ErrorAction SilentlyContinue;
Start-Sleep -Seconds 75;
md "C:\Dest\bService\fld";
Copy-Item -Path "C:\Source\bService\fld\123.txt" -Destination "C:\Dest\bService\fld\123.txt";
Copy-Item -Path "C:\Source\bService\fld\456.txt" -Destination "C:\Dest\bService\fld\456.txt";
Copy-Item -Path "C:\Source\bService\fld\789.txt" -Destination "C:\Dest\bService\fld\789.txt";
Stop-Service $serviceNames[1] -ErrorAction SilentlyContinue;
Start-Sleep -Seconds 15;

I think I have an idea of what you want.我想我知道你想要什么。

[array]$serviceNames = "lfsvc", "iphlpsvc"
[array]$apps = "app1", "app2"
$sourceStart = "C:\Source\"
$destinationStart = "C:\Target\"
$logs = "\logs"

# main loop, which loops over the apps

foreach($app in $apps)
{
    $sourceFull = $sourceStart + $app + "\data"
    $destinationFull = $destinationStart + $app
    # each app will iterate over all of the services
    ForEach ($name in $serviceNames) 
    {
        # uses -passthru to get the service object, and pulls its status from that. Will cause any errors to terminate the script
        $status = (Start-Service $name -ErrorAction Stop -PassThru).Status

        # this while loop will cause it to pause until the service is in "running" state
        while($status -ne "Running") {Start-Sleep -Seconds 5; Get-Service $name}
        $text = Select-String -Path $($sourceStart + $app + $logs + "\log.txt") -Pattern "Service successfully started"

        # check to see if the $text variable is null or empty, if not, do the thing
        if (![string]::IsNullOrEmpty($text))
        { 
            if(!(Test-Path -Path $destinationFull)){New-Item -ItemType Directory -Path $destinationFull}
            Get-Content -Path "$sourceFull\123.txt" | Add-Content -Path "$destinationFull\123.txt"
            Get-Content -Path "$sourceFull\456.txt" | Add-Content -Path "$destinationFull\456.txt"
       
        }

        # stops the service
        $status = Stop-Service $name -PassThru

        # pauses until the service is stopped
        while($status -ne "Stopped") {Start-Sleep -Seconds 5; Get-Service $name}
    }
}

something like this?像这样的东西?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM