Repository navigation
Expand file tree
/
Copy pathPersistentScript-System.ps1
More file actions
90 lines (71 loc) · 3.25 KB
/
Copy pathPersistentScript-System.ps1
File metadata and controls
90 lines (71 loc) · 3.25 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
#Your task name for the Task Scheduler
$TaskName="CustomTaskName"
#Specifies how long the Task Scheduler will attempt to restart the task. The format for this string is PDTHMS (for example, "PT5M" is 5 minutes, "PT1H" is 1 hour, and "PT20M" is 20 minutes
$ScriptInterval="PT1M"
function RunMyCode()
{
Import-Module $env:SyncroModule -WarningAction SilentlyContinue
#PUT YOUR CODE HERE
Log-Activity -Message "Persistent script Triggered" -EventName "Test Event"
}
##############################################################################
#
#
# You should not need to modify below this line.
#
#
##############################################################################
function InstallScript()
{
write-host "Installing Scipt..."
if (!(test-path -path $PersistentScriptPath)) {new-item -path $PersistentScriptPath -itemtype directory}
Copy-Item $CurrentScript $SourcePath -Force
#If the script was updated, run it with orginal parameters
$Trigger= New-ScheduledTaskTrigger -At 1:00am –Daily # Specify the trigger settings
$principal = New-ScheduledTaskPrincipal -LogonType ServiceAccount -UserID "NT AUTHORITY\SYSTEM" -RunLevel Highest
$Action= New-ScheduledTaskAction -Execute "C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe" -Argument "-ExecutionPolicy Bypass -file ""$SourcePath""" # Specify what program to run and with its parameters
$Task = Register-ScheduledTask -TaskName $TaskName -Trigger $Trigger -Principal $principal -Action $Action –Force # Specify the name of the task
$Task.Triggers.Repetition.Duration = "P1D"
$Task.Triggers.Repetition.Interval = $ScriptInterval
$Task | Set-ScheduledTask
write-host "Done."
}
$PersistentScriptPath="$Env:Programfiles\RepairTech\Syncro\PersistentScripts"
$SourcePath="$PersistentScriptPath\$TaskName.ps1"
$CurrentScript = $($MyInvocation.MyCommand.Source)
$ErrorLog="$PersistentScriptPath\Error.log"
$env:RepairTechApiBaseURL = "syncromsp.com"
$env:RepairTechApiSubDomain = (Get-ItemProperty "HKLM:SOFTWARE\WOW6432Node\RepairTech\Syncro").shop_subdomain
$env:RepairTechFilePusherPath = "C:\ProgramData\Syncro\bin\FilePusher.exe"
$env:RepairTechUUID = (Get-ItemProperty "HKLM:SOFTWARE\WOW6432Node\RepairTech\Syncro").uuid
$env:SyncroModule = "C:\ProgramData\Syncro\bin\module.psm1"
try {
#check that the destination file exists
if (!(Test-Path -path $SourcePath)) {
#If the script is not running from the Persistent Scripts path then Install script.
if (!($SourcePath -eq $CurrentScript ))
{
InstallScript
Exit
}
} else {
#If the script already exists but isn't running from the Persistent Scripts path then Update the script
if (!($SourcePath -eq $CurrentScript ))
{
Write-host "Script Already Exists!"
Write-host "Unregistering Task..."
Unregister-ScheduledTask -TaskName $TaskName -Confirm:$false -ErrorAction SilentlyContinue
InstallScript
Exit
} else {
#If the script is being run from the Persistent Scripts path then run our code.
RunMyCode
Exit
}
}
}
Catch
{
$ErrorInfo = $_ | Out-String
"ERROR [$TaskName] $ErrorInfo" | Add-Content $ErrorLog
}