$batchfileToAdd = @'
@echo off
SETLOCAL ENABLEDELAYEDEXPANSION
set srvlist=UDP_SERVER_CHAN0,UDP_CANBUS_SERVER,UDP_UART_SERVER_CHAN0
for %%i in (%srvlist%) do (
powershell "$task = get-ScheduledTask -taskname %%i ; $Task.Settings.ExecutionTimeLimit = 'PT0H' ; set-ScheduledTask $task"
)
ENDLOCAL
exit /B
'@
Add-Content "C:\CAB\chgtskshtdn.bat" $batchfileToAdd
$xmlfileToAdd = @'
<Task version="1.2" xmlns="http://schemas.microsoft.com/windows/2004/02/mit/task">
<RegistrationInfo>
<Date>2024-02-20T09:21:14.0904858</Date>
<Author>myself</Author>
<Description>Uncheck "shutdown task is running longer than" for the tasks UDP_SERVER_CHAN0,UDP_CANBUS_SERVER,UDP_UART_SERVER_CHAN0</Description>
<URI>\Keep Tasks running</URI>
</RegistrationInfo>
<Triggers>
<CalendarTrigger>
<Repetition>
<Interval>PT60M</Interval>
<Duration>P1D</Duration>
<StopAtDurationEnd>false</StopAtDurationEnd>
</Repetition>
<StartBoundary>2024-02-20T09:15:39</StartBoundary>
<Enabled>true</Enabled>
<ScheduleByDay>
<DaysInterval>1</DaysInterval>
</ScheduleByDay>
</CalendarTrigger>
</Triggers>
<Settings>
<MultipleInstancesPolicy>IgnoreNew</MultipleInstancesPolicy>
<DisallowStartIfOnBatteries>false</DisallowStartIfOnBatteries>
<StopIfGoingOnBatteries>true</StopIfGoingOnBatteries>
<AllowHardTerminate>false</AllowHardTerminate>
<StartWhenAvailable>true</StartWhenAvailable>
<RunOnlyIfNetworkAvailable>false</RunOnlyIfNetworkAvailable>
<IdleSettings>
<StopOnIdleEnd>true</StopOnIdleEnd>
<RestartOnIdle>false</RestartOnIdle>
</IdleSettings>
<AllowStartOnDemand>true</AllowStartOnDemand>
<Enabled>true</Enabled>
<Hidden>false</Hidden>
<RunOnlyIfIdle>false</RunOnlyIfIdle>
<WakeToRun>false</WakeToRun>
<ExecutionTimeLimit>PT0S</ExecutionTimeLimit>
<Priority>7</Priority>
</Settings>
<Actions Context="Author">
<Exec>
<Command>C:\CAB\chgtskshtdn.bat</Command>
</Exec>
</Actions>
</Task>
'@
Add-Content "C:\CAB\KeepTasksRunning.xml" $xmlfileToAdd
$complstfileToAdd = @'
'@
Add-Content "C:\tmp\complst.txt" $complstfileToAdd
$cred = Get-Credential;
foreach($line in Get-Content C:\tmp\complst.txt) {
Write-Host "Running on $line"
$comp = New-PSSession -Credential $cred $line
Write-Host "Session to $comp established"
Copy-Item -ToSession $comp C:\CAB\chgtskshtdn.bat -Destination C:\CAB\chgtskshtdn.bat
Write-Host "bat file copied"
Copy-Item -ToSession $comp "C:\CAB\KeepTasksRunning.xml" -Destination "C:\CAB\KeepTasksRunning.xml"
Write-Host "xml file copied"
Invoke-Command -ComputerName $line -Credential $cred { $Task = Get-Content "C:\CAB\KeepTasksRunning.xml" -raw ; Register-ScheduledTask -Xml $Task -TaskName 'Keep Tasks Running' -User adminuser -Password "som3p455wrd" -Force }
Write-Host "task created"
}
Remove-Item "C:\tmp\complst.txt"
Remove-Item "C:\CAB\KeepTasksRunning.xml"
Remove-Item "C:\CAB\chgtskshtdn.bat"
exit