[英]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 个服务;
C:\\sourcepath\\location1\\folder\\abc.dat C:\\sourcepath\\location1\\folder\\abc.dat
C:\\sourcepath\\location1\\folder\\cde.dat C:\\sourcepath\\location1\\folder\\cde.dat
C:\\sourcepath\\folder1\\logs\\logfile.txt C:\\sourcepath\\folder1\\logs\\logfile.txt
--- The flow should be like that; ---流程应该是这样的;
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
}
}
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.