我需要关于Powershell的特定问题的帮助。
我想做的是逐个启动多个服务,在成功启动过程后,我需要将一些文件从一个位置复制到另一个位置。这些文件仅在服务/应用程序启动后创建。我需要从文本文件中的字符串中检查它(比如"Service successfully started"(。
我试着为每个循环创建一个,但由于复制和文本检查位置不同,我无法做到。老实说,我没有太多关于嵌套循环的信息。也许你可以给我一些想法让它发挥作用。
例如,对于1个服务;
- 源文件夹文件位置
C:\sourcepath\location1\folder\abc.dat
C: \sourcepath\location1\folder\cde.dat
- txt文件,如果存在名为";服务成功启动";(了解服务应用程序已成功启动(
C:\sourcepath\folder1\logs\logfile.txt
- 目标文件夹文件位置D: \destinationpath\location1\(abc.dat和cde.dat文件应在同一文件夹中(
--流程应该是这样的;
- 启动服务
- 确保检查txt文件字符串
- 控制后,将指定文件从源文件夹复制到目标文件夹(如基于源文件夹创建目标文件夹(
- 停止服务
- 在检查其状态为已停止后,再次启动另一个服务并执行相同的过程,直到最后一个服务,但针对不同的位置例如,位置1应该是位置2,然后是位置3,但文件名是相同的。此外,应根据源文件夹创建目标文件夹
即使是任何方向都会有所帮助。
第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)+$logslog.txt -Pattern "Service successfully started"
if ($text -ne $null)
{
md $destination;
Copy-Item -Path $sourceFull123.txt -Destination $destinationFull123.txt
Copy-Item -Path $sourceFull456.txt -Destination $destinationFull456.txt
}
}
- 我需要在一行中指向其他$app值,同时相应地指向其他$serviceNames值
- 如果等待显示服务成功启动,我需要控制if值
感谢
第2版:若我想把它写得很长,那个应该是这样的。(Ofc,如果我可以检查指定文本文件中的字符串,它将是gr8(
我需要缩短代码
[array]$serviceNames = "aService", "bService"
Start-Service $serviceNames[0] -ErrorAction SilentlyContinue;
Start-Sleep -Seconds 75;
md "C:DestaServicefld";
Copy-Item -Path "C:SourceaServicefld123.txt" -Destination "C:DestaServicefld123.txt";
Copy-Item -Path "C:SourceaServicefld456.txt" -Destination "C:DestaServicefld456.txt";
Copy-Item -Path "C:SourceaServicefld789.txt" -Destination "C:DestaServicefld789.txt";
Stop-Service $serviceNames[0] -ErrorAction SilentlyContinue;
Start-Sleep -Seconds 15;
Start-Service $serviceNames[1] -ErrorAction SilentlyContinue;
Start-Sleep -Seconds 75;
md "C:DestbServicefld";
Copy-Item -Path "C:SourcebServicefld123.txt" -Destination "C:DestbServicefld123.txt";
Copy-Item -Path "C:SourcebServicefld456.txt" -Destination "C:DestbServicefld456.txt";
Copy-Item -Path "C:SourcebServicefld789.txt" -Destination "C:DestbServicefld789.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 "$sourceFull123.txt" | Add-Content -Path "$destinationFull123.txt"
Get-Content -Path "$sourceFull456.txt" | Add-Content -Path "$destinationFull456.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}
}
}
像这样的东西?