簡體   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