Page 1 of 1

create a scheduled PowerShell task

Published: August 10, 2021 - 3:57 PM
by Discovery
Hello,
I need to create a scheduled task on Windows 2012 servers using WAPT.
Each scheduled task must execute a file named servername_sftp-backup.ps1.
I can't seem to pass the filename as a parameter; I think the ' and " are causing problems.
The error message isn't clear.
How can I store the filename (servername + 'ps1') in a variable and pass this variable into the run_powershell() command?

I tried this:
run_powershell('$pcname=[Environment]::MachineName')
run_powershell('$file="c:\tx-backups\script\"+$pcname+"_sftp-backup.ps1"
run_powershell('$Action = "New-ScheduledTaskAction -Execute "Powershell.exe " -Argument "-ExcutionPolicy Bypass -NoProfile -File"+ $file"'+'"')
run_powershell('$Trigger = New-ScheduledTaskTrigger -Daily -At 1am')
run_powershell('$Principal = New-ScheduledTaskPrincipal -LogonType S4U -RunLevel Highest')
run_powershell('$Settings = New-ScheduledTaskSettingsSet')
run_powershell('$Task = New-ScheduledTask -Action $Action -Trigger $Trigger -Settings $Settings')
run_powershell('Register-ScheduledTask -TaskName "Backup SFTP" -Action $Action -Trigger $Trigger -Settings $Settings -Principal $Principal')

The error I have:

run_powershell('$Action="New-ScheduledTaskAction -Execute "Powershell.exe" -Argument "-ExecutionPolicy Bypass -NoProfile -File "+$file+'"'')
^
SyntaxError: EOL while scanning string literal
SyntaxError: EOL while scanning string literal (setup.py, line 29)

Thanks for your help