如何使用PowerShell处理服务的无限循环?
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了如何使用PowerShell处理服务的无限循环?相关的知识,希望对你有一定的参考价值。
我有一个包含infinity循环的powershell脚本。我想使用NSSM https://nssm.cc/description创建服务但是,一旦我在脚本中执行了某些过程,该服务就无法正常工作。这就是我创建服务的方式。
$NSSMPath = (Get-Command "D:\SERVICE\nssm-2.24\win64\nssm.exe").Source
$NameService = "TestService"
$PoshPath = (Get-Command powershell.exe).Source
$PoshScriptPath = "D:\SERVICE\TestService.ps1"
$arg = '-ExecutionPolicy Bypass -NoProfile -File "0"' -f $PoshScriptPath
& $NSSMPath install $NameService $PoshPath $args
Start-Service $NameService
Get-Service $NameService
这是powersehll脚本。
for(;;)
Start-Transcript -Path "D:\SERVICE\logfile.txt"
Stop-Transcript
通过此步骤,该服务正在运行。我可以看到logfile.txt。但是一旦我在循环中进行了一些处理,就像这样]
for(;;) Start-Transcript -Path "D:\SERVICE\logfile.txt" #add acnd check - will it work or not... if(Test-Path -Path "D:\SERVICE\*.txt") Get-ChildItem -Path "D:\SERVICE\*.txt" | Rename-Item -NewName [System.IO.Path]::ChangeExtension($_.Name, ".csv") Stop-Transcript
服务正在运行,但是脚本无法运行,logfile.txt不存在。
任何人都可以帮助我。谢谢
我有一个包含infinity循环的powershell脚本。我想使用NSSM https://nssm.cc/description创建服务,但是一旦在脚本中执行了某些处理,该服务就无法使用。这就是...
答案
您正在尝试重命名由Start-Transcript打开并正在使用的文件,这将不起作用。
以上是关于如何使用PowerShell处理服务的无限循环?的主要内容,如果未能解决你的问题,请参考以下文章
如何在导航控制器中创建无限循环,如 iphone 中的 instagram 应用程序?