繁体   English   中英

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

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

我需要有关 Powershell 特定问题的帮助。

我想要做的是逐一启动多个服务,并在成功启动过程后,我需要将一些文件从一个位置复制到另一个位置。 这些文件仅在服务/应用程序启动后创建。 我需要从文本文件中的字符串中检查它(如“服务已成功启动”)。

我试图做一个 for-each 循环,但由于复制和文本检查位置不同,我无法做到。 老实说,我没有太多关于嵌套循环的信息。 也许你可以给我一些想法来完成这项工作。

例如,对于 1 个服务;

  • 源文件夹文件位置;

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

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

  • 需要检查的txt文件中是否有一行名为“Service successfully started”(以了解service-app已成功启动)

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

  • 目标文件夹文件位置 D:\\destinationpath\\location1\\(abc.dat 和 cde.dat 文件应在同一文件夹中)

---流程应该是这样的;

  • 启动服务
  • 确保它在检查 txt 文件字符串时启动
  • 控制后,对指定文件进行从源文件夹到目标的复制过程(如基于源文件夹创建目标文件夹)
  • 停止服务
  • 检查它的状态为停止后,再次启动另一个服务并执行相同的过程,直到最后一个服务,但位置不同。例如,location1 应为 location2,然后为 location3,但文件名相同。 还应根据源文件夹创建目标文件夹。

甚至任何方向都会有所帮助。

编辑1:

到目前为止,我可以编写代码。

[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
    }
}
  • 我需要将其他 $app 值连续指向其他 $serviceNames 值。
  • 如果等到它显示服务成功启动行,我需要控制 if 值

谢谢

Edit2:如果我想用很长的方式写它,那应该是这样的。 (Ofc,如果我可以从指定的文本文件中检查字符串,它将是 gr8)

我需要缩短代码

[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;

我想我知道你想要什么。

[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}
    }
}

像这样的东西?

暂无
暂无

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

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