1

I have a powershell script contain of inifinity loop. I want to create a service using NSSM https://nssm.cc/description But the service is not working once I do some process in the script. This is how I create the service.

$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

enter image description here

This is the powersehll script.

for(;;)
{
Start-Transcript -Path "D:\SERVICE\logfile.txt"
Stop-Transcript 
}

With this scipt, the service is working. I can see the logfile.txt. BUT once I do some process in inside the loop like this

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 
}

The service is running, but the script is not working, the logfile.txt not exsit.

Anyone can help me please. Thank you

Cheries
  • 834
  • 1
  • 13
  • 32

1 Answers1

1

You're trying to rename file that was opened by Start-Transcript and is in use, this will not work.

Dmitriy
  • 21
  • 2