Image

Image

Search This Blog

Saturday, March 23, 2024

Create a task that removes "shutdown task is running longer than" from other tasks

$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 = @'

cmp1
cmp2
cmp3
cmp4

'@
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

Saturday, February 03, 2024

backup cmd

:: *** SYNC DATA - Copy one way v3.6 ***
@echo off
title BACKUP in progress
SETLOCAL ENABLEDELAYEDEXPANSION
if not defined is_min set is_min=1 && start "" /min "%~dpnx0" %* && goto end
echo.
if not "%~1" == "" (set source=%~1)
if not "%~2" == "" (set destination=%~2)
if not "%~3" == "" (set rcptto=%~3) else (echo USAGE: %~0 "source" "destination" "mail@dom.tld; mail2@dom.tld" "mail.server(optional)" && goto end)
if not "%~4" == "" (set smtpsrv=%~4) else (set smtpsrv="smtp.dom.tld")
set mailfrom="%COMPUTERNAME%@%USERDNSDOMAIN%"
set emailer=%temp%\email_%random%.vbs
set logfile=%temp%\copy_report_%random%.log

:: *** Copy ***
echo > %logfile% %date% %time% *** STARTING COPY ***

robocopy %source% %destination% /E /FP /TS /XO /FFT /COPY:D /R:3 /W:5 /IPG:25 /X /V /NP /LOG:%logfile%
set erlvl=%ERRORLEVEL%
if %erlvl% EQU 16 echo >> %logfile% %date% %time% *** !!! FATAL ERROR - NOTHING COPIED !!! *** && set err=yes
if %erlvl% EQU 15 echo >> %logfile% %date% %time% * FAIL + MISMATCHES + XTRA + OKCOPY * && set err=yes
if %erlvl% EQU 14 echo >> %logfile% %date% %time% * FAIL + MISMATCHES + XTRA * && set err=yes
if %erlvl% EQU 13 echo >> %logfile% %date% %time% * FAIL + MISMATCHES + OKCOPY * && set err=yes
if %erlvl% EQU 12 echo >> %logfile% %date% %time% * FAIL + MISMATCHES * && set err=yes
if %erlvl% EQU 11 echo >> %logfile% %date% %time% * FAIL + XTRA + OKCOPY * && set err=yes
if %erlvl% EQU 10 echo >> %logfile% %date% %time% * FAIL + XTRA * && set err=yes
if %erlvl% EQU 9 echo >> %logfile% %date% %time% * FAIL + OKCOPY * && set err=yes
if %erlvl% EQU 8 echo >> %logfile% %date% %time% * FAIL * && set err=yes
if %erlvl% EQU 7 echo >> %logfile% %date% %time% * MISMATCHES + OKCOPY + XTRA *
if %erlvl% EQU 6 echo >> %logfile% %date% %time% * MISMATCHES + XTRA *
if %erlvl% EQU 5 echo >> %logfile% %date% %time% * MISMATCHES + OKCOPY *
if %erlvl% EQU 4 echo >> %logfile% %date% %time% * MISMATCHES *
if %erlvl% EQU 3 echo >> %logfile% %date% %time% * OKCOPY + XTRA *
if %erlvl% EQU 2 echo >> %logfile% %date% %time% * XTRA *
if %erlvl% EQU 1 echo >> %logfile% %date% %time% * OKCOPY *
if %erlvl% EQU 0 echo >> %logfile% %date% %time% * NO CHANGES / NOCOPY *

:: *** Delete files & folder older than 365 days ****
::forfiles /p %destination% /s /m *.* /c "cmd /c del @path" /d -365
::for /f "tokens=*" %d in ('dir %destination% /ad/b/s ^| sort /R') do rd "%d"
::echo Files older than 365 days deleted

:: *** Send Email ***
echo Set objNet = CreateObject("WScript.Network") >%emailer%
echo strHostName = objNet.ComputerName >>%emailer%
echo Set email = CreateObject("CDO.Message") >>%emailer%
if "%err%"=="" echo email.Subject = strHostName ^& " - Backup Report" >>%emailer%
if "%err%"=="yes" echo email.Subject = strHostName ^& " - FAILED Backup Report" >>%emailer%
echo email.From = %mailfrom% >>%emailer%
echo email.To = "%rcptto%" >>%emailer%
if "%err%"=="" echo email.TextBody = "Copy completed as %username% on " ^& strHostName ^& ".  Please check the attached report" >>%emailer%
if "%err%"=="yes" echo email.TextBody = "Copy as %username% has FAILED on " ^& strHostName ^& ".  Please check the attached report" >>%emailer%
echo email.AddAttachment "%logfile%" >>%emailer%
echo email.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/sendusername")="UserName" >>%emailer%
echo email.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/sendpassword")="PassWord" >>%emailer%
echo email.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/sendusing")=2 >>%emailer%
echo email.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/smtpserver")=%smtpsrv% >>%emailer%
echo email.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/smtpserverport")=25 >>%emailer%
echo email.Configuration.Fields.Update >>%emailer%
echo email.Send >>%emailer%
echo set email = Nothing >>%emailer%

start %emailer%
timeout 1 >nul /nobreak && del /q %emailer%
timeout 1 >nul /nobreak && del /q %logfile%

:end
ENDLOCAL
title
exit /B
 

Saturday, January 06, 2024

Powershell backup

 #Backup folder
$dateStr = (Get-Date -Format "yyy-MM-dd-HH-mm")
$Source = "C:\source"
$Staging = "U:\BKPTemp"
$Destination = "U:\Backup\destination_$dateStr.zip"
Get-ChildItem "U:\Backup\" -Recurse -File | Where CreationTime -lt  (Get-Date).AddDays(-90)  | Remove-Item -Force
Add-Type -AssemblyName System.IO.Compression.Filesystem
Copy-Item -Path $Source -Destination $Staging -Recurse
[System.IO.Compression.ZipFile]::CreateFromDirectory($Staging, $Destination)
Remove-Item -Path $Staging -Force -Recurse
exit

Blog Archive