59 lines
No EOL
2.3 KiB
PowerShell
59 lines
No EOL
2.3 KiB
PowerShell
$ScriptDirectory = if ([string]::IsNullOrWhiteSpace(($PSScriptRoot))) { Get-Location } else { $PSScriptRoot }
|
|
|
|
$DestinationDirectory = [IO.Directory]::CreateDirectory("C:\Scripts").FullName
|
|
|
|
Copy-Item -Path ([IO.Path]::Combine($ScriptDirectory, "Downloader")) `
|
|
-Destination $DestinationDirectory `
|
|
-Recurse `
|
|
-Force `
|
|
-Confirm:$false
|
|
|
|
# -------------------- Variablen --------------------
|
|
$TaskName = "Download latest application software"
|
|
$ScriptPath = [IO.Path]::Combine($DestinationDirectory, "Downloader\Script.ps1")
|
|
|
|
# Optional: Task Beschreibung
|
|
$TaskDescription = "Startet das PowerShell-Projekt automatisch beim Systemstart."
|
|
|
|
# -------------------- Existiert der Task schon? --------------------
|
|
$existingTask = Get-ScheduledTask -TaskName $TaskName -ErrorAction SilentlyContinue
|
|
|
|
if ($existingTask) {
|
|
Write-Host "Task '$TaskName' existiert bereits. Aktualisiere..." -ForegroundColor Yellow
|
|
Unregister-ScheduledTask -TaskName $TaskName -Confirm:$false
|
|
}
|
|
|
|
# -------------------- Task Action --------------------
|
|
# PowerShell-Executable
|
|
$pwshPath = (Get-Command pwsh -ErrorAction SilentlyContinue).Source
|
|
if (-not $pwshPath) { $pwshPath = (Get-Command powershell).Source }
|
|
|
|
$Action = New-ScheduledTaskAction -Execute $pwshPath -Argument "-NoProfile -ExecutionPolicy Bypass -File `"$ScriptPath`""
|
|
|
|
# -------------------- Task Trigger --------------------
|
|
# Beispiel: Beim Systemstart (kannst du ändern zu -AtLogon, -Daily etc.)
|
|
$Trigger = New-ScheduledTaskTrigger -Daily -At 20:00
|
|
|
|
# -------------------- Task Einstellungen --------------------
|
|
$Settings = New-ScheduledTaskSettingsSet -AllowStartIfOnBatteries -DontStopIfGoingOnBatteries -StartWhenAvailable
|
|
|
|
# -------------------- Task Principal --------------------
|
|
# Führt mit höchsten Privilegien unter SYSTEM aus
|
|
$Principal = New-ScheduledTaskPrincipal -UserId "SYSTEM" -RunLevel Highest
|
|
|
|
# -------------------- Task Registrierung --------------------
|
|
try{
|
|
Register-ScheduledTask `
|
|
-TaskName $TaskName `
|
|
-Description $TaskDescription `
|
|
-Action $Action `
|
|
-Trigger $Trigger `
|
|
-Principal $Principal `
|
|
-Settings $Settings
|
|
|
|
Write-Host "Task '$TaskName' wurde erfolgreich registriert!" -ForegroundColor Green
|
|
Write-Host "Pfad: $ScriptPath"
|
|
}
|
|
catch{
|
|
Write-Host "Fehler beim registrieren von Task '$TaskName'. $($_.Exception.Message)" -ForegroundColor Red
|
|
} |